General

Collection of general Ivy functions.

ivy.all_equal(*xs, equality_matrix=False)[source]

Determines whether the inputs are all equal.

Parameters
  • xs (Iterable[Any]) – inputs to compare.

  • equality_matrix (bool) – Whether to return a matrix of equalities comparing each input with every other. (default: False) Default is False.

Return type

Union[bool, Array, NativeArray]

Returns

ret – Boolean, whether or not the inputs are equal, or matrix array of booleans if equality_matrix=True is set.

Examples

With Number inputs:

>>> x1 = 1.2
>>> x2 = 1.0
>>> y = ivy.all_equal(x1, x2, equality_matrix=False)
>>> print(y)
False

With ivy.Array inputs:

>>> x1 = ivy.array([1, 1, 0, 0, 1, -1])
>>> x2 = ivy.array([1, 1, 0, 0, 1, -1])
>>> y = ivy.all_equal(x1, x2, equality_matrix=True)
>>> print(y)
ivy.array([[ True,  True], [ True,  True]])

With ivy.NativeArray inputs:

>>> x1 = ivy.native_array([1, 1, 0, 0, 1, -1])
>>> x2 = ivy.native_array([1, 1, 0, 0, 1, -1])
>>> y = ivy.all_equal(x1, x2, equality_matrix=False)
>>> print(y)
True

With one ivy.Container inputs:

>>> x1 = ivy.Container(a=ivy.native_array([0, 0, -1, 1, 0]),                             b=ivy.array([0, 0, -1, 1, 0]))
>>> x2 = ivy.array([0, 0, -1, 1, 0])
>>> y = ivy.all_equal(x1, x2, equality_matrix=False)
>>> print(y)
{
    a: true,
    b: true
}

With multiple ivy.Container inputs:

>>> x1 = ivy.Container(a=ivy.array([1, 0, 1, 1]),                             b=ivy.native_array([1, 0, 0, 1]))
>>> x2 = ivy.Container(a=ivy.native_array([1, 0, 1, 1]),                             b=ivy.array([1, 0, -1, -1]))
>>> y = ivy.all_equal(x1, x2, equality_matrix=False)
>>> print(y)
{
    a: true,
    b: false
}
ivy.arg_info(fn, *, name=None, idx=None)[source]

Return the index and inspect.Parameter representation of the specified argument. In the form of a dict with keys “idx” and “param”.

Parameters
  • fn (Callable) – The function to retrieve the argument information for

  • name (Optional[str]) – The name of the argument (default: None)

  • idx (Optional[int]) – the index of the argument in the inputs (default: None)

Returns

ret – a dict containing the idx, and the inspect.Parameter for the argument, which itself contains the parameter name, type, and other helpful information.

ivy.arg_names(receiver)[source]

Gets the expected keyword arguments for a function or class constructor.

Parameters

receiver – Function or class constructor

Returns

ret – List containing the keyword arguments’ names for a function or class constructor

Examples

>>> x = ivy.arg_names(ivy.tan)
>>> print(x)
['x', 'out']
>>> x = ivy.arg_names(ivy.optimizers.Adam)
>>> print(x)
['lr', 'beta1', 'beta2', 'epsilon', 'inplace',
'stop_gradients', 'compile_on_next_step', 'device']
ivy.array_equal(x0, x1)[source]

Determines whether two input arrays are equal across all elements.

Parameters
  • x0 (Union[Array, NativeArray]) – The first input array to compare.

  • x1 (Union[Array, NativeArray]) – The second input array to compare.

  • dtype – array data type

Return type

bool

Returns

ret – Boolean, whether or not the input arrays are equal across all elements.

Examples

>>> x = ivy.array([1,0,1])
>>> y = ivy.array([1,0,-1])
>>> z = ivy.array_equal(x,y)
>>> print(z)
False
>>> a = ivy.array([1, 2])
>>> b = ivy.array([1, 2])
>>> c = ivy.array_equal(a,b)
>>> print(c)
True
>>> i = ivy.array([1, 2])
>>> j = ivy.array([1, 2, 3])
>>> k = ivy.array_equal(i,j)
>>> print(k)
False
ivy.arrays_equal(xs)[source]

Determines whether input arrays are equal across all elements.

Parameters
  • xs (List[Union[Array, NativeArray]]) – Sequence of arrays to compare for equality

  • dtype – list data type

Return type

bool

Returns

ret – Boolean, whether or not all of the input arrays are equal across all elements.

Functional Examples

With ivy.Array input:

>>> i = ivy.array([1, 2])
>>> j = ivy.arrays_equal([i])
>>> print(j)
True
>>> x = ivy.array([0, 1, 2])
>>> y = ivy.array([1, 0, 2])
>>> z = ivy.array([0, 1, 2])
>>> w = ivy.arrays_equal([x, y, z])
>>> print(w)
False
>>> a = ivy.array([-1, 0, 1])
>>> b = ivy.array([-1, 0, 1])
>>> c = ivy.array([-1, 0, 1])
>>> d = ivy.arrays_equal([a, b, c])
>>> print(d)
True
>>> x = ivy.array([0.1, 1.1])
>>> y = ivy.array([0.1, 1.1, 2.1])
>>> z = ivy.array([0.1, 1.1])
>>> w = ivy.arrays_equal([x, y, z])
>>> print(w)
False

With ivy.NativeArray input:

>>> m = ivy.native_array([1.1, 0.2, 1.3])
>>> n = ivy.native_array([1.1, 0.2, 1.4])
>>> o = ivy.arrays_equal([m, n])
>>> print(o)
False
>>> a = ivy.native_array([1, 2, 3, 0, -1])
>>> b = ivy.array([1, 2, 3, 0, -1])
>>> c = ivy.arrays_equal([a,b])
>>> print(c)
True
>>> a = ivy.native_array([1, 2, 3, 0, -1])
>>> b = ivy.array([1, 2, 3, 0, -2])
>>> c = ivy.arrays_equal([a,b])
>>> print(c)
False

With ivy.Container input:

>>> r = ivy.Container(a=ivy.array([0., 1., 2.]), b=ivy.array([3., 4., 5.]))
>>> s = ivy.Container(a=ivy.array([0., 1., 2.]), b=ivy.array([3., 4., 5.]))
>>> t = ivy.Container(a=ivy.array([0., 1., 2.]), b=ivy.array([6., 7., 8.]))
>>> print(ivy.arrays_equal([r,s,t]))
{
    a: true,
    b: false
}
>>> x = ivy.Container(a=ivy.array([0, 1, 2]), b=ivy.array([3, 4, 5]))
>>> y = ivy.array([0,1,2])
>>> z = ivy.arrays_equal([x,y])
>>> print(z)
{
    a: true,
    b: false
}
ivy.assert_supports_inplace(x)[source]

Asserts that inplace operations are supported for x, else raises exception.

Parameters

x – Input variable or array to check for inplace support for.

Returns

ret – True if support, raises exception otherwise

ivy.cache_fn(func)[source]

Wrap a function, such that when cache=True is passed as an argument, a previously cached output is returned.

Parameters

func (Callable) – The function to wrap, whose output should be cached for later.

Return type

Callable

Returns

ret – The newly cache wrapped function.

ivy.clip_matrix_norm(x, max_norm, p=2.0, *, out=None)[source]

Clips (limits) the matrix norm of an array.

Parameters
  • x (Union[Array, NativeArray]) – Input array containing elements to clip.

  • max_norm (float) – The maximum value of the array norm.

  • p (float) – The p-value for computing the p-norm. Default is 2. (default: 2.0)

  • 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

Union[Array, NativeArray]

Returns

ret – An array with the matrix norm downscaled to the max norm if needed.

