Creation#

ivy.eye_like(x, /, *, k=0, dtype=None, device=None, out=None)[source]#

Return a 2D array filled with ones on the k diagonal and zeros elsewhere. having the same shape as the first and last dim of input array x. input array x should to be 2D.

Parameters:
  • x (Union[Array, NativeArray]) – input array from which to derive the output array shape.

  • k (int) – index of the diagonal. A positive value refers to an upper diagonal, a negative (default: 0) value to a lower diagonal, and 0 to the main diagonal. Default: 0.

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

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

  • 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 having the same shape as x and filled with ones in diagonal k and zeros elsewhere.

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 as a replacement to any of the arguments.

Functional Examples

With ivy.Array input:

>>> x1 = ivy.array([0, 1],[2, 3])
>>> y1 = ivy.eye_like(x1)
>>> print(y1)
ivy.array([[1., 0.],
           [0., 1.]])
>>> x1 = ivy.array([0, 1, 2],[3, 4, 5],[6, 7, 8])
>>> y1 = ivy.eye_like(x1, k=1)
>>> print(y1)
ivy.array([[0., 1., 0.],
           [0., 0., 1.],
           [0., 0., 0.]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([[3, 8],[0, 2]]), b=ivy.array([[0, 2], [8, 5]]))
>>> y = ivy.eye_like(x)
>>> print(y)
{
    a: ivy.array([[1., 0.],
                  [0., 1.]]),
    b: ivy.array([[1., 0.],
                  [0., 1.]])
}
ivy.hamming_window(window_length, /, *, periodic=True, alpha=0.54, beta=0.46, dtype=None, out=None)[source]#

Compute the Hamming window with window length window_length.

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

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

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

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

  • 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.

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(size, /, *, periodic=True, dtype=None, out=None)[source]#

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

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

  • periodic (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.indices(dimensions, dtype='int64', sparse=False)[source]#

Return an array representing the indices of a grid.

Parameters:
  • dimensions (Sequence) – The shape of the grid.

  • dtype (Union[Dtype, NativeDtype]) – The data type of the result. (default: 'int64')

  • sparse (bool) – Return a sparse representation of the grid instead of a dense representation. (default: False)

Return type:

Union[Array, Tuple[Array, ...]]

Returns:

ret – If sparse is False, returns one grid indices array of shape (len(dimensions),) + tuple(dimensions). If sparse is True, returns a tuple of arrays each of shape (1, …, 1, dimensions[i], 1, …, 1) with dimensions[i] in the ith place.

Examples

>>> ivy.indices((3, 2))
ivy.array([[[0 0]
            [1 1]
            [2 2]]
           [[0 1]
            [0 1]
            [0 1]]])
>>> ivy.indices((3, 2), sparse=True)
(ivy.array([[0], [1], [2]]), ivy.array([[0, 1]]))
ivy.kaiser_bessel_derived_window(window_length, beta=12.0, *, dtype=None, out=None)[source]#

Compute 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.

  • 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.00726415, 0.9999736 , 0.9999736 , 0.00726415])
>>> ivy.kaiser_bessel_derived_window(5, 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]#

Compute 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[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.

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.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.tril_indices(n_rows, n_cols=None, k=0, /, *, device=None)[source]#

Return the indices of the lower 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 lower triangular part of the matrix is defined as the elements on and below the diagonal. The argument k controls which diagonal to consider. If k = 0, all elements on and below the main diagonal are retained. A positive value excludes just as many diagonals below the main diagonal, and similarly a negative value includes just as many diagonals above 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.tril_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 (int) – number of shifts from the main diagonal. k = 0 includes main diagonal, (default: 0) k > 0 moves downward and k < 0 moves upward

  • 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.tril_indices(4,4,0)
>>> print(x)
(ivy.array([0, 1, 1, 2, 2, 2, 3, 3, 3, 3]),
ivy.array([0, 0, 1, 0, 1, 2, 0, 1, 2, 3]))
>>> x = ivy.tril_indices(4,4,1)
>>> print(x)
(ivy.array([0, 0, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]),
ivy.array([0, 1, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3]))
>>> x = ivy.tril_indices(4,4,-2)
>>> print(x)
(ivy.array([2, 3, 3]), ivy.array([0, 0, 1]))
>>> x = ivy.tril_indices(4,2,0)
>>> print(x)
(ivy.array([0, 1, 1, 2, 2, 3, 3]),
ivy.array([0, 0, 1, 0, 1, 0, 1]))
>>> x = ivy.tril_indices(2,4,0)
>>> print(x)
(ivy.array([0, 1, 1]), ivy.array([0, 0, 1]))
>>> x = ivy.tril_indices(4,-4,0)
>>> print(x)
(ivy.array([]), ivy.array([]))
>>> x = ivy.tril_indices(4,4,100)
>>> print(x)
(ivy.array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]),
ivy.array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]))
>>> x = ivy.tril_indices(2,4,-100)
>>> print(x)
(ivy.array([]), ivy.array([]))
ivy.vorbis_window(window_length, *, dtype=None, out=None)[source]#

Return 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])