Experimental

class ivy.NativeSparseArray[source]

Bases: object

class ivy.SparseArray(data=None, *, coo_indices=None, csr_crow_indices=None, csr_col_indices=None, values=None, dense_shape=None)[source]

Bases: object

__init__(data=None, *, coo_indices=None, csr_crow_indices=None, csr_col_indices=None, values=None, dense_shape=None)[source]
property coo_indices
property csr_col_indices
property csr_crow_indices
property data
property dense_shape
to_dense_array(*, native=False)[source]
property values
ivy.allclose(a, b, /, *, rtol=1e-05, atol=1e-08, equal_nan=False, out=None)[source]

Returns a True if the two arrays are element-wise equal within given tolerance; otherwise False. The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(x2)) and the absolute difference atol are added together to compare against the absolute difference between x1 and x2. The default atol is not appropriate for comparing numbers that are much smaller than one

Parameters
  • x1 – First input array.

  • x2 – Second input array.

  • rtol (Optional[float]) – The relative tolerance parameter. (default: 1e-05)

  • atol (Optional[float]) – The absolute tolerance parameter. (default: 1e-08)

  • equal_nan (Optional[bool]) – Whether to compare NaN’s as equal. If True, NaN’s in x1 will be (default: False) considered equal to NaN’s in x2 in the output array.

  • out (Optional[Array]) – Alternate output array in which to place the result. (default: None) The default is None.

Return type

bool

Returns

ret – Returns True if the two arrays are equal within the given tolerance; False otherwise.

Examples

>>> x1 = ivy.array([1e10, 1e-7])
>>> x2 = ivy.array([1.00001e10, 1e-8])
>>> y = ivy.allclose(x1, x2)
>>> print(y)
False
>>> x1 = ivy.array([1.0, ivy.nan])
>>> x2 = ivy.array([1.0, ivy.nan])
>>> y = ivy.allclose(x1, x2, equal_nan=True)
>>> print(y)
True
>>> x1 = ivy.array([1e-10, 1e-10])
>>> x2 = ivy.array([1.00001e-10, 1e-10])
>>> y = ivy.allclose(x1, x2, rtol=0.005, atol=0.0)
>>> print(y)
True
ivy.atleast_1d(*arys)[source]

Convert inputs to arrays with at least one dimension. Scalar inputs are converted to 1-dimensional arrays, whilst higher-dimensional inputs are preserved.

Parameters

arys (Union[Array, NativeArray, bool, Number]) – One or more input arrays.

Return type

List[Array]

Returns

ret – An array, or list of arrays, each with atleast 1D. Copies are made only if necessary.

Examples

>>> ary1 = ivy.array(5)
>>> ivy.atleast_1d(ary1)
ivy.array([5])
>>> ary2 = ivy.array([[3,4]])
>>> ivy.atleast_1d(ary2)
ivy.array([[3, 4]])
>>> ivy.atleast_1d(6,7,8)
[ivy.array([6]), ivy.array([7]), ivy.array([8])]
ivy.atleast_2d(*arys)[source]

Convert inputs to arrays with at least two dimension. Scalar inputs are converted to 2-dimensional arrays, whilst higher-dimensional inputs are preserved.

Parameters

arys (Union[Array, NativeArray]) – One or more array-like sequences. Non-array inputs are converted to arrays. Arrays that already have two or more dimensions are preserved.

Return type

List[Array]

Returns

ret – An array, or list of arrays, each with atleast 2D. Copies are made only if necessary.

Examples

>>> ary1 = ivy.array(5)
>>> ivy.atleast_2d(ary1)
ivy.array([[5]])
>>> ary2 = ivy.array([[[3,4]]])
>>> ivy.atleast_2d(ary2)
ivy.array([[[3, 4]]])
>>> ivy.atleast_2d(6,7,8)
[ivy.array([[6]]), ivy.array([[7]]), ivy.array([[8]])]
ivy.atleast_3d(*arys)[source]

Convert inputs to arrays with at least three dimension. Scalar inputs are converted to 3-dimensional arrays, whilst higher-dimensional inputs are preserved.

Parameters

arys (Union[Array, NativeArray, bool, Number]) – One or more array-like sequences. Non-array inputs are converted to arrays. Arrays that already have three or more dimensions are preserved.

Return type

List[Array]

Returns

ret – An array, or list of arrays, each with a.ndim >= 3. Copies are avoided where possible, and views with three or more dimensions are returned. For example, a 1-D array of shape (N,) becomes a view of shape (1, N, 1), and a 2-D array of shape (M, N) becomes a view of shape (M, N, 1).

Examples

>>> ary1 = ivy.array([5,6])
>>> ivy.atleast_3d(ary1)
ivy.array([[[5],
        [6]]])
>>> ary2 = ivy.array([[[3,4]]])
>>> ivy.atleast_3d(ary2)
ivy.array([[[3, 4]]])
>>> ary3 = ivy.array([[3,4],[9,10]])
>>> ivy.atleast_3d(6,7,ary3)
[ivy.array([[[6]]]), ivy.array([[[7]]]), ivy.array([[[ 3],
        [ 4]],
[[ 9],

[10]]])]

ivy.avg_pool1d(x, kernel, strides, padding, /, *, data_format='NWC', out=None)[source]

Computes a 1-D avg pool given 3-D input x.

Parameters
  • x (Union[Array, NativeArray]) – Input image [batch_size, w, d_in].

  • kernel (Union[int, Tuple[int]]) – Size of the kernel i.e., the sliding window for each dimension of input. [w].

  • strides (Union[int, Tuple[int]]) – The stride of the sliding window for each dimension of input.

  • padding (str) – SAME” or “VALID” indicating the algorithm, or list indicating the per-dimension paddings.

  • data_format (str) – NWC” or “NCW”. Defaults to “NWC”. (default: 'NWC')

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

  • ret – The result of the pooling operation.

  • Both the description and the type hints above assumes an array input

  • for simplicity, but this function is *nestable, and therefore*

  • also accepts ivy.Container instances in place of any of

  • the arguments.

Examples

>>> x = ivy.arange(0, 24.).reshape((2, 3, 4))
>>> print(ivy.avg_pool1d(x, 2, 2, 'SAME'))
ivy.array([[[ 2.,  3.,  4.,  5.],
        [ 8.,  9., 10., 11.]],
[[14., 15., 16., 17.],

[20., 21., 22., 23.]]])

>>> x = ivy.arange(0, 24.).reshape((2, 3, 4))
>>> print(ivy.avg_pool1d(x, 2, 2, 'VALID'))
ivy.array([[[ 2.,  3.,  4.,  5.]],

[[14., 15., 16., 17.]]])

ivy.avg_pool2d(x, kernel, strides, padding, /, *, data_format='NHWC', out=None)[source]

Computes a 2-D average pool given 4-D input x.

Parameters
  • x (Union[Array, NativeArray]) – Input image [batch_size,h,w,d_in].

  • kernel (Union[int, Tuple[int], Tuple[int, int]]) – Size of the kernel i.e., the sliding window for each dimension of input. [h,w].

  • strides (Union[int, Tuple[int], Tuple[int, int]]) – The stride of the sliding window for each dimension of input.

  • padding (str) – SAME” or “VALID” indicating the algorithm, or list indicating the per-dimensio paddings.

  • data_format (str) – NHWC” or “NCHW”. Defaults to “NHWC”. (default: 'NHWC')

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

  • ret – The result of the pooling operation.

  • Both the description and the type hints above assumes an array input

  • for simplicity, but this function is *nestable, and therefore*

  • also accepts ivy.Container instances in place of any of

  • the arguments.

Examples

>>> x = ivy.arange(12).reshape((2, 1, 3, 2))
>>> print(ivy.avg_pool2d(x, (2, 2), (1, 1), 'SAME'))
ivy.array([[[[ 1.,  2.],
         [ 3.,  4.],
         [ 4.,  5.]]],
[[[ 7., 8.],

[ 9., 10.], [10., 11.]]]])

>>> x = ivy.arange(48).reshape((2, 4, 3, 2))
>>> print(ivy.avg_pool2d(x, 3, 1, 'VALID'))
ivy.array([[[[ 8.,  9.]],

[[14., 15.]]],

[[[32., 33.]],

[[38., 39.]]]])

ivy.avg_pool3d(x, kernel, strides, padding, /, *, data_format='NDHWC', out=None)[source]

Computes a 3-D avg pool given 5-D input x.

Parameters
  • x (Union[Array, NativeArray]) – Input volume [batch_size,d,h,w,d_in].

  • kernel (Union[int, Tuple[int], Tuple[int, int, int]]) – Convolution filters [d,h,w].

  • strides (Union[int, Tuple[int], Tuple[int, int, int]]) – The stride of the sliding window for each dimension of input.

  • padding (str) – SAME” or “VALID” indicating the algorithm, or list indicating the per-dimension paddings.

  • data_format (str) – NDHWC” or “NCDHW”. Defaults to “NDHWC”. (default: 'NDHWC')

  • out (Optional[Array]) – optional output array, for writing the result to. It must have a shape that the (default: None) inputs broadcast to.

Return type

Array

Returns

  • ret – The result of the pooling operation.

  • Both the description and the type hints above assumes an array input

  • for simplicity, but this function is *nestable, and therefore*

  • also accepts ivy.Container instances in place of any of

  • the arguments.

Examples

>>> x = ivy.arange(48.).reshape((2, 3, 2, 2, 2))
>>> print(ivy.avg_pool3d(x,2,2,'VALID'))
ivy.array([[[[[ 7.,  8.]]]],

[[[[31., 32.]]]]])

>>> print(ivy.avg_pool3d(x,2,2,'SAME'))
ivy.array([[[[[ 7.,  8.]]],

[[[19., 20.]]]],

[[[[31., 32.]]],

[[[43., 44.]]]]])