Functional Examples

With ivy.Array input:

>>> x = ivy.array([[0., 1., 2.]])
>>> y = ivy.clip_matrix_norm(x, 2.0)
>>> print(y)
ivy.array([[0.   , 0.894, 1.79 ]])
>>> x = ivy.array([[0.1, -1.2, 3.7], [0., 7.3, -0.5]])
>>> y = ivy.clip_matrix_norm(x, 3.0, 1.0)
>>> print(y)
ivy.array([[ 0.0353, -0.424 ,  1.31  ],
           [ 0.    ,  2.58  , -0.176 ]])
>>> x = ivy.array([[[5., 4.], [-2., 6.]],                        [[3., 7.], [0., -5.]]])
>>> y = ivy.empty((2, 2, 2))
>>> ivy.clip_matrix_norm(x, 0.5, 2.0, out=y)
>>> print(y)
ivy.array([[[ 0.339,  0.271],
            [-0.135,  0.406]],
           [[ 0.168,  0.391],
            [ 0.   , -0.279]]])
>>> x = ivy.array([[0., 1.],                        [2., 3.]])
>>> ivy.clip_matrix_norm(x, 5.0, 1.0, out=x)
>>> print(x)
ivy.array([[0., 1.],
           [2., 3.]])

With ivy.NativeArray input:

>>> x = ivy.native_array([[0., 1., 2.]])
>>> y = ivy.clip_matrix_norm(x, 2.0)
>>> print(y)
ivy.array([[0.   , 0.894, 1.79 ]])
>>> x = ivy.native_array([[0.1, -1.2, 3.7], [0., 7.3, -0.5]])
>>> y = ivy.clip_matrix_norm(x, 3.0, 1.0)
>>> print(y)
ivy.array([[ 0.0353, -0.424 ,  1.31  ],
           [ 0.    ,  2.58  , -0.176 ]])
>>> x = ivy.native_array([[[5., 4.], [-2., 6.]],                        [[3., 7.], [0., -5.]]])
>>> y = ivy.empty((2, 2, 2))
>>> ivy.clip_matrix_norm(x, 0.5, 2.0, out=y)
>>> print(y)
ivy.array([[[ 0.339,  0.271],
            [-0.135,  0.406]],
           [[ 0.168,  0.391],
            [ 0.   , -0.279]]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([[0., 1., 2.]]),                           b=ivy.array([[3., 4., 5.]]))
>>> y = ivy.clip_matrix_norm(x, 2.0)
>>> print(y)
{
    a: ivy.array([[0., 0.894, 1.79]]),
    b: ivy.array([[0.849, 1.13, 1.41]])
}
ivy.clip_vector_norm(x, max_norm, p=2.0, *, out=None)[source]

Clips (limits) the vector p-norm of an array.

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

  • max_norm (float) – float, the maximum value of the array norm.

  • p (float) – optional float, the p-value for computing the p-norm. Default is 2. (default: 2.0)

  • 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

Union[Array, NativeArray]

Returns

ret – An array with the vector norm downscaled to the max norm if needed.

Functional Examples

With ivy.Array input:

>>> x = ivy.array([0., 1., 2.])
>>> y = ivy.clip_vector_norm(x, 2.0)
>>> print(y)
ivy.array([0.   , 0.894, 1.79 ])
>>> x = ivy.array([0.5, -0.7, 2.4])
>>> y = ivy.clip_vector_norm(x, 3.0, 1.0)
>>> print(y)
ivy.array([ 0.417, -0.583,  2.   ])
>>> x = ivy.array([[[0., 0.], [1., 3.], [2., 6.]],                        [[3., 9.], [4., 12.], [5., 15.]]])
>>> y = ivy.zeros(((2, 3, 2)))
>>> ivy.clip_vector_norm(x, 4.0, 1.0, out=y)
>>> print(y)
ivy.array([[[0.    , 0.    ],
            [0.0667, 0.2   ],
            [0.133 , 0.4   ]],
           [[0.2   , 0.6   ],
            [0.267 , 0.8   ],
            [0.333 , 1.    ]]])
>>> x = ivy.array([[1.1, 2.2, 3.3],                        [-4.4, -5.5, -6.6]])
>>> ivy.clip_vector_norm(x, 1.0, 3.0, out=x)
>>> print(x)
ivy.array([[ 0.131,  0.263,  0.394],
           [-0.526, -0.657, -0.788]])

With ivy.NativeArray input:

>>> x = ivy.native_array([0., 1., 2.])
>>> y = ivy.clip_vector_norm(x, 2.0)
>>> print(y)
ivy.array([0.   , 0.894, 1.79 ])
>>> x = ivy.native_array([0.5, -0.7, 2.4])
>>> y = ivy.clip_vector_norm(x, 3.0, 1.0)
>>> print(y)
ivy.array([ 0.417, -0.583,  2.   ])
>>> x = ivy.native_array([[[0., 0.], [1., 3.], [2., 6.]],                               [[3., 9.], [4., 12.], [5., 15.]]])
>>> y = ivy.zeros(((2, 3, 2)))
>>> ivy.clip_vector_norm(x, 4.0, 1.0, out=y)
>>> print(y)
ivy.array([[[0.    , 0.    ],
            [0.0667, 0.2   ],
            [0.133 , 0.4   ]],
           [[0.2   , 0.6   ],
            [0.267 , 0.8   ],
            [0.333 , 1.    ]]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]),                           b=ivy.array([3., 4., 5.]))
>>> y = ivy.clip_vector_norm(x, 2.0)
>>> print(y)
{
    a: ivy.array([0., 0.894, 1.79]),
    b: ivy.array([0.849, 1.13, 1.41])
}
ivy.container_types()[source]

Summary.

Returns

ret – a key-value structure, and exposes public methods .keys(), .values() and items().

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

Copy an array.

Parameters
  • x (Union[Array, NativeArray]) – 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 – a copy of the input array x.

Examples

With one ivy.Array input:

>>> x = ivy.array([-1, 0, 1])
>>> y = ivy.copy_array(x)
>>> print(y)
ivy.array([-1, 0, 1])
>>> x = ivy.array([1, 0, 1, 1])
>>> y = ivy.copy_array(x)
>>> print(y)
ivy.array([1, 0, 1, 1])
>>> x = ivy.array([1, 0, 1, -1])
>>> y = ivy.zeros((1, 4))
>>> ivy.copy_array(x, out=y)
>>> print(y)
ivy.array([1, 0, 1, -1])
>>> x = ivy.array([1, 0, 1, 1])
>>> ivy.copy_array(x, out=x)
>>> print(x)
ivy.array([1, 0, 1, 1])

With one ivy.Container input:

>>> x = ivy.Container(a=ivy.array([-1, 0, 1]))
>>> y = ivy.copy_array(x)
>>> print(y)
{
    a: ivy.array([-1, 0, 1])
}
>>> x = ivy.Container(a=ivy.array([-1, 0, 1]),                          b=ivy.array([-1, 0, 1, 1, 1, 0]))
>>> y = ivy.copy_array(x)
>>> print(y)
{
    a: ivy.array([-1, 0, 1]),
    b: ivy.array([-1, 0, 1, 1, 1, 0])
}

With one ivy.Container static method:

>>> x = ivy.Container(a=ivy.array([-1, 0, 1]),                          b=ivy.array([-1, 0, 1, 1, 1, 0]))
>>> y = ivy.Container.static_copy_array(x)
>>> print(y)
{
    a: ivy.array([-1, 0, 1]),
    b: ivy.array([-1, 0, 1, 1, 1, 0])
}

With one ivy.Array instance method:

>>> x = ivy.array([-1, 0, 1])
>>> y = x.copy_array()
>>> print(y)
ivy.array([-1, 0, 1])
>>> x = ivy.array([1, 0, 1, 1])
>>> y = x.copy_array()
>>> print(y)
ivy.array([1, 0, 1, 1])

With ivy.Container instance method:

>>> x = ivy.Container(a=ivy.array([1, 0, 1]),                          b=ivy.array([-1, 0, 1, 1]))
>>> y = x.copy_array()
>>> print(y)
{
    a: ivy.array([1, 0, 1]),
    b: ivy.array([-1, 0, 1, 1])
}
ivy.cumprod(x, axis=0, exclusive=False, *, out=None)[source]

Returns the cumulative product of the elements along a given axis.

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

  • axis (int) – int , axis along which the cumulative product is computed. By default 0. (default: 0)

  • exclusive (Optional[bool]) – optional bool, Whether to perform the cumprod exclusively. Defaults is False. (default: False)

  • 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

Union[Array, NativeArray]

Returns

ret – Input array with cumulatively multiplied elements along axis.

Functional Examples

With ivy.Array input:

>>> x = ivy.array([2, 3, 4])
>>> y = ivy.cumprod(x)
>>> print(y)
ivy.array([2, 6, 24])
>>> x = ivy.array([2, 3, 4])
>>> exclusive = True
>>> y = ivy.cumprod(x, exclusive=exclusive)
>>> print(y)
ivy.array([1, 2, 6])

Example specifying axes

>>> x = ivy.array([[2, 3],                        [5, 7],                        [11, 13]])
>>> exclusive = True
>>> y = ivy.zeros((3, 2))
>>> ivy.cumprod(x, axis=1, exclusive=exclusive, out=y)
>>> print(y)
ivy.array([[1.,2.],[1.,5.],[1.,11.]])
>>> x = ivy.array([[2, 3],[5, 7],[11, 13]])
>>> exclusive = True
>>> ivy.cumprod(x, axis=0, exclusive=exclusive, out=x)
>>> print(x)
ivy.array([[1,  1],
           [2,  3],
           [10, 21]])

With ivy.NativeArray input:

>>> x = ivy.native_array([2, 3, 4])
>>> y = ivy.cumprod(x)
>>> print(y)
ivy.array([2, 6, 24])

With ivy.Container input: >>> x = ivy.Container(a=ivy.array([2, 3, 4]), b=ivy.array([3, 4, 5])) >>> y = ivy.cumprod(x) >>> print(y) {

a: ivy.array([2, 6, 24]), b: ivy.array([3, 12, 60])

}

ivy.cumsum(x, axis=0, *, out=None)[source]

Returns the cumulative sum of the elements along a given axis.

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

  • axis (int) – int, Axis along which the cumulative sum is computed. By default 0. (default: 0)

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

Return type

Union[Array, NativeArray]

Returns

ret – Input array with cumulatively summed elements along axis

ivy.current_backend_str()[source]

Summary.

Return type

Optional[str]

Returns

ret – The framework string.

ivy.default(x, default_val, catch_exceptions=False, rev=False, with_callable=False)[source]

Returns x provided it exists (is not None), else returns default value.

Parameters
  • x (Any) – Input which may or may not exist (be None).

  • default_val (Any) – The default value.

  • catch_exceptions (bool) – Whether to catch exceptions from callable x. Default is False. (default: False)

  • rev (bool) – Whether to reverse the input x and default_val. Default is False. (default: False)

  • with_callable (bool) – Whether either of the arguments might be callable functions. Default is False. (default: False)

Return type

Any

Returns

ret – x if x exists (is not None), else default.

ivy.einops_rearrange(x, pattern, *, out=None, **axes_lengths)[source]

Perform einops rearrange operation on input array x.

Parameters
  • x (Union[Array, NativeArray]) – Input array to be re-arranged.

  • pattern (str) – Rearrangement pattern.

  • axes_lengths (Dict[str, int]) – Any additional specifications for dimensions.

  • 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 – New array with einops.rearrange having been applied.

ivy.einops_reduce(x, pattern, reduction, *, out=None, **axes_lengths)[source]

Perform einops reduce operation on input array x.

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

  • pattern (str) – Reduction pattern.

  • reduction (Union[str, Callable]) – One of available reductions (‘min’, ‘max’, ‘sum’, ‘mean’, ‘prod’), or callable.

  • axes_lengths (Dict[str, int]) – Any additional specifications for dimensions.

  • 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 – New array with einops.reduce having been applied.

ivy.einops_repeat(x, pattern, *, out=None, **axes_lengths)[source]

Perform einops repeat operation on input array x.

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

  • pattern (str) – Rearrangement pattern.

  • axes_lengths (Dict[str, int]) – Any additional specifications for dimensions.

  • 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

Union[Array, NativeArray]

Returns

ret – New array with einops.repeat having been applied.

ivy.exists(x)[source]

Simple check as to whether the input is None or not.

Parameters

x (Any) – Input to check.

Return type

bool

Returns

ret – True if x is not None, else False.

Examples

With Any input:

>>> x = None
>>> y = ivy.exists(x)
>>> print(y)
False
>>> x = ""
>>> y = ivy.exists(x)
>>> print(y)
True
>>> x = []
>>> y = ivy.exists(x)
>>> print(y)
True
>>> x = 1
>>> y = ivy.exists(x)
>>> print(y)
True
>>> x = "abc"
>>> y = ivy.exists(x)
>>> print(y)
True
>>> x = [1, 0, -1, 1]
>>> y = ivy.exists(x)
>>> print(y)
True
>>> x = ivy.native_array([1, 2, 3, 1.2])
>>> y = ivy.exists(x)
>>> print(y)
True
>>> x = ivy.array([1, 2, 3, 1.2])
>>> y = ivy.exists(x)
>>> print(y)
True

With a mix of ivy.Container and Any input:

>>> x = ivy.Container(a=None, b=None)
>>> y = ivy.exists(x)
>>> print(y)
True
>>> x = ivy.Container(a=None, b="")
>>> y = ivy.exists(x)
>>> print(y)
True
>>> x = ivy.Container(a=123, b="")
>>> y = ivy.exists(x)
>>> print(y)
True
>>> x = ivy.Container(a=ivy.array([1, 2, 3]), b=ivy.native_array([1, 0, 1.2]))
>>> y = ivy.exists(x)
>>> print(y)
True
ivy.floormod(x, y, *, out=None)[source]

Returns element-wise remainder of division.

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

  • y (Union[Array, NativeArray]) – array, denominator input for floormod.

  • 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

Union[Array, NativeArray]

Returns

ret – An array of the same shape and type as x, with the elements floor modded.

ivy.fourier_encode(x, max_freq, num_bands=4, linear=False, concat=True, flatten=False)[source]

Pads an array with fourier encodings.

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

  • max_freq (Union[float, Array, NativeArray]) – The maximum frequency of the encoding.

  • num_bands (int) – The number of frequency bands for the encoding. Default is 4. (default: 4)

  • linear (bool) – Whether to space the frequency bands linearly as opposed to geometrically. (default: False) Default is False.

  • concat (bool) – Whether to concatenate the position, sin and cos values, or return seperately. (default: True) Default is True.

  • flatten (bool) – Whether to flatten the position dimension into the batch dimension. Default is (default: False) False.

Return type

Union[Array, NativeArray, Tuple]

Returns

ret – New array with the final dimension expanded, and the encodings stored in this channel.

ivy.function_supported_devices_and_dtypes(fn)[source]
Returns the supported combination of devices and dtypes

of the current backend’s function.

Parameters

fn (Callable) – The function to check for the supported device and dtype attribute

Return type

Dict

Returns

ret – The unsupported devices of the function