ivy.beta(a, b, /, *, shape=None, device=None, dtype=None, seed=None, out=None)[source]

Returns an array filled with random values sampled from a beta distribution.

Parameters
  • a (Union[float, NativeArray, Array]) – Alpha parameter of the beta distribution.

  • b (Union[float, NativeArray, Array]) – Beta parameter of the beta distribution.

  • shape (Optional[Union[Shape, NativeShape]]) – If the given shape is, e.g (m, n, k), then m * n * k samples are drawn (default: None) Can only be specified when mean and std are numeric values, else exception will be raised. Default is None, where a single value is returned.

  • device (Optional[Union[Device, NativeDevice]]) – device on which to create the array. ‘cuda:0’, (default: None) ‘cuda:1’, ‘cpu’ etc. (Default value = None).

  • dtype (Optional[Union[Dtype, NativeDtype]]) – output array data type. If dtype is None, the output array data (default: None) type will be the default floating point data type. Default None

  • seed (Optional[int]) – A python integer. Used to create a random seed distribution (default: None)

  • out (Optional[Array]) – optional output array, for writing the result to. It must have a shape (default: None) that the inputs broadcast to.

Return type

Array

Returns

ret – Returns an array with the given shape filled with random values sampled from a beta distribution.

ivy.count_nonzero(a, /, *, axis=None, keepdims=False, dtype=None, out=None)[source]

Counts the number of non-zero values in the array a.

Parameters
  • a (Union[Array, NativeArray]) – array for which to count non-zeros.

  • axis (Optional[Union[int, Tuple[int, ...]]]) – optional axis or tuple of axes along which to count non-zeros. Default is (default: None) None, meaning that non-zeros will be counted along a flattened version of the input array.

  • keepdims (Optional[bool]) – optional, if this is set to True, the axes that are counted are left in the (default: False) result as dimensions with size one. With this option, the result will broadcast correctly against the input array.

  • dtype (Optional[Union[Dtype, NativeDtype]]) – optional output dtype. Default is of type integer. (default: None)

  • out (Optional[Union[int, Array]]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – Number of non-zero values in the array along a given axis. Otherwise, the total number of non-zero values in the array is returned.

Examples

>>> a = ivy.array([[0, 1, 2, 3],[4, 5, 6, 7]])
>>> ivy.count_nonzero(a)
ivy.array(7)
>>> a = ivy.array([[0, 1, 2, 3],[4, 5, 6, 7]])
>>> ivy.count_nonzero(a, axis=0)
ivy.array([1, 2, 2, 2])
>>> a = ivy.array([[[0,1],[2,3]],[[4,5],[6,7]]])
>>> ivy.count_nonzero(a, axis=(0,1), keepdims=True)
ivy.array([[[3, 4]]])
ivy.dct(x, /, *, type=2, n=None, axis=-1, norm=None, out=None)[source]

Computes the 1D Discrete Cosine Tranformation of a given signal.

Parameters
  • x (Union[Array, NativeArray]) – The input signal.

  • type (Optional[Literal[1, 2, 3, 4]]) – The type of the dct. Must be 1, 2, 3 or 4. (default: 2)

  • n (Optional[int]) – The lenght of the transform. If n is less than the input signal lenght, (default: None) then x is truncated, if n is larger then x is zero-padded.

  • axis (Optional[int]) – The axis to compute the DCT along. (default: -1)

  • norm (Optional[Literal[‘ortho’]]) – The type of normalization to be applied. Must be either None or “ortho”. (default: None)

  • out (Optional[Union[Array, NativeArray]]) – optional output array, for writing the result to. (default: None)

Return type

Union[Array, NativeArray]

Returns

  • ret – Array containing the transformed input.

  • Both the description and the type hints above assumes an array input for simplicity,

  • but this function is nestable, and therefore also accepts ivy.Container

  • instances in place of any of the arguments.

Examples

With ivy.Array input:

>>> x = ivy.array([8, 16, 24, 32, 40, 48, 56, 64])
>>> ivy.dct(x, type=2, n=None, norm='ortho')
ivy.array([102., -51.5, 0., -5.39, 0., -1.61, 0., -0.406])
>>> x = ivy.array([[[8, 16, 24, 32], [40, 48, 56, 64]],
           [[1,  2,  3,  4], [ 5,  6,  7,  8]]])
>>> ivy.dct(x, type=1, n=None, axis=0, norm=None)
ivy.array([[[ 9., 18., 27., 36.],
            [45., 54., 63., 72.]],
           [[ 7., 14., 21., 28.],
            [35., 42., 49., 56.]]])
>>> x = ivy.array([[ 8.1, 16.2, 24.3, 32.4],
...                [40.5, 48.6, 56.7, 64.8]])
>>> y = ivy.zeros((2, 4), dtype=ivy.float32)
>>> ivy.dct(x, type=1, n=None, norm=None, out=y)
>>> print(y)
ivy.array([[ 1.22e+02, -3.24e+01,  1.91e-06, -8.10e+00],
           [ 3.16e+02, -3.24e+01,  3.81e-06, -8.10e+00]])
>>> x = ivy.array([8., 16., 24., 32., 40., 48., 56., 64.])
>>> ivy.dct(x, type=4, n=None, norm=None, out=x)
>>> print(x)
ivy.array([ 279. , -280. ,  128. , -115. ,   83.7,  -79.5,   69.8,  -68.7])

With one ivy.Container input:

>>> x = ivy.Container(a=ivy.array([8, 16, 24, 32, 40, 48, 56, 64]),
...                   b=ivy.array([1,  2,  3,  4,  5,  6,  7,  8]))
>>> ivy.dct(x, type=3, n=None, norm='ortho')
{
    a: ivy.array([79.5, -70.4, 30., -23.6, 13.9, -10.1, 5.2, -1.95]),
    b: ivy.array([9.94, -8.8, 3.75, -2.95, 1.74, -1.26, 0.65, -0.244])
}

With multiple ivy.Container inputs:

>>> x = ivy.Container(a=ivy.array([8, 16, 24, 32, 40, 48, 56, 64]),
...                   b=ivy.array([1,  2,  3,  4,  5,  6,  7,  8]))
>>> container_n = ivy.Container(a=9, b=4)
>>> container_type = ivy.Container(a=2, b=1)
>>> container_norm = ivy.Container(a="ortho", b=None)
>>> ivy.dct(x, type=container_type, n=container_n, norm=container_norm)
{
    a: ivy.array([96., -28.2, -31.9, 22.9, -26., 19.8, -17., 10.9,
                -5.89]),
    b: ivy.array([15., -4., 0., -1.])
}
ivy.diagflat(x, /, *, offset=0, padding_value=0, align='RIGHT_LEFT', num_rows=-1, num_cols=-1, out=None)[source]

Returns a two-dimensional array with the flattened input as a diagonal.

Parameters
  • x (Union[Array, NativeArray]) – Input data, which is flattened and set as the k-th diagonal of the output.

  • k – Diagonal to set. Positive value means superdiagonal, 0 refers to the main diagonal, and negative value means subdiagonal.

  • out (Optional[Union[Array, NativeArray]]) – optional output array, for writing the result to. It must have a shape that the (default: None) inputs broadcast to.

Return type

Array

Returns

ret – The 2-D output array.

This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

Both the description and the type hints above assumes an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances in place of any of the arguments.

Functional Examples

With ivy.Array inputs:

>>> x = ivy.array([[1,2], [3,4]])
>>> ivy.diagflat(x)
ivy.array([[1, 0, 0, 0],
           [0, 2, 0, 0],
           [0, 0, 3, 0],
           [0, 0, 0, 4]])
>>> x = ivy.array([1,2])
>>> ivy.diagflat(x, k=1)
ivy.array([[0, 1, 0],
           [0, 0, 2],
           [0, 0, 0]])
ivy.diff(x, /, *, out=None)[source]

Returns the n-th discrete difference along the given axis.

Parameters
  • x (Union[Array, NativeArray, int, list, tuple]) – array-like input.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – Rreturns the n-th discrete difference along the given axis.

Examples

>>> x = ivy.array([1, 2, 4, 7, 0])
>>> ivy.diff(x)
ivy.array([ 1,  2,  3, -7])
ivy.dirichlet(alpha, /, *, size=None, dtype=None, seed=None, out=None)[source]

Draw size samples of dimension k from a Dirichlet distribution. A Dirichlet-distributed random variable can be seen as a multivariate generalization of a Beta distribution. The Dirichlet distribution is a conjugate prior of a multinomial distribution in Bayesian inference.

Parameters
  • alpha (Union[Array, NativeArray, float, Sequence[float]]) – Sequence of floats of length k

  • size (Optional[Union[Shape, NativeShape]]) – optional int or tuple of ints, Output shape. If the given shape is, (default: None) e.g., (m, n), then m * n * k samples are drawn. Default is None, in which case a vector of length k is returned.

  • dtype (Optional[Union[Dtype, NativeDtype]]) – output array data type. If dtype is None, the output array data (default: None) type will be the default floating-point data type. Default None

  • seed (Optional[int]) – A python integer. Used to create a random seed distribution (default: None)

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – The drawn samples, of shape (size, k).

Functional Examples

>>> alpha = [1.0, 2.0, 3.0]
>>> ivy.dirichlet(alpha)
ivy.array([0.10598304, 0.21537054, 0.67864642])
>>> alpha = [1.0, 2.0, 3.0]
>>> ivy.dirichlet(alpha, size = (2,3))
ivy.array([[[0.48006698, 0.07472073, 0.44521229],
    [0.55479872, 0.05426367, 0.39093761],
    [0.19531053, 0.51675832, 0.28793114]],
[[0.12315625, 0.29823365, 0.5786101 ],

[0.15564976, 0.50542368, 0.33892656], [0.1325352 , 0.44439589, 0.42306891]]])

ivy.dsplit(ary, indices_or_sections, /, *, out=None)[source]

Split an array into multiple sub-arrays along the 3rd axis.

Parameters
  • ary (Union[Array, NativeArray]) – Array input.

  • indices_or_sections (Union[int, Tuple[int]]) – If indices_or_sections is an integer n, the array is split into n sections. If the array is divisible by n along the 3rd axis, each section will be of equal size. If input is not divisible by n, the sizes of the first int(ary.size(0) % n) sections will have size int(ary.size(0) / n) + 1, and the rest will have size int(ary.size(0) / n). If indices_or_sections is a tuple of ints, then input is split at each of the indices in the tuple.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – input array split along the 3rd axis.

Examples

>>> ary = ivy.array(
    [[[ 0.,   1.,   2.,   3.],
      [ 4.,   5.,   6.,   7.]],
     [[ 8.,   9.,  10.,  11.],
      [12.,  13.,  14.,  15.]]]
    )
>>> ivy.dsplit(ary, 2)
[ivy.array([[[ 0.,  1.], [ 4.,  5.]], [[ 8.,  9.], [12., 13.]]]),
 ivy.array([[[ 2.,  3.], [ 6.,  7.]], [[10., 11.], [14., 15.]]])]
ivy.dstack(arrays, /, *, out=None)[source]

Stack arrays in sequence depth wise (along third axis).

Parameters

arrays (Sequence[Array]) – Sequence of arrays to be stacked.

Return type

Array

Returns

ret – The array formed by stacking the given arrays.

Examples

>>> x = ivy.array([1, 2, 3])
>>> y = ivy.array([2, 3, 4])
>>> ivy.dstack((x, y))
ivy.array([[[1, 2],
            [2, 3],
            [3, 4]]])
>>> x = ivy.array([[1], [2], [3]])
>>> y = ivy.array([[2], [3], [4]])
>>> ivy.dstack((x, y))
ivy.array([[[1, 2]],
           [[2, 3]],
           [[3, 4]]])
ivy.exp2(x, /, *, out=None)[source]

Calculate 2**p for all p in the input array.

Parameters
  • x (Union[Array, float, list, tuple]) – Array-like input.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – Element-wise 2 to the power x. This is a scalar if x is a scalar.

Examples

>>> x = ivy.array([1, 2, 3])
>>> ivy.exp2(x)
ivy.array([2.,    4.,   8.])
>>> x = [5, 6, 7]
>>> ivy.exp2(x)
ivy.array([32.,   64.,  128.])
ivy.fix(x, /, *, out=None)[source]

Round an array of floats element-wise to nearest integer towards zero. The rounded values are returned as floats.

Parameters
  • x (Union[Array, NativeArray, float, int, list, tuple]) – Array input.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – Array of floats with elements corresponding to input elements rounded to nearest integer towards zero, element-wise.

Examples

>>> x = ivy.array([2.1, 2.9, -2.1])
>>> ivy.fix(x)
ivy.array([ 2.,  2., -2.])
ivy.flatten(x, /, *, start_dim=0, end_dim=-1, order='C', out=None)[source]
Flattens input by reshaping it into a one-dimensional tensor.

If start_dim or end_dim are passed, only dimensions starting with start_dim and ending with end_dim are flattened. The order of elements in input is unchanged.

Parameters
  • x (Union[Array, NativeArray]) – input array to flatten.

  • start_dim (Optional[int]) – first dim to flatten. If not set, defaults to 0. (default: 0)

  • end_dim (Optional[int]) – last dim to flatten. If not set, defaults to -1. (default: -1)

  • order (Optional[str]) – Read the elements of the input container using this index order, (default: 'C') and place the elements into the reshaped array using this index order. ‘C’ means to read / write the elements using C-like index order, with the last axis index changing fastest, back to the first axis index changing slowest. ‘F’ means to read / write the elements using Fortran-like index order, with the first index changing fastest, and the last index changing slowest. Note that the ‘C’ and ‘F’ options take no account of the memory layout of the underlying array, and only refer to the order of indexing. Default order is ‘C’

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

  • ret – the flattened array over the specified dimensions.

  • This function conforms to the `Array API Standard

  • <https (//data-apis.org/array-api/latest/>`_. This docstring is an extension of the)

  • `docstring <https (//data-apis.org/array-api/latest/API_specification/generated/signatures.manipulation_functions.concat.html>`_ )

  • in the standard.

  • Both the description and the type hints above assumes an array input for simplicity,

  • but this function is nestable, and therefore also accepts ivy.Container

  • instances in place of any of the arguments.

Examples

With ivy.Array input:

>>> x = ivy.array([[1,2], [3,4]])
>>> ivy.flatten(x)
ivy.array([1, 2, 3, 4])
>>> x = ivy.array([[1,2], [3,4]])
>>> ivy.flatten(x, order='F')
ivy.array([1, 3, 2, 4])
>>> x = ivy.array(
    [[[[ 5,  5,  0,  6],
     [17, 15, 11, 16],
     [ 6,  3, 13, 12]],
[[ 6, 18, 10, 4],

[ 5, 1, 17, 3], [14, 14, 18, 6]]],

[[[12, 0, 1, 13],

[ 8, 7, 0, 3], [19, 12, 6, 17]],

[[ 4, 15, 6, 15],

[ 0, 5, 17, 9], [ 9, 3, 6, 19]]],

[[[17, 13, 11, 16],

[ 4, 18, 17, 4], [10, 10, 9, 1]],

[[19, 17, 13, 10],

[ 4, 19, 16, 17], [ 2, 12, 8, 14]]]] )

>>> ivy.flatten(x, start_dim = 1, end_dim = 2)
ivy.array(
    [[[ 5,  5,  0,  6],
      [17, 15, 11, 16],
      [ 6,  3, 13, 12],
      [ 6, 18, 10,  4],
      [ 5,  1, 17,  3],
      [14, 14, 18,  6]],
[[12, 0, 1, 13],

[ 8, 7, 0, 3], [19, 12, 6, 17], [ 4, 15, 6, 15], [ 0, 5, 17, 9], [ 9, 3, 6, 19]],

[[17, 13, 11, 16],

[ 4, 18, 17, 4], [10, 10, 9, 1], [19, 17, 13, 10], [ 4, 19, 16, 17], [ 2, 12, 8, 14]]]))

ivy.fliplr(m, /, *, out=None)[source]

Flip array in the left/right direction. Flip the entries in each column in the left/right direction. Columns are preserved, but appear in a different order than before.

Parameters
  • m (Union[Array, NativeArray]) – The array to be flipped. Must be at least 2-D.

  • out (Optional[Union[Array, NativeArray]]) – optional output array, for writing the result to. (default: None)

Return type

Union[Array, NativeArray]

Returns

ret – Array corresponding to input array with elements order reversed along axis 1.

Examples

>>> m = ivy.diag([1, 2, 3])
>>> ivy.fliplr(m)
ivy.array([[0, 0, 1],
       [0, 2, 0],
       [3, 0, 0]])
ivy.flipud(m, /, *, out=None)[source]

Flip array in the up/down direction. Flip the entries in each column in the up/down direction. Rows are preserved, but appear in a different order than before.

Parameters
  • m (Union[Array, NativeArray]) – The array to be flipped.

  • out (Optional[Union[Array, NativeArray]]) – optional output array, for writing the result to. (default: None)

Return type

Union[Array, NativeArray]

Returns

ret – Array corresponding to input array with elements order reversed along axis 0.

Examples

>>> m = ivy.diag([1, 2, 3])
>>> ivy.flipud(m)
ivy.array([[ 0.,  0.,  3.],
    [ 0.,  2.,  0.],
    [ 1.,  0.,  0.]])
ivy.float_power(x1, x2, /, *, out=None)[source]

Raise each base in x1 to the positionally-corresponding power in x2. x1 and x2 must be broadcastable to the same shape. This differs from the power function in that integers, float16, and float32 are promoted to floats with a minimum precision of float64 so that the result is always inexact.

Parameters
  • x1 (Union[Array, float, list, tuple]) – Array-like with elements to raise in power.

  • x2 (Union[Array, float, list, tuple]) – Array-like of exponents. If x1.shape != x2.shape, they must be broadcastable to a common shape (which becomes the shape of the output).

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – The bases in x1 raised to the exponents in x2. This is a scalar if both x1 and x2 are scalars

Examples

>>> x1 = ivy.array([1, 2, 3, 4, 5])
>>> ivy.float_power(x1, 3)
ivy.array([1.,    8.,   27.,   64.,  125.])
>>> x1 = ivy.array([1, 2, 3, 4, 5])
>>> x2 = ivy.array([2, 3, 3, 2, 1])
>>> ivy.float_power(x1, x2)
ivy.array([1.,   8.,  27.,  16.,   5.])
ivy.fmax(x1, x2, /, *, out=None)[source]

Computes the element-wise maximums of two arrays. Differs from ivy.maximum in the case where one of the elements is NaN. ivy.maximum returns the NaN element while ivy.fmax returns the non-NaN element.

Parameters
  • x1 (Union[Array, NativeArray]) – First input array.

  • x2 (Union[Array, NativeArray]) – Second input array.

  • out (Optional[Union[Array, NativeArray]]) – optional output array, for writing the result to. (default: None)

Return type

Union[Array, NativeArray]

Returns

ret – Array with element-wise maximums.

Examples

>>> x1 = ivy.array([2, 3, 4])
>>> x2 = ivy.array([1, 5, 2])
>>> ivy.fmax(x1, x2)
ivy.array([ 2.,  5.,  4.])
>>> x1 = ivy.array([ivy.nan, 0, ivy.nan])
>>> x2 = ivy.array([0, ivy.nan, ivy.nan])
>>> ivy.fmax(x1, x2)
ivy.array([ 0.,  0.,  nan])
ivy.fmod(x1, x2, /, *, out=None)[source]