ivy.function_unsupported_devices_and_dtypes(fn)[source]
Returns the unsupported combination of devices and dtypes

of the current backend’s function.

Parameters

fn (Callable) – The function to check for the unsupported device and dtype attribute

Return type

Dict

Returns

ret – The unsupported combination of devices and dtypes of the function

ivy.gather(params, indices, axis=-1, *, out=None)[source]

Gather slices from params at axis according to indices.

Parameters
  • params (Union[Array, NativeArray]) – array, the array from which to gather values.

  • indices (Union[Array, NativeArray]) – array, index array.

  • axis (int) – optional int, the axis from which to gather from. Default is -1. (default: -1)

  • device – optional ivy.Device, device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. Same as x if None.

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

Return type

Union[Array, NativeArray]

Returns

  • ret – New array with the values gathered at the specified indices along the specified axis.

  • 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 input:

>>> x = ivy.array([0., 1., 2.])
>>> y = ivy.array([0, 1])
>>> print(ivy.gather(x, y))
ivy.array([0., 1.])
>>> x = ivy.array([[0., 1., 2.],                         [3., 4., 5.]])
>>> y = ivy.array([[0, 1],                         [1, 2]])
>>> z = ivy.array([[0., 0.],                         [0., 0.]])
>>> ivy.gather(x, y, out=z)
>>> print(z)
ivy.array([[0., 1.],
           [4., 5.]])
>>> x = ivy.array([[[0., 1.], [2., 3.]],                         [[4., 5.], [6., 7.]],                         [[8., 9.], [10., 11.]]])
>>> y = ivy.array([[[0, 1]],                         [[1, 2]],                         [[2, 0]]])
>>> ivy.gather(x, y, axis=0, out=x)
>>> print(x)
ivy.array([[[0.,5.]],[[4.,9.]],[[8.,1.]]])

With ivy.NativeArray input:

>>> x = ivy.native_array([0., 1., 2.])
>>> y = ivy.native_array([0, 1])
>>> print(ivy.gather(x, y))
ivy.array([0., 1.])

With a mix of ivy.Array and ivy.NativeArray inputs:

>>> x = ivy.native_array([0., 1., 2.])
>>> y = ivy.array([0, 1])
>>> print(ivy.gather(x, y))
ivy.array([0., 1.])

With a mix of ivy.Array and ivy.Container inputs:

>>> x = ivy.Container(a = ivy.array([0., 1., 2.]),                           b = ivy.array([4., 5., 6.]))
>>> y = ivy.array([0, 1])
>>> print(ivy.gather(x, y))
{
    a: ivy.array([0., 1.]),
    b: ivy.array([4., 5.])
}

With ivy.Container input:

>>> x = ivy.Container(a = ivy.array([0., 1., 2.]),                           b = ivy.array([4., 5., 6.]))
>>> y = ivy.Container(a = ivy.array([0, 1]),                           b = ivy.array([1, 2]))
>>> print(ivy.gather(x, y))
{
    a: ivy.array([0., 1.]),
    b: ivy.array([5., 6.])
}

Instance Method Examples

Using ivy.Array instance method:

>>> x = ivy.array([0., 1., 2.])
>>> y = ivy.array([0, 1])
>>> print(x.gather(y))
ivy.array([0., 1.])

Using ivy.Container instance method:

>>> x = ivy.Container(a = ivy.array([0., 1., 2.]),                           b = ivy.array([4., 5., 6.]))
>>> y = ivy.Container(a = ivy.array([0, 1]),                           b = ivy.array([1, 2]))
>>> print(x.gather(y))
{
    a: ivy.array([0., 1.]),
    b: ivy.array([5., 6.])
}
ivy.gather_nd(params, indices, *, out=None)[source]

Gather slices from params into a array with shape specified by indices.

Parameters
  • params (Union[Array, NativeArray]) – The array from which to gather values.

  • indices (Union[Array, NativeArray]) – Index array.

  • device – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. Same as x if 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

Union[Array, NativeArray]

Returns

ret – New array of given shape, with the values gathered at the indices.

Examples

With ivy.Array input:

>>> x = ivy.array([0., 1., 2., 3., 4., 5., 6.])
>>> y = ivy.array([1])
>>> print(ivy.gather_nd(x, y))
ivy.array(1.)

With ivy.NativeArray input:

>>> x = ivy.native_array([0., 1., 2.])
>>> y = ivy.native_array([1])
>>> print(ivy.gather_nd(x, y))
ivy.array(1.)

With a mix of ivy.Array and ivy.NativeArray inputs:

>>> x = ivy.native_array([0., 1., 2.])
>>> y = ivy.array([1])
>>> print(ivy.gather_nd(x, y))
ivy.array(1.)

With a mix of ivy.Array and ivy.Container inputs:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]),                           b=ivy.array([4., 5., 6.]))
>>> y = ivy.array([1])
>>> print(ivy.gather_nd(x, y))
{
    a: ivy.array(1.),
    b: ivy.array(5.)
}

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]),                           b=ivy.array([4., 5., 6.]))
>>> y = ivy.Container(a=ivy.array([0]),                           b=ivy.array([2]))
>>> print(ivy.gather_nd(x, y))
{
    a: ivy.array(0.),
    b: ivy.array(6.)
}
ivy.get_all_arrays_in_memory()[source]

Gets all arrays which are currently alive.

ivy.get_array_mode()[source]

Get the current state of array_mode

Examples

>>> ivy.get_array_mode()
True
>>> ivy.set_array_mode(False)
>>> ivy.get_array_mode()
False
Return type

bool

ivy.get_min_base()[source]

Gets the global minimum base used by ivy for numerically stable power raising.

Return type

float

Returns

ret – Global minimum base number

Examples

>>> x = ivy.get_min_base()
>>> print(x)
1e-05
ivy.get_min_denominator()[source]

Get the global minimum denominator used by ivy for numerically stable division.

Return type

float

Returns

ret – A float number of the global minimum denominator.

Examples

>>> x = ivy.get_min_denominator()
>>> print(x)
1e-12
ivy.get_nestable_mode()[source]

Get the current mode of whether to check if function inputs are ivy.Container. Default is True.

Examples

>>> ivy.get_nestable_mode()
True
>>> ivy.set_nestable_mode(False)
>>> ivy.get_nestable_mode()
False
Return type

bool

ivy.get_num_dims(x, as_array=False)[source]

Returns the number of dimensions of the array x.

Parameters
  • x (Union[Array, NativeArray]) – Input array to infer the number of dimensions for.

  • as_array (bool) – Whether to return the shape as a array, default False. (default: False)

Return type

int

Returns

ret – Shape of the array

ivy.get_referrers_recursive(item, depth=0, max_depth=None, seen_set=None, local_set=None)[source]

Summary.