Computes the element-wise remainder of divisions of two arrays.

Parameters
  • x1 (Union[Array, NativeArray]) – First input array.

  • x2 (Union[Array, NativeArray]) – Second input array

  • out (Optional[Union[Array, NativeArray]]) – optional output array, for writing the result to. (default: None)

Return type

Union[Array, NativeArray]

Returns

ret – Array with element-wise remainder of divisions.

Examples

>>> x1 = ivy.array([2, 3, 4])
>>> x2 = ivy.array([1, 5, 2])
>>> ivy.fmod(x1, x2)
ivy.array([ 0,  3,  0])
>>> x1 = ivy.array([ivy.nan, 0, ivy.nan])
>>> x2 = ivy.array([0, ivy.nan, ivy.nan])
>>> ivy.fmod(x1, x2)
ivy.array([ nan,  nan,  nan])
ivy.gamma(alpha, beta, /, *, shape, device=None, dtype=None, seed=None, out=None)[source]

Returns an array filled with random values sampled from a gamma distribution.

Parameters
  • shape (Union[float, NativeArray, Array]) – Shape parameter of the gamma distribution.

  • alpha (Union[float, NativeArray, Array]) – Alpha parameter of the gamma distribution.

  • beta (Union[float, NativeArray, Array]) – Beta parameter of the gamma distribution.

  • device (Optional[Union[Device, NativeDevice]]) – device on which to create the array. ‘cuda:0’, (default: None) ‘cuda:1’, ‘cpu’ etc. (Default value = None).

  • dtype (Optional[Union[Dtype, NativeDtype]]) – output array data type. If dtype is None, the output array data (default: None) type will be the default floating point data type. Default None

  • seed (Optional[int]) – A python integer. Used to create a random seed distribution (default: None)

  • out (Optional[Array]) – optional output array, for writing the result to. It must have a shape (default: None) that the inputs broadcast to.

Return type

Array

Returns

ret – Returns an array filled with random values sampled from a gamma distribution.

ivy.gcd(x1, x2, /, *, out=None)[source]

Returns the greatest common divisor of |x1| and |x2|.

Parameters
  • x1 (Union[Array, NativeArray, int, list, tuple]) – First array-like input.

  • x2 (Union[Array, NativeArray, int, list, tuple]) – Second array-input.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – Element-wise gcd of |x1| and |x2|.

Examples

>>> x1 = ivy.array([1, 2, 3])
>>> x2 = ivy.array([4, 5, 6])
>>> ivy.gcd(x1, x2)
ivy.array([1.,    1.,   3.])
>>> x1 = ivy.array([1, 2, 3])
>>> ivy.gcd(x1, 10)
ivy.array([1.,   2.,  1.])
ivy.hamming_window(window_length, /, *, periodic=True, alpha=0.54, beta=0.46, dtype=None, out=None)[source]

Computes the Hamming window with window length window_length

Parameters
  • window_length (int) – an int defining the length of the window.

  • periodic (Optional[bool]) – If True, returns a window to be used as periodic function. (default: True) If False, return a symmetric window.

  • alpha (Optional[float]) – The coefficient alpha in the hamming window equation (default: 0.54)

  • beta (Optional[float]) – The coefficient beta in the hamming window equation (default: 0.46)

  • dtype (Optional[Union[Array, NativeArray]]) – data type of the returned array. (default: None)

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – The array containing the window.

Examples

>>> ivy.hamming_window(5)
ivy.array([0.0800, 0.3979, 0.9121, 0.9121, 0.3979])
>>> ivy.hamming_window(5, periodic=False)
ivy.array([0.0800, 0.5400, 1.0000, 0.5400, 0.0800])
>>> ivy.hamming_window(5, periodic=False, alpha=0.2, beta=2)
ivy.array([-1.8000,  0.2000,  2.2000,  0.2000, -1.8000])
ivy.hann_window(window_length, periodic=True, dtype=None, *, out=None)[source]

Generate a Hann window. The Hanning window is a taper formed by using a weighted cosine.

Parameters
  • window_length (int) – the size of the returned window.

  • periodic (Optional[bool]) – If True, returns a window to be used as periodic function. (default: True) If False, return a symmetric window.

  • dtype (Optional[Union[Dtype, NativeDtype]]) – The data type to produce. Must be a floating point type. (default: None)

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – The array containing the window.

Functional Examples

>>> ivy.hann_window(4, True)
ivy.array([0. , 0.5, 1. , 0.5])
>>> ivy.hann_window(7, False)
ivy.array([0.  , 0.25, 0.75, 1.  , 0.75, 0.25, 0.  ])
ivy.heaviside(x1, x2, /, *, out=None)[source]

Computes the Heaviside step function for each element in x1.

Parameters
  • x1 (Union[Array, NativeArray]) – input array.

  • x2 (Union[Array, NativeArray]) – values to use where x1 is zero.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – output array with element-wise Heaviside step function of x1. This is a scalar if both x1 and x2 are scalars.

Examples

With ivy.Array input:

>>> x1 = ivy.array([-1.5, 0, 2.0])
>>> x2 = ivy.array([0.5])
>>> ivy.heaviside(x1, x2)
ivy.array([0.0000, 0.5000, 1.0000])
>>> x1 = ivy.array([-1.5, 0, 2.0])
>>> x2 = ivy.array([1.2, -2.0, 3.5])
>>> ivy.heaviside(x1, x2)
ivy.array([0., -2., 1.])
ivy.hsplit(ary, indices_or_sections, /, *, out=None)[source]

Split an array into multiple sub-arrays horizontally.

Parameters
  • ary (Union[Array, NativeArray]) – Array input.

  • indices_or_sections (Union[int, Tuple[int]]) – If indices_or_sections is an integer n, the array is split into n sections. If the array is divisible by n along the 3rd axis, each section will be of equal size. If input is not divisible by n, the sizes of the first int(ary.size(0) % n) sections will have size int(ary.size(0) / n) + 1, and the rest will have size int(ary.size(0) / n). If indices_or_sections is a tuple of ints, then input is split at each of the indices in the tuple.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – input array split horizontally.

Examples

>>> ary = ivy.array(
        [[0.,  1., 2., 3.],
         [4.,  5., 6,  7.],
         [8.,  9., 10., 11.],
         [12., 13., 14., 15.]]
        )
>>> ivy.vsplit(ary, 2)
[ivy.array([[ 0.,  1.],
                [ 4.,  5.],
                [ 8.,  9.],
                [12., 13.]]),
     ivy.array([[ 2.,  3.],
                [ 6.,  7.],
                [10., 11.],
                [14., 15.]]))
ivy.hstack(arrays, /)[source]

Stack arrays in sequence horizotally (column wise).

Parameters

arrays (Sequence[Array]) – Sequence of arrays to be stacked.

Return type

Array

Returns

ret – The array formed by stacking the given arrays.

Examples

>>> x = ivy.array([1, 2, 3])
>>> y = ivy.array([2, 3, 4])
>>> ivy.hstack((x, y))
ivy.array([1, 2, 3, 2, 3, 4])
>>> x = ivy.array([1, 2, 3])
>>> y = ivy.array([0, 0, 0])
>>> ivy.hstack((x, y, x))
ivy.array([1, 2, 3, 0, 0, 0, 1, 2, 3])
>>> y = [ivy.array([[5, 6]]), ivy.array([[7, 8]])]
>>> print(ivy.hstack(y))
ivy.array([[5, 6, 7, 8]])
ivy.i0(x, /, *, out=None)[source]

Computes the Bessel i0 function of x element-wise.

Parameters
  • x (Union[Array, NativeArray]) – Array input.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – Array with the modified Bessel function evaluated at each of the elements of x.

Examples

>>> x = ivy.array([1, 2, 3])
>>> ivy.i0(x)
ivy.array([1.26606588, 2.2795853 , 4.88079259])
ivy.is_ivy_sparse_array(x)[source]
ivy.is_native_sparse_array(x)[source]
ivy.isclose(a, b, /, *, rtol=1e-05, atol=1e-08, equal_nan=False, out=None)[source]

Returns a boolean array where two arrays are element-wise equal within a tolerance. The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b. The default atol is not appropriate for comparing numbers that are much smaller than one

Parameters
  • a (Union[Array, NativeArray]) – First input array.

  • b (Union[Array, NativeArray]) – Second input array.

  • rtol (Optional[float]) – The relative tolerance parameter. (default: 1e-05)

  • atol (Optional[float]) – The absolute tolerance parameter. (default: 1e-08)

  • equal_nan (Optional[bool]) – Whether to compare NaN’s as equal. If True, NaN’s in a will be (default: False) considered equal to NaN’s in b in the output array.

  • out (Optional[Array]) – Alternate output array in which to place the result. (default: None) The default is None.

Return type

Array

Returns

ret – Returns a boolean array of where a and b are equal within the given tolerance. If both a and b are scalars, returns a single boolean value.

Examples

>>> ivy.isclose([1e10,1e-7], [1.00001e10,1e-8])
ivy.array([True, False])
>>> ivy.isclose([1.0, ivy.nan], [1.0, ivy.nan], equal_nan=True)
ivy.array([True, True])
>>> ivy.isclose([1e-100, 1e-7], [0.0, 0.0], atol=0.0)
ivy.array([False, False])
>>> ivy.isclose([1e-10, 1e-10], [1e-20, 0.999999e-10], rtol=0.005, atol=0.0)
ivy.array([False, True])
ivy.isin(elements, test_elements, /, *, assume_unique=False, invert=False)[source]

Tests if each element of elements is in test_elements.