Parameters
  • item

  • depth – (Default value = 0)

  • max_depth – (Default value = None)

  • seen_set – (Default value = None)

  • local_set – (Default value = None`)

ivy.has_nans(x, include_infs=True)[source]

Determine whether the array contains any nans, as well as infs or -infs if specified.

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

  • include_infs (bool) – Whether to include +infinity and -infinity in the check. Default is True. (default: True)

Return type

bool

Returns

ret – Boolean as to whether the array contains nans.

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.

Examples

With ivy.Array input:

>>> x = ivy.array([1, 2, 3])
>>> y = ivy.has_nans(x)
>>> print(y)
False
>>> x = ivy.array([float('nan'), 2, 3])
>>> y = ivy.has_nans(x)
>>> print(y)
True
>>> x = ivy.array([float('inf'), 2, 3])
>>> y = ivy.has_nans(x)
>>> print(y)
True
>>> x = ivy.array([float('inf'), 2, 3])
>>> y = ivy.has_nans(x, False)
>>> print(y)
False

With :code: ivy.NativeArray input:

>>> x = ivy.native_array([1, 2, 3, float('nan')])
>>> y = ivy.has_nans(x)
>>> print(y)
True

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]), b=ivy.array([3., 4., 5.]))
>>> y = ivy.has_nans(x)
>>> print(y)
{
    a: false,
    b: false
}

With one ivy.Container static method: >>> x = ivy.Container(a=ivy.array([-1, 0, 1]), b=ivy.array([-1, 0, 1, 1, 1, 0])) >>> y = ivy.Container.static_has_nans(x) >>> print(y) {

a: false, b: false

}

With one ivy.Array instance method:

>>> x = ivy.array([-1, 0, 1])
>>> y = x.has_nans()
>>> print(y)
False

With ivy.Container instance method: >>> x = ivy.Container(a=ivy.array([1, 0, 1]), b=ivy.array([-1, 0, 1, 1])) >>> y = x.has_nans() >>> print(y) {

a: false, b: false

}

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

Returns indices or true elements in an input boolean array.

Parameters
  • x (Union[Array, NativeArray]) – Boolean array, for which indices are desired.

  • 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

Union[Array, NativeArray]

Returns

ret – Indices for where the boolean array is True.

ivy.inplace_arrays_supported(f=None)[source]

Determine whether inplace arrays are supported for the current backend framework.

Parameters

f – (Default value = None)

Returns

ret – Boolean, whether or not inplace arrays are supported.

ivy.inplace_decrement(x, val)[source]

Perform in-place decrement for the input array.

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

  • val (Union[Array, NativeArray]) – The array to decrement the variable with.

Return type

Array

Returns

ret – The array following the in-place decrement.

ivy.inplace_increment(x, val)[source]

Perform in-place increment for the input array.

Parameters
  • x (Union[Array, NativeArray]) – The input array to be incremented by the defined value.

  • val (Union[Array, NativeArray]) – The value of increment.

Return type

Array

Returns

ret – The array following the in-place increment.

Examples

With ivy.Array input: >>> x = ivy.array([[5.3, 7., 0.], [6.8, 8, 3.9], [0., 10., 6.3]]) >>> y = ivy.inplace_increment(x, 3.) >>> print(y) ivy.array([[ 8.3, 10., 3.],

[ 9.8, 11., 6.9], [ 3., 13., 9.3]])

With ivy.NativeArray input: >>> x = ivy.native_array([10, 20, 30]) >>> val = ivy.native_array([1, 2, 3]) >>> y = ivy.inplace_increment(x, val) >>> print(y) ivy.array([11, 22, 33])

With ivy.Container input >>> x = ivy.Container(a=ivy.array([0., 15., 30.]), b=ivy.array([0., 25., 50.])) >>> y = ivy.inplace_increment(x, 2.5) >>> print(y) {

a: ivy.array([2.5, 17.5, 32.5]), b: ivy.array([2.5, 27.5, 52.5])

}

>>> x = ivy.Container(a=ivy.array([0., 15., 30.]), b=ivy.array([0., 25., 50.]))
>>> y = ivy.Container(a=ivy.array([0., 15., 30.]), b=ivy.array([0., 25., 50.]))
>>> z = ivy.inplace_increment(x, y)
>>> print(z)
{
    a: ivy.array([0., 30., 60.]),
    b: ivy.array([0., 50., 100.])
}
ivy.inplace_update(x, val, ensure_in_backend=False)[source]

Perform in-place update for the input array. This will always be performed on ivy.Array instances pass in the input, and will also be performed on the native array classes in the backend when the backend supports this. If the backend does not natively support inplace updates, and x is an ivy.NativeArray instance, then an exception will be thrown.

Parameters
  • x (Union[Array, NativeArray]) – The variable to update.

  • val (Union[Array, NativeArray]) – The array to update the variable with.

  • ensure_in_backend (bool) – Whether or not to ensure that the ivy.NativeArray is also inplace updated. (default: False) In cases where it should be, backends which do not natively support inplace updates will raise an exception.

Return type

Array

Returns

ret – The array following the in-place update.

ivy.inplace_variables_supported(f=None)[source]

Determine whether inplace variables are supported for the current backend framework.

Parameters

f – (Default value = None)

Returns

ret – Boolean, whether or not inplace variables are supported.

ivy.is_array(x, exclusive=False)[source]

Determines whether the input x is either an Ivy Array or a Native Array.

Parameters
  • x (Any) – The input to check

  • exclusive (bool) – Whether to check if the data type is exclusively an array, rather than a (default: False) variable or traced array.

Return type

bool

Returns

ret – Boolean, whether or not x is an array.

ivy.is_ivy_array(x, exclusive=False)[source]

Determines whether the input x is an Ivy Array.

Parameters
  • x (Union[Array, NativeArray]) – The input to check

  • exclusive (bool) – Whether to check if the data type is exclusively an array, rather than a (default: False) variable or traced array.

Return type

bool

Returns

ret – Boolean, whether or not x is an array.

Examples

>>> x = ivy.array([0, 1, 2])
>>> ivy.is_ivy_array(x)
True
>>> x = ivy.native_array([1.5, 2.3, 4.9, 2.6])
>>> ivy.is_ivy_array(x)
False
>>> x = ivy.native_array([-1, 2, 7, -3])
>>> ivy.is_ivy_array(x, False)
False
>>> x = ivy.native_array([9.1, -8.3, 2.8, 3.0])
>>> ivy.is_ivy_array(x, True)
False
>>> x = ivy.array([5, 2, 6, 9])
>>> ivy.is_ivy_array(x, True)
True
ivy.is_ivy_container(x)[source]

Determines whether the input x is an Ivy Container.

Parameters

x (Any) – The input to check

Return type

bool

Returns

ret – Boolean, whether or not x is an ivy container.

ivy.is_native_array(x, exclusive=False)[source]

Determines whether the input x is a Native Array.

Parameters
  • x (Union[Array, NativeArray]) – The input to check

  • exclusive (bool) – Whether to check if the data type is exclusively an array, rather than a (default: False) variable or traced array.

Return type

bool

Returns

ret – Boolean, whether or not x is a native array.

Examples

>>> x = ivy.array([0, 1, 2])
>>> ivy.is_native_array(x)
False
>>> x = ivy.native_array([1.5, 2.3, 4.9, 2.6])
>>> ivy.is_native_array(x)
True
>>> x = ivy.native_array([-1, 2, 7, -3])
>>> ivy.is_native_array(x, False)
True
>>> x = ivy.native_array([9.1, -8.3, 2.8, 3.0])
>>> ivy.is_native_array(x, True)
True
>>> x = ivy.array([5, 2, 6, 9])
>>> ivy.is_native_array(x, True)
False
ivy.match_kwargs(kwargs, *receivers, allow_duplicates=False)[source]

Match keyword arguments to either class or function receivers.

Parameters
  • kwargs – Keyword arguments to match.

  • receivers – Functions and/or classes to match the keyword arguments to.

  • allow_duplicates – Whether to allow one keyword argument to be used for multiple receivers. Default is False.

Returns

ret – Sequence of keyword arguments split as best as possible.

ivy.multiprocessing(context=None)[source]

Return backend-specific multiprocessing module.

Parameters

context (Optional[str]) – The context of the multiprocessing, either fork, forkserver or spawn. (default: None) Default is None.

Returns

ret – Multiprocessing module

ivy.num_arrays_in_memory()[source]

Returns the number of arrays which are currently alive.

ivy.one_hot(indices, depth, *, device=None, out=None)[source]

Returns a one-hot array.

Parameters
  • indices (Union[Array, NativeArray]) – Indices for where the ones should be scattered [batch_shape, dim]

  • depth (int) – Scalar defining the depth of the one-hot dimension.

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

  • 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

Union[Array, NativeArray]

Returns

ret – Tensor of zeros with the same shape and type as a, unless dtype provided which overrides.

ivy.print_all_arrays_in_memory()[source]

Prints all arrays which are currently alive.

ivy.queue_timeout()[source]

Get the global queue timeout values (in seconds).

Default value without this function being called is 10 seconds.

ivy.scatter_flat(indices, updates, size=None, tensor=None, reduction='sum', *, out=None)[source]

Scatter flat updates into a new flat array according to flat indices.

Parameters
  • indices (Union[Array, NativeArray]) – Indices for the new values to occupy.

  • updates (Union[Array, NativeArray]) – Values for the new array to hold.

  • size (Optional[int]) – The size of the result. (default: None)

  • tensor (Optional[Union[Array, NativeArray]]) – The tensor in which to scatter the results, default is None, in which case the (default: None) size is used to scatter into a zeros array.

  • reduction (str) – The reduction method for the scatter, one of ‘sum’, ‘min’, ‘max’ or ‘replace’ (default: 'sum')

  • device – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. Same as updates if None.

  • 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

Union[Array, NativeArray]

Returns

ret – New array of given shape, with the values scattered at the indices.

ivy.scatter_nd(indices, updates, shape=None, tensor=None, reduction='sum', *, out=None)[source]

Scatter updates into a new array according to indices.

Parameters
  • indices (Union[Array, NativeArray]) – Indices for the new values to occupy.

  • updates (Union[Array, NativeArray]) – Values for the new array to hold.

  • shape (Optional[Union[Shape, NativeShape]]) – The shape of the result. Default is None, in which case tensor argument must be (default: None) provided.

  • tensor (Optional[Union[Array, NativeArray]]) – The tensor in which to scatter the results, default is None, in which case the (default: None) shape arg is used to scatter into a zeros array.

  • reduction (str) – The reduction method for the scatter, one of ‘sum’, ‘min’, ‘max’ or ‘replace’ (default: 'sum')

  • device – device on which to create the array ‘cuda:0’, ‘cuda:1’, ‘cpu’ etc. Same as updates if None.

  • 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

Union[Array, NativeArray]

Returns

ret – New array of given shape, with the values scattered at the indices.

ivy.set_array_mode(mode)[source]

Set the mode of whether to convert inputs to ivy.NativeArray, then convert outputs back to ivy.Array

mode

boolean whether to perform ivy.Array conversions

Examples

>>> ivy.set_array_mode(False)
>>> ivy.get_array_mode()
False
>>> ivy.set_array_mode(True)
>>> ivy.get_array_mode()
True
Return type

None

ivy.set_min_base(val)[source]

Set the global minimum base used by ivy for numerically stable power raising.

Parameters

val (float) – The new value to set the minimum base to.

Return type

None

ivy.set_min_denominator(val)[source]

Set the global minimum denominator used by ivy for numerically stable division.

Parameters

val (float) – The new value to set the minimum denominator to.

Return type

None

ivy.set_nestable_mode(mode)[source]

Set the mode of whether to check if function inputs are ivy.Container

mode

boolean whether to check if function inputs are ivy.Container

Examples

>>> ivy.set_nestable_mode(False)
>>> ivy.get_nestable_mode()
False
>>> ivy.set_nestable_mode(True)
>>> ivy.get_nestable_mode()
True
Return type

None

ivy.set_queue_timeout(timeout)[source]

Set the global queue timeout value (in seconds) Default value without this function being called is 15 seconds.

Parameters

timeout – The timeout when waiting for containers to arrive from the queues. To be set in seconds.

Examples

>> x = ivy.queue_timeout() >> print(x) 15.0

To set the timeout for example 30 seconds

>> ivy.set_queue_timeout(30) >> y = ivy.queue_timeout() >> print(y) 30

ivy.set_shape_array_mode(mode)[source]

Set the mode of returning shape as ivy.Array to the given mode instance

mode

boolean whether to return shape as ivy.Array

Examples

>>> ivy.set_shape_array_mode(False)
>>> ivy.shape_array_mode()
False
>>> ivy.set_shape_array_mode(True)
>>> ivy.shape_array_mode()
True
Return type

None

ivy.set_tmp_dir(tmp_dr)[source]

Set the directory for saving temporary files.

Parameters

tmp_dr

ivy.shape(x, as_array=False)[source]

Returns the shape of the array x.

Parameters
  • x (Union[Array, NativeArray]) – Input array to infer the shape of.

  • as_array (bool) – Whether to return the shape as a array, default False. (default: False)

Return type

Union[Shape, NativeShape]

Returns

ret – Shape of the array x.

Examples

>>> x = ivy.array([[-1, 0, 1],[1, 0, -1]])
>>> y = ivy.shape(x)
>>> z = ivy.shape(x, as_array = True)
>>> print(y)
(2, 3)
>>> print(z)
ivy.array([2, 3])
ivy.shape_array_mode()[source]

Get the current state of shape_array_mode

Examples

>>> ivy.shape_array_mode()
False
>>> ivy.set_shape_array_mode(True)
>>> ivy.shape_array_mode()
True
Return type

bool

ivy.stable_divide(numerator, denominator, min_denominator=None)[source]

Divide the numerator by the denominator, with min denominator added to the denominator for numerical stability.

Parameters
  • numerator (Union[Number, Array, NativeArray, Container]) – The numerator of the division.

  • denominator (Union[Number, Array, NativeArray, Container]) – The denominator of the division.

  • min_denominator (Optional[Union[Number, Array, NativeArray, Container]]) – The minimum denominator to use, use global ivy._MIN_DENOMINATOR by default. (default: None)

Return type

Union[Number, Array, NativeArray, Container]

Returns

ret – The new item following the numerically stable division.

Examples

With int input: >>> x = ivy.stable_divide(1, 2) >>> print(x) 0.49999999999975

>>> x = ivy.stable_divide(1, 4, min_denominator=1)
>>> print(x)
0.2

With float input: >>> x = ivy.stable_divide(5.0, 3.33) >>> print(x) 1.5015015015010504

With complex input: >>> x = ivy.stable_divide(1+1j, 1-1j) >>> print(x) (5.000444502911705e-13+0.9999999999995j)

With ivy.Array input: >>> x = ivy.asarray([[10., 20., 30.], [40., 50., 60.]]) >>> y = ivy.stable_divide(x, 10.) >>> print(y) ivy.array([[1., 2., 3.],

[4., 5., 6.]])

>>> x = ivy.asarray([1,2,3])
>>> y = np.array((1., 3., 5.))
>>> z = ivy.stable_divide(x, y)
>>> print(z)
ivy.array([1.   , 0.667, 0.6  ])
>>> x = ivy.asarray([1., 2., 4.])
>>> y = ivy.asarray([1., 0.5, 0.25])
>>> z = ivy.asarray([0.01, 0.02, 0.03])
>>> w = ivy.stable_divide(x, y, min_denominator=z)
>>> print(w)
ivy.array([ 0.99,  3.85, 14.3 ])

With ivy.Container input >>> x = ivy.Container(a=ivy.asarray([10., 15.]), b=ivy.asarray([20., 25.])) >>> y = ivy.stable_divide(x, 0.5) >>> print(y) {

a: ivy.array([20., 30.]), b: ivy.array([40., 50.])

}

>>> x = ivy.Container(a=ivy.asarray([1., 2.]), b=ivy.asarray([3., 4.]))
>>> y = ivy.Container(a=ivy.asarray([0.5, 2.5]), b=ivy.asarray([3.5, 0.4]))
>>> z = ivy.stable_divide(x, y)
>>> print(z)
{
    a: ivy.array([2., 0.8]),
    b: ivy.array([0.857, 10.])
}
ivy.stable_pow(base, exponent, min_base=None)[source]

Raise the base by the power, with MIN_BASE added to the base when exponent > 1 for numerical stability.

Parameters
  • base (Any) – The numerator of the division.

  • exponent (Any) – The denominator of the division.

  • min_base (Optional[float]) – The minimum base to use, use global ivy._MIN_BASE by default. (default: None)

Return type

Any

Returns

ret – The new item following the numerically stable division.

ivy.supports_inplace(x)[source]

Determine whether inplace operations are supported for the data type of x.

Parameters

x – Input variable or array to check for inplace support for.

Returns

ret – Boolean, whether or not inplace operations are supported for x.

ivy.tmp_dir()[source]

Get the path for directory that saves temporary files.

Returns

ret – The path of directory that saves temporary files.

ivy.to_ivy_shape(shape)[source]

Returns the input shape in ivy.Shape form

Parameters

shape (Union[Shape, NativeShape]) – The input to be converted

Return type

Shape

Returns

ret – the input in ivy.Shape form

ivy.to_list(x)[source]

Creates a (possibly nested) list from input array.

Parameters

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

Return type

List

Returns

ret – A list representation of the input array x.

Functional Examples

With ivy.Array input:

>>> x = ivy.array([-1, 0, 1])
>>> y = ivy.to_list(x)
>>> print(y)
[-1, 0, 1]
>>> print(isinstance(y, list))
True
>>> x = ivy.array([[ 1.1,  2.2,  3.3],                        [-4.4, -5.5, -6.6]])
>>> y = ivy.to_list(x)
>>> print(y)
[[1.100000023841858,2.200000047683716,3.299999952316284],[-4.400000095367432,-5.5,-6.599999904632568]]
>>> print(isinstance(y, list))
True
>>> x = ivy.array([[[-1,  0,  1],                        [ 1,  0, -1]],                        [[ 1, -1,  0],                         [ 1,  0, -1]]])
>>> y = ivy.to_list(x)
>>> print(y)
[[[-1, 0, 1], [1, 0, -1]], [[1, -1, 0], [1, 0, -1]]]
>>> print(isinstance(y, list))
True

With ivy.NativeArray input:

>>> x = ivy.native_array([-1, 0, 1])
>>> y = ivy.to_list(x)
>>> print(y)
[-1, 0, 1]
>>> print(isinstance(y, list))
True
>>> x = ivy.native_array([[-1, 0, 1],                               [-1, 0, 1],                               [ 1, 0, -1]])
>>> y = ivy.to_list(x)
>>> print(y)
[[-1, 0, 1], [-1, 0, 1], [1, 0, -1]]
>>> print(isinstance(y, list))
True
>>> x = ivy.native_array([[[-1, 0, 1],                                [1, 0, -1]],                               [[1, -1, 0],                                [1, 0, -1]]])
>>> y = ivy.to_list(x)
>>> print(y)
[[[-1, 0, 1], [1, 0, -1]], [[1, -1, 0], [1, 0, -1]]]
>>> print(isinstance(y, list))
True

With a mix of ivy.Container and ivy.Array input:

>>> x = ivy.Container(a=ivy.array([-1, 0, 1]))
>>> y = ivy.to_list(x)
>>> print(y)
{
    a: [-1, 0, 1]
}
>>> x = ivy.Container(a=ivy.array([[-1, 0, 1],                                        [-1, 0, 1],                                        [1, 0, -1]]))
>>> y = ivy.to_list(x)
>>> print(y)
{
    a: [[-1, 0, 1], [-1, 0, 1], [1,0,-1]]
}
>>> x =     ivy.Container(a=ivy.array([[[-1, 0, 1],[1, 0, -1]],[[1, -1, 0],[1, 0, -1]]]))
>>> y = ivy.to_list(x)
>>> print(y)
{
    a: [[[-1, 0, 1], [1, 0, -1]], [[1, -1, 0], [1, 0, -1]]]
}

With a mix of ivy.Container and ivy.NativeArray input:

>>> x = ivy.Container(a=ivy.native_array([-1, 0, 1]))
>>> y = ivy.to_list(x)
>>> print(y)
{
    a: [-1, 0, 1]
}
>>> x = ivy.Container(a=ivy.native_array([[-1, 0, 1],[-1, 0, 1],[1, 0, -1]]))
>>> y = ivy.to_list(x)
>>> print(y)
{
    a: [[-1, 0, 1], [-1, 0, 1], [1, 0, -1]]
}
>>> x =    ivy.Container(a=ivy.native_array([[[-1 ,0, 1],[1, 0 ,-1]],[[1, -1, 0],[1,0 ,-1]]]))
>>> y = ivy.to_list(x)
>>> print(y)
{
    a: [[[-1, 0, 1], [1, 0, -1]], [[1, -1, 0], [1, 0, -1]]]
}

Instance Method Examples

With ivy.Array instance method:

>>> x = ivy.array([0, 1, 2])
>>> y = x.to_list()
>>> print(y)
[0, 1, 2]

With ivy.Container instance method:

>>> x = ivy.Container(a=ivy.array([0, 1, 2]))
>>> y = x.to_list()
>>> print(y)
{a:[0,1,2]}
ivy.to_native_shape(shape)[source]

Returns the input shape in its native backend framework form

Parameters

shape (Union[Shape, NativeShape]) – The input to be converted

Return type

NativeShape

Returns

ret – the input in its native framework form

ivy.to_numpy(x, copy=True)[source]

Converts an array into a numpy array.

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

  • copy (bool) – whether to copy the array to a new address or not. Default is True. (default: True)

Return type

ndarray

Returns

  • ret – a numpy array copying all the element of the array x.

  • Functional Method Examples

  • ————————–

  • With ivy.Array inputs

  • >>> x = ivy.array([-1, 0, 1])

  • >>> y = ivy.to_numpy(x, copy=True)

  • >>> print(y)

  • [-1 0 1]

  • >>> x = ivy.array([[-1, 0, 1],[-1, 0, 1], [1,0,-1]])

  • >>> y = ivy.to_numpy(x, copy=True)

  • >>> print(y)

  • [[-1 0 1]

  • [-1 0 1]

  • [ 1 0 -1]]

  • With ivy.NativeArray inputs

  • >>> x = ivy.native_array([-1, 0, 1])

  • >>> y = ivy.to_numpy(x)

  • >>> print(y)

  • [-1 0 1]

  • >>> x = ivy.native_array([[-1, 0, 1],[-1, 0, 1], [1,0,-1]])

  • >>> y = ivy.to_numpy(x)

  • >>> print(y)

  • [[-1 0 1]

  • [-1 0 1]

  • [ 1 0 -1]]

  • With a mix of ivy.Container and ivy.NativeArray inputs

  • >>> x = ivy.Container(a=ivy.native_array([-1, 0, 1]))

  • >>> y = ivy.to_numpy(x)

  • >>> print(y)

  • { – a: array([-1, 0, 1], dtype=int32)

  • }

  • >>> x = ivy.Container(a=ivy.native_array([[-1, 0, 1], [-1, 0, 1], [1, 0, -1]]), b=ivy.native_array([[-1, 0, 0], [1, 0, 1], [1, 1, 1]]))

  • >>> y = ivy.to_numpy(x)

  • >>> print(y)

  • {

    a: array([[-1, 0, 1],

    [-1, 0, 1], [1, 0, -1]], dtype=int32),

    b: array([[-1, 0, 0],

    [1, 0, 1], [1, 1, 1]], dtype=int32)

  • }

  • With a mix of ivy.Container and ivy.Array inputs

  • >>> x = ivy.Container(x=ivy.array([-1, 0, 1]))

  • >>> y = ivy.to_numpy(x)

  • >>> print(y)

  • {x (array([-1,0,1],dtype=int32)})

  • >>> x = ivy.Container(a=ivy.array([[-1.0, 0., 1.], [-1, 0, 1], [1, 0, -1]]), b=ivy.array([[-1, 0, 0], [1, 0, 1], [1, 1, 1]]))

  • >>> y = ivy.to_numpy(x)

  • >>> print(y)

  • {

    a: array([[-1., 0., 1.],

    [-1., 0., 1.], [1., 0., -1.]], dtype=float32),

    b: array([[-1, 0, 0],

    [1, 0, 1], [1, 1, 1]], dtype=int32)

  • }

Instance Method Example

With ivy.Array inputs:

>>> x = ivy.array([-1, 0, 1])
>>> y = x.to_numpy()
>>> print(y)
[-1  0  1]
>>> x = ivy.array([[-1, 0, 1],[-1, 0, 1], [1,0,-1]])
>>> y = x.to_numpy()
>>> print(y)
[[-1  0  1]
[-1  0  1]
[ 1  0 -1]]

With ivy.Container inputs:

>>> x = ivy.Container(a=ivy.array([[-1.0, 0., 1.], [-1, 0, 1], [1, 0, -1]]),                      b=ivy.native_array([[-1, 0, 0], [1, 0, 1], [1, 1, 1]]))
>>> y = x.to_numpy()
>>> print(y)
{
    a: array([[-1., 0., 1.],
              [-1., 0., 1.],
              [1., 0., -1.]], dtype=float32),
    b: array([[-1, 0, 0],
              [1, 0, 1],
              [1, 1, 1]], dtype=int32)
}
>>> x = ivy.Container(a=ivy.array([-1, 0, 1]), b=ivy.array([1, 0, 1, 1]))
>>> y = x.to_numpy()
>>> print(y)
{
    a: array([-1, 0, 1], dtype=int32),
    b: array([1, 0, 1, 1], dtype=int32)
}
ivy.to_scalar(x)[source]

Converts an array with a single element into a scalar.

Parameters

x (Union[Array, NativeArray]) – Input array with a single element.

Return type

Number

Returns

ret – a scalar copying the element of the array x.

Functional Examples

With ivy.Array input:

>>> x = ivy.array([-1])
>>> y = ivy.to_scalar(x)
>>> print(y)
-1
>>> print(ivy.is_int_dtype(y))
True
>>> x = ivy.array([3])
>>> y = ivy.to_scalar(x)
>>> print(y)
3

With ivy.NativeArray input:

>>> x = ivy.native_array([-1])
>>> y = ivy.to_scalar(x)
>>> print(y)
-1
>>> print(ivy.is_int_dtype(y))
True
>>> x = ivy.native_array([3])
>>> y = ivy.to_scalar(x)
>>> print(y)
3

With a mix of ivy.Container and ivy.Array input:

>>> x = ivy.Container(a=ivy.array([-1]), b=ivy.array([3]))
>>> y = ivy.to_scalar(x)
>>> print(y)
{
    a: -1,
    b: 3
}
>>> print(ivy.is_int_dtype(y))
{
    a: true,
    b: true
}
>>> x = ivy.Container(a=ivy.array([1]), b=ivy.array([0]),                          c=ivy.array([-1]))
>>> y = ivy.to_scalar(x)
>>> print(y)
{
    a: 1,
    b: 0,
    c: -1
}

With a mix of ivy.Container and ivy.NativeArray input:

>>> x = ivy.Container(a=ivy.native_array([-1]), b=ivy.native_array([3]))
>>> y = ivy.to_scalar(x)
>>> print(y)
{
    a: -1,
    b: 3
}
>>> print(ivy.is_int_dtype(y))
{
    a: true,
    b: true
}
>>> x = ivy.Container(a=ivy.native_array([1]), b=ivy.native_array([0]),                          c=ivy.native_array([-1]))
>>> y = ivy.to_scalar(x)
>>> print(y)
{
    a: 1,
    b: 0,
    c: -1
}

Instance Method Examples

With ivy.Array instance method:

>>> x = ivy.array([-1])
>>> y = x.to_scalar()
>>> print(y)
-1
>>> print(ivy.is_int_dtype(y))
True
>>> x = ivy.array([3])
>>> y = x.to_scalar()
>>> print(y)
3

With a mix of ivy.Container instance method:

>>> x = ivy.Container(a=ivy.array([-1]), b=ivy.array([3]))
>>> y = x.to_scalar()
>>> print(y)
{
    a: -1,
    b: 3
}
>>> print(ivy.is_int_dtype(y))
{
    a: true,
    b: true
}
>>> x = ivy.Container(a=ivy.array([1]), b=ivy.array([0]),                          c=ivy.array([-1]))
>>> y = x.to_scalar()
>>> print(y)
{
    a: 1,
    b: 0,
    c: -1
}
ivy.try_else_none(fn, *args, **kwargs)[source]
Try and return the function, otherwise return None

if an exception was raised during function execution.

Parameters
  • fn (Callable) – Function to try and call and return.

  • args (Any) – list of arguments.

  • kwargs (Any) – dictionay of keyword arguments

Return type

Optional[Callable]

Returns

  • Either the function itself or None if an exception was raised

  • during function execution.

Examples

with: if the function is executed without any exception >>> x = ivy.array([1, 2, 3]) >>> y = ivy.array([4, 5, 6]) >>> z = ivy.try_else_none(ivy.add,x, y) >>> print(z.__name__) add

with: if the function is executed with an exception >>> x = ivy.array([1, 2, 3]) >>> y = ‘hemant’ >>> z = ivy.try_else_none(ivy.add,x, y) >>> print(z) None

ivy.unset_array_mode()[source]

Reset the mode of converting inputs to ivy.NativeArray, then converting outputs back to ivy.Array to the previous state

Examples

>>> ivy.set_array_mode(False)
>>> ivy.get_array_mode()
False
>>> ivy.unset_shape_array_mode()
>>> ivy.get_array_mode()
True
Return type

None

ivy.unset_nestable_mode()[source]

Reset the mode of whether to check if function inputs are ivy.Container to the previous state

Examples

>>> ivy.set_nestable_mode(False)
>>> ivy.get_nestable_mode()
False
>>> ivy.unset_nestable_mode()
>>> ivy.get_nestable_mode()
True
Return type

None

ivy.unset_shape_array_mode()[source]

Reset the mode of returning shape as ivy.Array to the previous state

Examples

>>> ivy.set_shape_array_mode(True)
>>> ivy.shape_array_mode()
True
>>> ivy.unset_shape_array_mode()
>>> ivy.shape_array_mode()
False
Return type

None

ivy.unstack(x, axis, keepdims=False)[source]

Unpacks the given dimension of a rank-R array into rank-(R-1) arrays.

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

  • axis (int) – Axis for which to unpack the array.

  • keepdims (bool) – Whether to keep dimension 1 in the unstack dimensions. Default is False. (default: False)

Return type

Union[Array, NativeArray]

Returns

ret – List of arrays, unpacked along specified dimensions.

ivy.value_is_nan(x, include_infs=True)[source]

Determine whether the single valued array or scalar is of nan type.

Parameters
  • x (Union[Array, NativeArray, Number]) – The input to check Input array.

  • include_infs (bool) – Whether to include infs and -infs in the check. Default is True. (default: True)

Return type

bool

Returns

ret – Boolean as to whether the input value is a nan or not.