Parameters
  • elements (Union[Array, NativeArray]) – input array

  • test_elements (Union[Array, NativeArray]) – values against which to test for each input element

  • assume_unique (Optional[bool]) – If True, assumes both elements and test_elements contain unique elements, (default: False) which can speed up the calculation. Default value is False.

  • invert (Optional[bool]) – If True, inverts the boolean return array, resulting in True values for (default: False) elements not in test_elements. Default value is False.

Return type

Array

Returns

ret – output a boolean array of the same shape as elements that is True for elements in test_elements and False otherwise.

Examples

>>> x = ivy.array([[10, 7, 4], [3, 2, 1]])
>>> y = ivy.array([1, 2, 3])
>>> ivy.isin(x, y)
ivy.array([[False, False, False], [ True,  True,  True]])
>>> x = ivy.array([3, 2, 1, 0])
>>> y = ivy.array([1, 2, 3])
>>> ivy.isin(x, y, invert=True)
ivy.array([False, False, False,  True])
ivy.isneginf(x, /, *, out=None)[source]

Test element-wise for negative infinity, return result as bool array.

Parameters
  • x (Union[Array, float, list, tuple]) – Array-like input.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – Returns a boolean array with values True where the corresponding element of the input is negative infinity and values False where the element of the input is not negative infinity.

Examples

>>> x = ivy.array([1, 2, -ivy.inf])
>>> ivy.isneginf(x)
ivy.array([False, False,  True])
>>> x = [5, -ivy.inf, ivy.inf]
>>> ivy.isneginf(x)
ivy.array([False, True,  False])
ivy.isposinf(x, /, *, out=None)[source]

Test element-wise for positive infinity, return result as bool array.

Parameters
  • x (Union[Array, float, list, tuple]) – Array-like input.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – Returns a boolean array with values True where the corresponding element of the input is positive infinity and values False where the element of the input is not positive infinity.

Examples

>>> x = ivy.array([1, 2, ivy.inf])
>>> ivy.isposinf(x)
ivy.array([False, False,  True])
>>> x = [5, -ivy.inf, ivy.inf]
>>> ivy.isposinf(x)
ivy.array([False, False,  True])
ivy.kaiser_bessel_derived_window(window_length, periodic=True, beta=12.0, *, dtype=None, out=None)[source]

Computes the Kaiser bessel derived window with window length window_length and shape beta

Parameters
  • window_length (int) – an int defining the length of the window.

  • periodic (bool) – If True, returns a periodic window suitable for use in spectral analysis. (default: True) If False, returns a symmetric window suitable for use in filter design.

  • beta (float) – a float used as shape parameter for the window. (default: 12.0)

  • dtype (Optional[Union[Dtype, NativeDtype]]) – data type of the returned array (default: None)

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – The array containing the window.

Functional Examples

>>> ivy.kaiser_bessel_derived_window(5)
ivy.array([0.00713103, 0.70710677, 0.99997455, 0.99997455, 0.70710677])
>>> ivy.kaiser_derived_window(5, False)
ivy.array([0.00726415, 0.9999736 , 0.9999736 , 0.00726415])
>>> ivy.kaiser_derived_window(5, False, 5)
ivy.array([0.18493208, 0.9827513 , 0.9827513 , 0.18493208])
ivy.kaiser_window(window_length, periodic=True, beta=12.0, *, dtype=None, out=None)[source]

Computes the Kaiser window with window length window_length and shape beta

Parameters
  • window_length (int) – an int defining the length of the window.

  • periodic (bool) – If True, returns a periodic window suitable for use in spectral analysis. (default: True) If False, returns a symmetric window suitable for use in filter design.

  • beta (float) – a float used as shape parameter for the window. (default: 12.0)

  • dtype (Optional[Union[Array, NativeArray]]) – data type of the returned array. (default: None)

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – The array containing the window.

Examples

>>> ivy.kaiser_window(5)
ivy.array([5.2773e-05, 1.0172e-01, 7.9294e-01, 7.9294e-01, 1.0172e-01]])
>>> ivy.kaiser_window(5, True, 5)
ivy.array([0.0367, 0.4149, 0.9138, 0.9138, 0.4149])
>>> ivy.kaiser_window(5, False, 5)
ivy.array([0.0367, 0.5529, 1.0000, 0.5529, 0.0367])
ivy.kron(a, b, /, *, out=None)[source]

Computes the Kronecker product, a composite array made of blocks of the second array scaled by the first.

Parameters
  • a (Union[Array, NativeArray]) – First input array.

  • b (Union[Array, NativeArray]) – Second input array

  • out (Optional[Array]) – optional output array, for writing the result to. It must have a shape that the (default: None) inputs broadcast to.

Return type

Array

Returns

ret – Array representing the Kronecker product of the input arrays.

Examples

>>> a = ivy.array([1,2])
>>> b = ivy.array([3,4])
>>> ivy.kron(a, b)
ivy.array([3, 4, 6, 8])
ivy.lcm(x1, x2, /, *, out=None)[source]

Computes the element-wise least common multiple (LCM) of x1 and x2.

Parameters
  • x1 (Union[Array, NativeArray]) – first input array, must be integers

  • x2 (Union[Array, NativeArray]) – second input array, must be integers

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – an array that includes the element-wise least common multiples of x1 and x2

Examples

With ivy.Array input:

>>> x1=ivy.array([2, 3, 4])
>>> x2=ivy.array([5, 8, 15])
>>> x1.lcm(x1, x2)
ivy.array([10, 21, 60])
ivy.logaddexp2(x1, x2, /, *, out=None)[source]

Calculates log2(2**x1 + 2**x2).

Parameters
  • x1 (Union[Array, NativeArray, float, list, tuple]) – First array-like input.

  • x2 (Union[Array, NativeArray, float, list, tuple]) – Second array-input.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – Element-wise logaddexp2 of x1 and x2.

Examples

>>> x1 = ivy.array([1, 2, 3])
>>> x2 = ivy.array([4, 5, 6])
>>> ivy.logaddexp2(x1, x2)
ivy.array([4.169925, 5.169925, 6.169925])
ivy.max_pool1d(x, kernel, strides, padding, /, *, data_format='NWC', out=None)[source]

Computes a 1-D max pool given 3-D input x.

Parameters
  • x (Union[Array, NativeArray]) – Input image [batch_size, w, d_in].

  • kernel (Union[int, Tuple[int]]) – Size of the kernel i.e., the sliding window for each dimension of input. [w].

  • strides (Union[int, Tuple[int]]) – The stride of the sliding window for each dimension of input.

  • padding (str) – SAME” or “VALID” indicating the algorithm, or list indicating the per-dimension paddings.

  • data_format (str) – NWC” or “NCW”. Defaults to “NWC”. (default: 'NWC')

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

  • ret – The result of the pooling operation.

  • Both the description and the type hints above assumes an array input

  • for simplicity, but this function is *nestable, and therefore*

  • also accepts ivy.Container instances in place of any of

  • the arguments.

Examples

>>> x = ivy.arange(0, 24.).reshape((2, 3, 4))
>>> print(ivy.max_pool1d(x, 2, 2, 'SAME'))
ivy.array([[[ 4.,  5.,  6.,  7.],
        [ 8.,  9., 10., 11.]],
[[16., 17., 18., 19.],

[20., 21., 22., 23.]]])

>>> x = ivy.arange(0, 24.).reshape((2, 3, 4))
>>> print(ivy.max_pool1d(x, 2, 2, 'VALID'))
ivy.array([[[ 4.,  5.,  6.,  7.]],

[[16., 17., 18., 19.]]])

ivy.max_pool2d(x, kernel, strides, padding, /, *, data_format='NHWC', out=None)[source]

Computes a 2-D max pool given 4-D input x.

Parameters
  • x (Union[Array, NativeArray]) – Input image [batch_size,h,w,d_in].

  • kernel (Union[int, Tuple[int], Tuple[int, int]]) – Size of the kernel i.e., the sliding window for each dimension of input. [h,w].

  • strides (Union[int, Tuple[int], Tuple[int, int]]) – The stride of the sliding window for each dimension of input.

  • padding (str) – SAME” or “VALID” indicating the algorithm, or list indicating the per-dimensio paddings.

  • data_format (str) – NHWC” or “NCHW”. Defaults to “NHWC”. (default: 'NHWC')

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

  • ret – The result of the pooling operation.

  • Both the description and the type hints above assumes an array input

  • for simplicity, but this function is *nestable, and therefore*

  • also accepts ivy.Container instances in place of any of

  • the arguments.

Examples

>>> x = ivy.arange(12).reshape((2, 1, 3, 2))
>>> print(ivy.max_pool2d(x, (2, 2), (1, 1), 'SAME'))
ivy.array([[[[ 2,  3],
 [ 4,  5],
 [ 4,  5]]],
[[[ 8,  9],
 [10, 11],
 [10, 11]]]])
>>> x = ivy.arange(48).reshape((2, 4, 3, 2))
>>> print(ivy.max_pool2d(x, 3, 1, 'VALID'))
ivy.array([[[[16, 17]],
[[22, 23]]],
[[[40, 41]],
[[46, 47]]]])
ivy.max_pool3d(x, kernel, strides, padding, /, *, data_format='NDHWC', out=None)[source]

Computes a 3-D max pool given 5-D input x.

Parameters
  • x (Union[Array, NativeArray]) – Input volume [batch_size,d,h,w,d_in].

  • kernel (Union[int, Tuple[int], Tuple[int, int, int]]) – Convolution filters [d,h,w].

  • strides (Union[int, Tuple[int], Tuple[int, int, int]]) – The stride of the sliding window for each dimension of input.

  • padding (str) – SAME” or “VALID” indicating the algorithm, or list indicating the per-dimension paddings.

  • data_format (str) – NDHWC” or “NCDHW”. Defaults to “NDHWC”. (default: 'NDHWC')

  • out (Optional[Array]) – optional output array, for writing the result to. It must have a shape that the (default: None) inputs broadcast to.

Return type

Array

Returns

  • ret – The result of the pooling operation.

  • Both the description and the type hints above assumes an array input

  • for simplicity, but this function is *nestable, and therefore*

  • also accepts ivy.Container instances in place of any of

  • the arguments.

Examples

>>> x = ivy.arange(48.).reshape((2, 3, 2, 2, 2))
>>> print(ivy.max_pool3d(x, 2, 2, 'VALID'))
ivy.array([[[[[14., 15.]]]],

[[[[38., 39.]]]]])

>>> print(ivy.max_pool3d(x, 2, 2, 'SAME'))
ivy.array([[[[[14., 15.]]],

[[[22., 23.]]]],

[[[[38., 39.]]],

[[[46., 47.]]]]])

ivy.median(input, /, *, axis=None, keepdims=False, out=None)[source]

Compute the median along the specified axis.

Parameters
  • input (Array) – Input array.

  • axis (Optional[Union[Tuple[int], int]]) – Axis or axes along which the medians are computed. The default is to compute (default: None) the median along a flattened version of the array.

  • keepdims (Optional[bool]) – If this is set to True, the axes which are reduced are left in the result (default: False) as dimensions with size one.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – The median of the array elements.

Functional Examples

>>> a = ivy.array([[10, 7, 4], [3, 2, 1]])
>>> ivy.median(a)
3.5
>>> ivy.median(a, axis=0)
ivy.array([6.5, 4.5, 2.5])
ivy.moveaxis(a, source, destination, /, *, out=None)[source]

Move axes of an array to new positions..

Parameters
  • a (Union[Array, NativeArray]) – The array whose axes should be reordered.

  • source (Union[int, Sequence[int]]) – Original positions of the axes to move. These must be unique.

  • destination (Union[int, Sequence[int]]) – Destination positions for each of the original axes. These must also be unique.

  • out (Optional[Union[Array, NativeArray]]) – optional output array, for writing the result to. (default: None)

Return type

Union[Array, NativeArray]

Returns

ret – Array with moved axes. This array is a view of the input array.

Examples

With ivy.Array input: >>> x = ivy.zeros((3, 4, 5)) >>> ivy.moveaxis(x, 0, -1).shape (4, 5, 3) >>> ivy.moveaxis(x, -1, 0).shape (5, 3, 4)

ivy.msort(a, /, *, out=None)[source]

Return a copy of an array sorted along the first axis.

Parameters
  • a (Union[Array, NativeArray, list, tuple]) – array-like input.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – sorted array of the same type and shape as a

Examples

>>> a = ivy.randint(10, size=(2,3))
>>> ivy.msort(a)
ivy.array(
    [[6, 2, 6],
     [8, 9, 6]]
    )
ivy.nan_to_num(x, /, *, copy=True, nan=0.0, posinf=None, neginf=None, out=None)[source]

Replace NaN with zero and infinity with large finite numbers (default behaviour) or with the numbers defined by the user using the nan, posinf and/or neginf keywords.

Parameters
  • x (Union[Array, NativeArray]) – Array input.

  • copy (Optional[bool]) – Whether to create a copy of x (True) or to replace values in-place (False). (default: True) The in-place operation only occurs if casting to an array does not require a copy. Default is True.

  • nan (Optional[Union[float, int]]) – Value to be used to fill NaN values. If no value is passed then NaN values (default: 0.0) will be replaced with 0.0.

  • posinf (Optional[Union[int, float]]) – Value to be used to fill positive infinity values. If no value is passed (default: None) then positive infinity values will be replaced with a very large number.

  • neginf (Optional[Union[int, float]]) – Value to be used to fill negative infinity values. (default: None) If no value is passed then negative infinity values will be replaced with a very small (or negative) number.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – Array with the non-finite values replaced. If copy is False, this may be x itself.

Examples

>>> x = ivy.array([1, 2, 3, nan])
>>> ivy.nan_to_num(x)
ivy.array([1.,    1.,   3.,   0.0])
>>> x = ivy.array([1, 2, 3, inf])
>>> ivy.nan_to_num(x, posinf=5e+100)
ivy.array([1.,   2.,   3.,   5e+100])
ivy.nanmean(a, /, *, axis=None, keepdims=False, dtype=None, out=None)[source]

Computes the mean of all non-NaN elements along the specified dimensions.

Parameters
  • a (Array) – Input array.

  • axis (Optional[Union[Tuple[int], int]]) – Axis or axes along which the means are computed. (default: None) The default is to compute the mean of the flattened array.

  • keepdims (Optional[bool]) – If this is set to True, the axes which are reduced are left in the result (default: False) as dimensions with size one. With this option, the result will broadcast correctly against the original a. If the value is anything but the default, then keepdims will be passed through to the mean or sum methods of sub-classes of ndarray. If the sub-classes methods does not implement keepdims any exceptions will be raised.

  • dtype (Optional[Union[Dtype, NativeDtype]]) – The desired data type of returned tensor. Default is None. (default: None)

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – The nanmean of the array elements.

Functional Examples

>>> a = ivy.array([[1, ivy.nan], [3, 4]])
>>> ivy.nanmean(a)
2.6666666666666665
>>> ivy.nanmean(a, axis=0)
ivy.array([2.,  4.])
ivy.nansum(x, /, *, axis=None, dtype=None, keepdims=False, out=None)[source]

Return the sum of array elements over a given axis treating Not a Numbers (NaNs) as zero.

Parameters
  • x (Union[Array, NativeArray]) – Input array.

  • axis (Optional[Union[tuple, int]]) – Axis or axes along which the sum is computed. (default: None) The default is to compute the sum of the flattened array.

  • dtype (Optional[Union[Dtype, NativeDtype]]) – The type of the returned array and of the accumulator in (default: None) which the elements are summed. By default, the dtype of input is used.

  • keepdims (Optional[bool]) – If this is set to True, the axes which are reduced are left (default: False) in the result as dimensions with size one.

  • out (Optional[Array]) – Alternate output array in which to place the result. (default: None) The default is None.

Return type

Array

Returns

ret – A new array holding the result is returned unless out is specified, in which it is returned.

Examples

>>> a = ivy.array([[ 2.1,  3.4,  ivy.nan], [ivy.nan, 2.4, 2.1]])
>>> ivy.nansum(a)
10.0
>>> ivy.nansum(a, axis=0)
ivy.array([2.1, 5.8, 2.1])
>>> ivy.nansum(a, axis=1)
ivy.array([5.5, 4.5])
ivy.native_sparse_array(data=None, *, coo_indices=None, csr_crow_indices=None, csr_col_indices=None, values=None, dense_shape=None)[source]
ivy.native_sparse_array_to_indices_values_and_shape(x)[source]
ivy.ndenumerate(input)[source]

Multidimensional index iterator.

Parameters

input (Iterable) – Input array to iterate over.

Return type

Generator

Returns

ret – An iterator yielding pairs of array coordinates and values.

Examples

>>> a = ivy.array([[1, 2], [3, 4]])
>>> for index, x in ivy.ndenumerate(a):
>>>     print(index, x)
(0, 0) 1
(0, 1) 2
(1, 0) 3
(1, 1) 4
ivy.ndindex(shape)[source]

Multidimensional index iterator.

Parameters

shape (Tuple) – The shape of the array to iterate over.

Return type

Generator

Returns

ret – An iterator yielding array coordinates.

Examples

>>> a = ivy.array([[1, 2], [3, 4]])
>>> for index in ivy.ndindex(a):
>>>     print(index)
(0, 0)
(0, 1)
(1, 0)
(1, 1)
ivy.nextafter(x1, x2, /, *, out=None)[source]

Return the next floating-point value after x1 towards x2, element-wise.

Parameters
  • x1 (Union[Array, NativeArray]) – First input array.

  • x2 (Union[Array, NativeArray]) – Second input array.

  • out (Optional[Array]) – Alternate output array in which to place the result. (default: None) The default is None.

Return type

bool

Returns

ret – The next representable values of x1 in the direction of x2.

Examples

>>> x1 = ivy.array([1.0e-50, 2.0e+50])
>>> x2 = ivy.array([2.0, 1.0])
>>> ivy.nextafter(x1, x2)
ivy.array([1.4013e-45., 3.4028e+38])
ivy.pad(input, pad_width, /, *, mode='constant', stat_length=None, constant_values=None, end_values=None, reflect_type='even', **kwargs)[source]

Pads an array.

Parameters
  • input (Union[Array, NativeArray]) – Input array to pad.

  • pad_width (Union[Iterable[Tuple[int]], int]) –

    Number of values padded to the edges of each axis.
    • ((before_1, after_1), … (before_N, after_N)) yields unique pad widths for each axis.

    • ((before, after),) yields same before and after pad for each axis.

    • pad (integer) is shortcut for before = after = pad width for all axes.

  • mode (Optional[Union[Literal[‘constant’, ‘edge’, ‘linear_ramp’, ‘maximum’, ‘mean’, ‘median’, ‘minimum’, ‘reflect’, ‘symmetric’, ‘wrap’, ‘empty’], Callable]]) –

    (default: 'constant') One of the following string values or a user-supplied function.

    • ”constant”: Pads with a constant value.

    • ”edge”: Pads with the input’s edge values.

    • ”linear_ramp”: Pads with the linear ramp between end_value and the input’s edge value.

    • ”maximum”: Pads with the maximum value of all or part of the vector along each axis.

    • ”mean”: Pads with the mean value of all or part of the vector along each axis.

    • ”median”: Pads with the median value of all or part of the vector along each axis.

    • ”minimum”: Pads with the minimum value of all or part of the vector along each axis.

    • ”reflect”: Pads with the reflection mirrored on the first and last values of the vector along each axis.

    • ”symmetric”: Pads with the reflection of the vector mirrored along the edge of the input.

    • ”wrap”: Pads with the wrap of the vector along the axis. The first values are used to pad the end and the end values are used to pad the beginning.

    • ”empty”: Pads with undefined values.

    • <function>: Pads with a user-defined padding function. The padding function should modify a rank 1 array following the signature padding_func(vector, iaxis_pad_width, iaxis, kwargs), where:

      • vector is a rank 1 array already padded with zeros. Padded values are vector[:iaxis_pad_width[0]] and vector[-iaxis_pad_width[1]:].

      • iaxis_pad_width is a 2-tuple of ints, where iaxis_pad_width[0] represents the number of values padded at the beginning of vector and iaxis_pad_width[1] represents the number of values padded at the end of vector.

      • iaxis is the axis currently being calculated.

      • kwargs is a dict of keyword arguments the function requires.

  • stat_length (Optional[Union[Iterable[Tuple[int]], int]]) –

    Used in “maximum”, “mean”, “median”, and “minimum”. Number of values at edge (default: None) of each axis used to calculate the statistic value.

    • ((before_1, after_1), … (before_N, after_N)) yields unique statistic lengths for each axis.

    • ((before, after),) yields same before and after statistic lengths for each axis.

    • stat_length (integer) is a shortcut for before = after = stat_length length for all axes.

    • None uses the entire axis.

  • constant_values (Optional[Union[Iterable[Tuple[Number]], Number]]) –

    (default: None) Used in “constant”. The values to set the padded values for each axis.

    • ((before_1, after_1), … (before_N, after_N)) yields unique pad constants for each axis.

    • ((before, after),) yields same before and after constants for each axis.

    • constant (integer) is a shortcut for before = after = constant for all axes.

  • end_values (Optional[Union[Iterable[Tuple[Number]], Number]]) –

    Used in “linear_ramp”. The values used for the ending value of the linear_ramp (default: None) and that will form the edge of the padded array.

    • ((before_1, after_1), … (before_N, after_N)) yields unique end values for each axis.

    • ((before, after),) yields same before and after end values for each axis

    • end (integer) is a shortcut for before = after = end for all axes.

  • reflect_type (Optional[Literal[‘even’, ‘odd’]]) – Used in “reflect”, and “symmetric”. The “even” style is the default with an (default: 'even') unaltered reflection around the edge value. For the “odd” style, the extended part of the array is created by subtracting the reflected values from two times the edge value.

Return type

Array

Returns

ret – Padded array of the same rank as the input but with shape increased according to pad_width.

Both the description and the type hints above assume an array input for simplicity, but this function is nestable, and therefore also accepts ivy.Container instances in place of any of the arguments.

Examples

With ivy.Array input:

>>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
>>> padding = ((1, 1), (2, 2))
>>> y = ivy.pad(x, padding, mode="constant")
>>> print(y)
ivy.array([[0, 0, 0, 0, 0, 0, 0],
           [0, 0, 1, 2, 3, 0, 0],
           [0, 0, 4, 5, 6, 0, 0],
           [0, 0, 0, 0, 0, 0, 0]])
>>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
>>> padding = ((1, 1), (2, 2))
>>> y = ivy.pad(x, padding, mode="reflect")
>>> print(y)
ivy.array([[6, 5, 4, 5, 6, 5, 4],
           [3, 2, 1, 2, 3, 2, 1],
           [6, 5, 4, 5, 6, 5, 4],
           [3, 2, 1, 2, 3, 2, 1]])
>>> x = ivy.array([[1, 2, 3], [4, 5, 6]])
>>> padding = ((1, 1), (2, 2))
>>> y = ivy.pad(x, padding, mode="symmetric")
>>> print(y)
ivy.array([[2, 1, 1, 2, 3, 3, 2],
           [2, 1, 1, 2, 3, 3, 2],
           [5, 4, 4, 5, 6, 6, 5],
           [5, 4, 4, 5, 6, 6, 5]])

With ivy.NativeArray input:

>>> x = ivy.native_array([[1, 2, 3], [4, 5, 6]])
>>> padding = ((1, 1), (2, 2))
>>> y = ivy.pad(x, padding, mode="constant", constant_values=7)
>>> print(y)
ivy.array([[7, 7, 7, 7, 7, 7, 7],
           [7, 7, 1, 2, 3, 7, 7],
           [7, 7, 4, 5, 6, 7, 7],
           [7, 7, 7, 7, 7, 7, 7]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0, 1, 2]), b=ivy.array([4, 5, 6]))
>>> padding = (1, 1)
>>> y = ivy.pad(x, padding, mode="constant")
>>> print(y)
{
    a: ivy.array([0, 0, 1, 2, 0]),
    b: ivy.array([0, 4, 5, 6, 0])
}
ivy.rot90(m, /, *, k=1, axes=(0, 1), out=None)[source]

Rotate an array by 90 degrees in the plane specified by axes. Rotation direction is from the first towards the second axis.

Parameters
  • m (Union[Array, NativeArray]) – Input array of two or more dimensions.

  • k (Optional[int]) – Number of times the array is rotated by 90 degrees. (default: 1)

  • axes (Optional[Tuple[int, int]]) – The array is rotated in the plane defined by the axes. Axes must be (default: (0, 1)) different.

  • out (Optional[Array]) – optional output container, for writing the result to. It must have a shape (default: None) that the inputs broadcast to.

Return type

Array

Returns

ret – A rotated view of m.

Examples

With ivy.Array input: >>> m = ivy.array([[1,2], [3,4]]) >>> ivy.rot90(m) ivy.array([[2, 4],

[1, 3]])

>>> m = ivy.array([[1,2], [3,4]])
>>> ivy.rot90(m, k=2)
ivy.array([[4, 3],
       [2, 1]])
>>> m = ivy.array([[[0, 1],                        [2, 3]],                       [[4, 5],                        [6, 7]]])
>>> ivy.rot90(m, k=2, axes=(1,2))
ivy.array([[[3, 2],
        [1, 0]],
[[7, 6],

[5, 4]]])

With ivy.NativeArray input: >>> m = ivy.native_array([[1,2], [3,4]]) >>> ivy.rot90(m) ivy.array([[2, 4],

[1, 3]])

>>> m = ivy.native_array([[1,2], [3,4]])
>>> ivy.rot90(m, k=2)
ivy.array([[4, 3],
       [2, 1]])
>>> m = ivy.native_array([[[0, 1],                               [2, 3]],                              [[4, 5],                               [6, 7]]])
>>> ivy.rot90(m, k=2, axes=(1,2))
ivy.array([[[3, 2],
        [1, 0]],
[[7, 6],

[5, 4]]])

ivy.signbit(x, /, *, out=None)[source]

Returns element-wise True where signbit is set (less than zero).

Parameters
  • x (Union[Array, NativeArray, float, int, list, tuple]) – Array-like input.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – Output array, or reference to out if that was supplied. This is a scalar if x is a scalar.

Examples

>>> x = ivy.array([1, -2, 3])
>>> ivy.signbit(x)
ivy.array([False, True, False])
ivy.sinc(x, /, *, out=None)[source]

Calculates an implementation-dependent approximation of the principal value of the normalized sinc function, having domain (-infinity, +infinity) and codomain [-0.217234, 1], for each element x_i of the input array x. Each element x_i is assumed to be expressed in radians.

Special cases

For floating-point operands,

  • If x_i is NaN, the result is NaN.

  • If x_i is 0, the result is 1.

  • If x_i is either +infinity or -infinity, the result is NaN.

Parameters
  • x (Union[Array, NativeArray]) – input array. Should have a floating-point data type.

  • out (Optional[Array]) – optional output array, for writing the result to. It must have a shape that the (default: None) inputs broadcast to.

Return type

Array

Returns

ret – an array containing the normalized sinc function of each element in x. The returned array must have a floating-point data type determined by type-promotion.

Examples

With ivy.Array input:

>>> x = ivy.array([0.5, 1.5, 2.5, 3.5])
>>> y = x.sinc()
>>> print(y)
ivy.array([0.637,-0.212,0.127,-0.0909])
>>> x = ivy.array([1.5, 0.5, -1.5])
>>> y = ivy.zeros(3)
>>> ivy.sinc(x, out=y)
>>> print(y)
ivy.array([-0.212,0.637,-0.212])

With ivy.NativeArray input:

>>> x = ivy.array([0.5, 1.5, 2.5, 3.5])
>>> y = ivy.sinc(x)
>>> print(y)
ivy.array([0.637,-0.212,0.127,-0.0909])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0.5, 1.5, 2.5]),
...                   b=ivy.array([3.5, 4.5, 5.5]))
>>> y = x.sinc()
>>> print(y)
{
    a: ivy.array([0.637,-0.212,0.127]),
    b: ivy.array([-0.0909,0.0707,-0.0579])
}
ivy.take_along_axis(arr, indices, axis, /, *, out=None)[source]

Take values from the input array by matching 1d index and data slices.

Parameters
  • arr (Union[Array, NativeArray]) – The source array.

  • indices (Union[Array, NativeArray]) – The indices of the values to extract.

  • axis (int) – The axis over which to select values.

  • out (Optional[Array]) – The output array. (default: None)

Return type

Array

Returns

ret – The returned array has the same shape as indices.

Examples

>>> arr = ivy.array([[4, 3, 5], [1, 2, 1]])
>>> indices = ivy.array([[0, 1, 1], [2, 0, 0]])
>>> y = ivy.take_along_axis(arr, indices, 1)
>>> print(y)
ivy.array([[4, 3, 3], [1, 1, 1]])
ivy.top_k(x, k, /, *, axis=None, largest=True, out=None)[source]

Returns the k largest elements of the given input array along a given axis.

Parameters
  • x (Union[Array, NativeArray]) – The array to compute top_k for.

  • k (int) – Number of top elements to retun must not exceed the array size.

  • axis (Optional[int]) – The axis along which we must return the top elements default value is 1. (default: None)

  • largest (Optional[bool]) – If largest is set to False we return k smallest elements of the array. (default: True)

  • out (Optional[tuple]) – Optional output tuple, for writing the result to. Must have two arrays inside, (default: None) with a shape that the returned tuple broadcast to.

Return type

Tuple[Array, NativeArray]

Returns

ret – A named tuple with values and indices of top k elements.

Examples

With ivy.Array input:

>>> x = ivy.array([2., 1., -3., 5., 9., 0., -4])
>>> y = ivy.top_k(x, 2)
>>> print(y)
top_k(values=ivy.array([9., 5.]), indices=ivy.array([4, 3]))
>>> x = ivy.array([[-2., 3., 4., 0.], [-8., 0., -1., 2.]])
>>> y = ivy.top_k(x, 2, axis=1, largest=False)
>>> print(y)
top_k(values=ivy.array([[-2.,  0.],[-8., -1.]]),
...   indices=ivy.array([[0, 3],[0, 2]]))

With ivy.NativeArray input:

>>> x = ivy.native_array([2., 1., -3., 5., 9., 0., -4])
>>> y = ivy.top_k(x, 3)
>>> print(y)
top_k(values=ivy.array([9., 5., 2.]), indices=ivy.array([4, 3, 0]))

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([-1, 2, -4]), b=ivy.array([4., 5., 0.]))
>>> y = ivy.top_k(2)
>>> print(y)
{
    a: [
        values = ivy.array([ 2, -1]),
        indices = ivy.array([1, 0])
    ],
    b: [
        values = ivy.array([5., 4.]),
        indices = ivy.array([1, 0])
    ]
}
ivy.trapz(y, /, *, x=None, dx=1.0, axis=-1, out=None)[source]

Integrate along the given axis using the composite trapezoidal rule. If x is provided, the integration happens in sequence along its elements - they are not sorted..

Parameters
  • y (Array) – The array that should be integrated.

  • x (Optional[Array]) – The sample points corresponding to the input array values. (default: None) If x is None, the sample points are assumed to be evenly spaced dx apart. The default is None.

  • dx (Optional[float]) – The spacing between sample points when x is None. The default is 1. (default: 1.0)

  • axis (Optional[int]) – The axis along which to integrate. (default: -1)

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – Definite integral of n-dimensional array as approximated along a single axis by the trapezoidal rule. If the input array is a 1-dimensional array, then the result is a float. If n is greater than 1, then the result is an n-1 dimensional array.

Examples

>>> y = ivy.array([1, 2, 3])
>>> ivy.trapz([1,2,3])
4.0
>>> y = ivy.array([1, 2, 3])
>>> ivy.trapz([1,2,3], x=[4, 6, 8])
8.0
>>> y = ivy.array([1, 2, 3])
>>> ivy.trapz([1,2,3], dx=2)
8.0
ivy.triu_indices(n_rows, n_cols=None, k=0, /, *, device=None)[source]

Returns the indices of the upper triangular part of a row by col matrix in a 2-by-N shape (tuple of two N dimensional arrays), where the first row contains row coordinates of all indices and the second row contains column coordinates. Indices are ordered based on rows and then columns. The upper triangular part of the matrix is defined as the elements on and above the diagonal. The argument k controls which diagonal to consider. If k = 0, all elements on and above the main diagonal are retained. A positive value excludes just as many diagonals above the main diagonal, and similarly a negative value includes just as many diagonals below the main diagonal. The main diagonal are the set of indices {(i,i)} for i∈[0,min{n_rows, n_cols}−1].

Notes

Primary purpose of this function is to slice an array of shape (n,m). See https://numpy.org/doc/stable/reference/generated/numpy.triu_indices.html for examples

Tensorflow does not support slicing 2-D tensor with tuple of tensor of indices

Parameters
  • n_rows (int) – number of rows in the 2-d matrix.

  • n_cols (Optional[int]) – number of columns in the 2-d matrix. If None n_cols will be the same as n_rows (default: None)

  • k (Optional[int]) – number of shifts from the main diagonal. k = 0 includes main diagonal, (default: 0) k > 0 moves upwards and k < 0 moves downwards

  • device (Optional[Union[Device, NativeDevice]]) – device on which to place the created array. Default: None. (default: None)

Return type

Tuple[Array]

Returns

  • ret – an 2xN shape, tuple of two N dimensional, where first subarray (i.e. ret[0]) contains row coordinates of all indices and the second subarray (i.e ret[1]) contains columns indices.

  • Function is nestable, and therefore also accepts ivy.Container

  • instances in place of any of the arguments.

Examples

>>> x = ivy.triu_indices(4,4,0)
>>> print(x)
(ivy.array([0, 0, 0, 0, 1, 1, 1, 2, 2, 3]),
ivy.array([0, 1, 2, 3, 1, 2, 3, 2, 3, 3]))
>>> x = ivy.triu_indices(4,4,1)
>>> print(x)
(ivy.array([0, 0, 0, 1, 1, 2]),
ivy.array([1, 2, 3, 2, 3, 3]))
>>> x = ivy.triu_indices(4,4,-2)
>>> print(x)
(ivy.array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3]),
ivy.array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 1, 2, 3]))
>>> x = ivy.triu_indices(4,2,0)
>>> print(x)
(ivy.array([0, 0, 1]),
ivy.array([0, 1, 1]))
>>> x = ivy.triu_indices(2,4,0)
>>> print(x)
(ivy.array([0, 0, 0, 0, 1, 1, 1]),
ivy.array([0, 1, 2, 3, 1, 2, 3]))
>>> x = ivy.triu_indices(4,-4,0)
>>> print(x)
(ivy.array([]), ivy.array([]))
>>> x = ivy.triu_indices(4,4,100)
>>> print(x)
(ivy.array([]), ivy.array([]))
>>> x = ivy.triu_indices(2,4,-100)
>>> print(x)
(ivy.array([0, 0, 0, 0, 1, 1, 1, 1]), ivy.array([0, 1, 2, 3, 0, 1, 2, 3]))
ivy.unravel_index(indices, shape, /, *, out=None)[source]

Converts a flat index or array of flat indices into a tuple of coordinate arrays.

Parameters
  • indices (Array) – Input array.

  • shape (Tuple[int]) – The shape of the array to use for unraveling indices.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – Tuple with arrays that have the same shape as the indices array.

Functional Examples

>>> indices = ivy.array([22, 41, 37])
>>> ivy.unravel_index(indices, (7,6))
(ivy.array([3, 6, 6]), ivy.array([4, 5, 1]))
ivy.vorbis_window(window_length, *, dtype=None, out=None)[source]

Returns an array that contains a vorbis power complementary window of size window_length.

Parameters
  • window_length (Union[Array, NativeArray]) – the length of the vorbis window.

  • dtype (Optional[Union[Dtype, NativeDtype]]) – data type of the returned array. By default float32. (default: None)

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – Input array with the vorbis window.

Examples

>>> ivy.vorbis_window(3)
ivy.array([0.38268346, 1. , 0.38268352])
>>> ivy.vorbis_window(5)
ivy.array([0.14943586, 0.8563191 , 1. , 0.8563191, 0.14943568])
ivy.vsplit(ary, indices_or_sections, /, *, out=None)[source]

Split an array into multiple sub-arrays along the 3rd axis.

Parameters
  • ary (Union[Array, NativeArray]) – Array input.

  • indices_or_sections (Union[int, Tuple[int]]) – If indices_or_sections is an integer n, the array is split into n sections. If the array is divisible by n along the 3rd axis, each section will be of equal size. If input is not divisible by n, the sizes of the first int(ary.size(0) % n) sections will have size int(ary.size(0) / n) + 1, and the rest will have size int(ary.size(0) / n). If indices_or_sections is a tuple of ints, then input is split at each of the indices in the tuple.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None)

Return type

Array

Returns

ret – input array split along the 3rd axis.

Examples

>>> ary = ivy.array(
    [[[0.,  1.],
      [2.,  3.]],
     [[4.,  5.],
      [6.,  7.]]]
    )
>>> ivy.vsplit(ary, 2)
[ivy.array([[[0., 1.], [2., 3.]]]), ivy.array([[[4., 5.], [6., 7.]]])])
ivy.vstack(arrays, /)[source]

Stack arrays in sequence vertically (row wise).

Parameters

arrays (Sequence[Array]) – Sequence of arrays to be stacked.

Return type

Array

Returns

ret – The array formed by stacking the given arrays.

Examples

>>> x = ivy.array([1, 2, 3])
>>> y = ivy.array([2, 3, 4])
>>> ivy.vstack((x, y))
ivy.array([[1, 2, 3],
       [2, 3, 4]])
>>> ivy.vstack((x, y, x, y))
ivy.array([[1, 2, 3],
           [2, 3, 4],
           [1, 2, 3],
           [2, 3, 4]])
>>> y = [ivy.array([[5, 6]]), ivy.array([[7, 8]])]
>>> print(ivy.vstack(y))
ivy.array([[5, 6],
           [7, 8]])
ivy.zeta(x, q, /, *, out=None)[source]

Compute the Hurwitz zeta function.

Parameters
  • x (Union[Array, NativeArray]) – First input array.

  • q (Union[Array, NativeArray]) – Second input array.

  • out (Optional[Array]) – Alternate output array in which to place the result. (default: None) The default is None.

Return type

bool

Returns

ret – Array with values computed from zeta function from input arrays’ values.

Examples

>>> x = ivy.array([5.0, 3.0])
>>> q = ivy.array([2.0])
>>> ivy.zeta(x, q)
ivy.array([0.0369, 0.2021])