General#

Collection of general Ivy functions.

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

Determine whether the inputs are all equal.

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

  • equality_matrix (bool, default: False) – Whether to return a matrix of equalities comparing each input with every other. 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 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)
>>> print(y)
True
>>> x1 = ivy.array([0, 0])
>>> x2 = ivy.array([0, 0])
>>> x3 = ivy.array([1, 0])
>>> y = ivy.all_equal(x1, x2, x3, equality_matrix=True)
>>> print(y)
ivy.array([[ True,  True, False],
   [ True,  True, False],
   [False, False,  True]])

With one ivy.Container inputs:

>>> x1 = ivy.Container(a=ivy.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.array([1, 0, 0, 1]))
>>> x2 = ivy.Container(a=ivy.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], default: None) – The name of the argument

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

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]#

Get 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', 'trace_on_next_step', 'device']
ivy.array_equal(x0, x1, /)[source]#

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

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.assert_supports_inplace(x, /)[source]#

Assert that inplace operations are supported for x.

Parameters:

x (Union[Array, NativeArray]) – Input variable or array to check for inplace support for.

Return type:

bool

Returns:

  • ret – True if supports, raises IvyBackendException otherwise

  • This function is *nestable*, and therefore also accepts (code:’ivy.Container’)

  • instance in place of the argument.

Examples

With ivy.Array input and default backend set as numpy:

>>> ivy.set_backend("numpy")
>>> x = ivy.array([1, 2, 3])
>>> print(x.assert_supports_inplace())
True

With ivy.Array input and default backend set as torch:

>>> ivy.set_backend("torch")
>>> x = ivy.array([1, 2, 3])
>>> print(x.assert_supports_inplace())
True

With ivy.Container input and default backend set as numpy:

>>> ivy.set_backend("numpy")
>>> x = ivy.Container(a=ivy.array([5, 6]), b=ivy.array([7, 8]))
>>> print(x.assert_supports_inplace())
{
    a: True,
    b: True
}

With ivy.Container input and default backend set as torch:

>>> ivy.set_backend("torch")
>>> x = ivy.Container(a=ivy.array([5, 6]), b=ivy.array([7, 8]))
>>> print(x.assert_supports_inplace())
{
    a: True,
    b: True
}
ivy.cache_fn(func)[source]#

Cache function outputs.

A decorator to wrap a function, such that computed outputs are cached to avoid recalculating them later.

Parameters:

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

Return type:

Callable

Returns:

ret – The newly cache wrapped function.

Examples

With positional arguments only:

>>> def my_sum(val1:float, val2:float)->float: return val1 + val2
>>> cached_sum = ivy.cache_fn(my_sum)
>>> print(cached_sum(3, 5))
8

With keyword arguments:

>>> def line_eq(x:float, /, *, slp:float=2, itc:float=0)->float: return x*slp+itc
>>> cached_line_eq = ivy.cache_fn(line_eq)
>>> print(cached_line_eq(3, itc=5, slp=2))
11
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, default: 2.0) – The p-value for computing the p-norm. Default is 2.

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

Return type:

Array

Returns:

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

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, p=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))
>>> y = ivy.clip_matrix_norm(x, 0.5, p=2.0)
>>> 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, p=1.0, out=x)
>>> print(x)
ivy.array([[0., 1.],
           [2., 3.]])

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]) – Input array containing elements to clip.

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

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

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

Return type:

Array

Returns:

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

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.89442718, 1.78885436])
>>> x = ivy.array([0.5, -0.7, 2.4])
>>> y = ivy.clip_vector_norm(x, 3.0, p=1.0)
>>> print(y)
ivy.array([ 0.41666666, -0.58333331,  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, p=1.0, out=y)
>>> print(y)
ivy.array([[[0.        , 0.        ],
    [0.06666667, 0.20000002],
    [0.13333334, 0.40000004]],
[[0.20000002, 0.60000002],

[0.26666668, 0.80000007], [0.33333334, 1. ]]]))

>>> x = ivy.array([[1.1, 2.2, 3.3],
...                [-4.4, -5.5, -6.6]])
>>> ivy.clip_vector_norm(x, 1.0, p=3.0, out=x)
>>> print(x)
ivy.array([[ 0.13137734,  0.26275468,  0.39413199],
   [-0.52550936, -0.6568867 , -0.78826398]])

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.89442718, 1.78885436]),
    b: ivy.array([0.84852815, 1.1313709, 1.41421366])
}

With multiple ivy.Container inputs:

>>> x = ivy.Container(a=ivy.array([0., 1., 2.]),
...                   b=ivy.array([3., 4., 5.]))
>>> max_norm = ivy.Container(a=2, b=3)
>>> y = ivy.clip_vector_norm(x, max_norm)
>>> print(y)
{
    a: ivy.array([0., 0.89442718, 1.78885436]),
    b: ivy.array([1.27279221, 1.69705628, 2.12132034])
}
ivy.container_types()[source]#

Summary.

Returns:

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

ivy.current_backend_str()[source]#

Return framework string.

Return type:

Optional[str]

Returns:

ret – The framework string.

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

Return 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, default: False) – Whether to catch exceptions from callable x. Default is False.

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

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

Return type:

Any

Returns:

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

Examples

With Any input:

>>> x = None
>>> y = ivy.default(x, "default_string")
>>> print(y)
default_string
>>> x = ""
>>> y = ivy.default(x, "default_string")
>>> print(y)
>>> x = ivy.array([4, 5, 6])
>>> y = ivy.default(x, ivy.array([1, 2, 3]), rev=True)
>>> print(y)
ivy.array([1, 2, 3])
>>> x = lambda: ivy.array([1, 2, 3])
>>> y = ivy.default(x, ivy.array([4, 5, 6]), with_callable=True)
>>> print(y)
ivy.array([1, 2, 3])
>>> x = lambda: None
>>> y = ivy.default(x, lambda: ivy.array([1, 2, 3]), with_callable=True)
>>> print(y)
ivy.array([1, 2, 3])
>>> x = lambda: None
>>> y = ivy.default(x, lambda: ivy.array([1, 2, 3]), catch_exceptions=True)
>>> print(y)
ivy.array([1, 2, 3])
>>> x = lambda a, b: a + b
>>> y = ivy.default(x, lambda: ivy.array([1, 2, 3]), with_callable=True,
...                 catch_exceptions=True)
>>> print(y)
ivy.array([1, 2, 3])
>>> x = lambda a, b: a + b
>>> y = ivy.default(x, lambda: ivy.array([1, 2, 3]), with_callable=True,
...                 catch_exceptions=True, rev=True)
>>> print(y)
ivy.array([1, 2, 3])
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], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

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

Examples

With ivy.Array instance method:

>>> x = ivy.array([[1, 2, 3],
...               [-4, -5, -6]])
>>> y = x.einops_rearrange("height width -> width height")
>>> print(y)
ivy.array([[ 1, -4],
       [ 2, -5],
       [ 3, -6]])
>>> x = ivy.array([[[ 1,  2,  3],
...                  [ 4,  5,  6]],
...               [[ 7,  8,  9],
...                  [10, 11, 12]]])
>>> y = x.einops_rearrange("c h w -> c (h w)")
>>> print(y)
ivy.array([[ 1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12]])
>>> x = ivy.array([[1, 2, 3, 4, 5, 6],
...            [7, 8, 9, 10, 11, 12]])
>>> y = ivy.zeros((4,3))
>>> x.einops_rearrange("c (h w) -> (c h) w", out=y, h=2, w=3)
>>> print(y)
ivy.array([[ 1,  2,  3],
       [ 4,  5,  6],
       [ 7,  8,  9],
       [10, 11, 12]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([[-4.47, 0.93, -3.34],
...                            [3.66, 24.29, 3.64]]),
...               b=ivy.array([[4.96, 1.52, -10.67],
...                            [4.36, 13.96, 0.3]]))
>>> y = ivy.einops_rearrange(x, 'a b -> b a')
>>> print(y)
{
    a: ivy.array([[-4.46999979, 3.66000009],
                  [0.93000001, 24.29000092],
                  [-3.33999991, 3.6400001]]),
    b: ivy.array([[4.96000004, 4.36000013],
                  [1.51999998, 13.96000004],
                  [-10.67000008, 0.30000001]])
}

With varying pattern:

Suppose we have a set of 32 images in “h w c” format (height-width-channel) and concatenate images along height (vertical axis), 960 = 32 * 30

>>> images = ivy.asarray([ivy.random_normal(shape=(30, 40, 3)) for _ in range(32)])
>>> x = ivy.einops_rearrange(images, 'b h w c -> (b h) w c')
>>> print(x.shape)
(960, 40, 3)

# Concatenate images along horizontal axis, 1280 = 32 * 40

>>> images = ivy.asarray([ivy.random_normal(shape=(30, 40, 3)) for _ in range(32)])
>>> x = ivy.einops_rearrange(images, 'b h w c -> h (b w) c')
>>> print(x.shape)
(30, 1280, 3)

# Reorder axes to “b c h w” format for deep learning

>>> images = ivy.asarray([ivy.random_normal(shape=(30, 40, 3)) for _ in range(32)])
>>> x = ivy.einops_rearrange(images, 'b h w c -> b c h w')
>>> print(x.shape)
(32, 3, 30, 40)

# Flatten each image into a vector, 3600 = 30 * 40 * 3

>>> images = ivy.asarray([ivy.random_normal(shape=(30, 40, 3)) for _ in range(32)])
>>> x = ivy.einops_rearrange(images, 'b h w c -> b (c h w)')
>>> print(x.shape)
(32, 3600)

# Split each image into 4 smaller (top-left, top-right, bottom-left, bottom-right), # 128 = 32 * 2 * 2

>>> images = ivy.asarray([ivy.random_normal(shape=(30, 40, 3)) for _ in range(32)])
>>> x = ivy.einops_rearrange(images, 'b (h1 h) (w1 w) c -> (b h1 w1) h w c',
... h1=2, w1=2)
>>> print(x.shape)
(128, 15, 20, 3)

# Space-to-depth operation >>> images = ivy.asarray([ivy.random_normal(shape=(30, 40, 3)) for _ in range(32)]) >>> x = ivy.einops_rearrange(images, ‘b (h h1) (w w1) c -> b h w (c h1 w1)’, h1=2, … w1=2) >>> print(x.shape) (32, 15, 20, 12)

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], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

  • ret – New array with einops.reduce having been applied.

  • This function is *nestable*, and therefore also accepts (code:’ivy.Container’)

  • instance in place of the argument.

Examples

With ivy.Array input:

>>> x = ivy.array([[-4.47, 0.93, -3.34],
...                [3.66, 24.29, 3.64]])
>>> reduced = ivy.einops_reduce(x, 'a b -> b', 'mean')
>>> print(reduced)
ivy.array([-0.40499985, 12.61000061, 0.1500001 ])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([[-4.47, 0.93, -3.34],
...                                [3.66, 24.29, 3.64]]),
...                   b=ivy.array([[4.96, 1.52, -10.67],
...                                [4.36, 13.96, 0.3]]))
>>> reduced = ivy.einops_reduce(x, 'a b -> a', 'mean')
>>> print(reduced)
{
    a: ivy.array([-2.29333329, 10.53000069]),
    b: ivy.array([-1.39666676, 6.20666695])
}
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], default: None) – optional output array, for writing the result to. It must have a shape that the inputs broadcast to.

Return type:

Array

Returns:

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

  • This function is *nestable*, and therefore also accepts (code:’ivy.Container’)

  • instance in place of the argument.

Examples

With ivy.Array input:

>>> x = ivy.array([1, 2, 3, 4])
>>> repeated = ivy.einops_repeat(x, 'a -> b a', b=2)
>>> print(repeated)
ivy.array([[1, 2, 3, 4],
           [1, 2, 3, 4]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([[4,5],
...                                [1, 3]]),
...                    b=ivy.array([[9, 10],
...                                 [4, 2]]))
>>> repeated = ivy.einops_repeat(x, 'h w -> h (c w)', c=2)
>>> print(repeated)
{
    a: ivy.array([[4, 5, 4, 5],
                  [1, 3, 1, 3]]),
    b: ivy.array([[9, 10, 9, 10],
                  [4, 2, 4, 2]])
}
ivy.exists(x, /)[source]#

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.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
ivy.fourier_encode(x, max_freq, /, *, num_bands=4, linear=False, concat=True, flatten=False)[source]#

Pad 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, default: 4) – The number of frequency bands for the encoding. Default is 4.

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

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

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

Return type:

Union[Array, NativeArray, Tuple]

Returns:

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

Examples

>>> x = ivy.array([1,2,3])
>>> y = 1.5
>>> z = ivy.fourier_encode(x,y)
>>> print(z)
ivy.array([[ 1.0000000e+00, 1.2246468e-16, 0.0000000e+00, 0.0000000e+00,
             0.0000000e+00, -1.0000000e+00, 1.0000000e+00, 1.0000000e+00,
             1.0000000e+00],
           [ 2.0000000e+00, -2.4492936e-16, 0.0000000e+00, 0.0000000e+00,
             0.0000000e+00, 1.0000000e+00, 1.0000000e+00, 1.0000000e+00,
             1.0000000e+00],
           [ 3.0000000e+00, 3.6739404e-16, 0.0000000e+00, 0.0000000e+00,
             0.0000000e+00, -1.0000000e+00, 1.0000000e+00, 1.0000000e+00,
             1.0000000e+00]])
>>> x = ivy.array([3,10])
>>> y = 2.5
>>> z = ivy.fourier_encode(x, y, num_bands=3)
>>> print(z)
ivy.array([[ 3.0000000e+00,  3.6739404e-16,  3.6739404e-16, 3.6739404e-16,
            -1.0000000e+00, -1.0000000e+00, -1.0000000e+00],
           [ 1.0000000e+01, -1.2246468e-15, -1.2246468e-15, -1.2246468e-15,
             1.0000000e+00,  1.0000000e+00,  1.0000000e+00]])
ivy.function_supported_devices_and_dtypes(fn, recurse=True)[source]#

Return the supported combination of devices and dtypes of the current backend’s function. The function returns a dict containing the supported combination of devices and dtypes of the primary and compositional implementations in case of partial mixed functions.

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

  • recurse (bool, default: True) – Whether to recurse into used ivy functions. Default is True.

Return type:

Dict

Returns:

ret – Tuple or dict containing the supported devices and dtypes of the function

ivy.function_unsupported_devices_and_dtypes(fn, recurse=True)[source]#

Return the unsupported combination of devices and dtypes of the current backend’s function. The function returns a dict containing the unsupported combination of devices and dtypes of the primary and compositional implementations in case of partial mixed functions.

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

  • recurse (bool, default: True) – Whether to recurse into used ivy functions. Default is True.

Return type:

Dict

Returns:

ret – Tuple or dict containing the unsupported devices and dtypes of the function

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

Gather slices from params at axis according to indices.

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

  • indices (Union[Array, NativeArray]) – The array which indicates the indices that will be gathered along the specified axis.

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

  • batch_dims (int, default: 0) – Optional int, lets you gather different items from each element of a batch. Default is 0.

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

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.

Examples

With ivy.Array input:

>>> x = ivy.array([0., 1., 2.])
>>> y = ivy.array([1, 2])
>>> print(ivy.gather(x, y))
ivy.array([1., 2.])
>>> x = ivy.array([[0., 1., 2.],[3., 4., 5.]])
>>> y = ivy.array([[0, 1],[1, 2]])
>>> z = ivy.zeros((2, 2, 2))
>>> ivy.gather(x, y, out=z)
>>> print(z)
ivy.array([[[0., 1.],[1., 2.]],[[3., 4.],[4., 5.]]])
>>> x = ivy.array([[[0., 1.], [2., 3.]],
...                [[8., 9.], [10., 11.]]])
>>> y = ivy.array([[0, 1]])
>>> z = ivy.zeros((1, 2, 2, 2))
>>> ivy.gather(x, y, axis=0, out=z)
>>> print(z)
ivy.array(
    [[[[ 0.,  1.],
       [ 2.,  3.]],
      [[ 8.,  9.],
       [10., 11.]]]])
>>> x = ivy.array([[0, 10, 20, 0, 0],
...                [0, 0, 0, 30, 40],
...                [0, 10, 0, 0, 40]])
>>> y = ivy.array([[1, 2],[3, 4],[1, 4]])
>>> z = ivy.gather(x, y, batch_dims=1)
>>> print(z)
ivy.array([[10, 20], [30, 40],[10, 40]])

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.])
}

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.])
}
ivy.gather_nd(params, indices, /, *, batch_dims=0, 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.

  • batch_dims (int, default: 0) – optional int, lets you gather different items from each element of a batch.

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

Return type:

Array

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.)
>>> x = ivy.array([[0., 1.], [2., 3.], [4., 5.]])
>>> y = ivy.array([[0],[1],[1]], dtype='int32')
>>> z = ivy.gather_nd(x,y,batch_dims=1)
ivy.array([0., 3., 5.])

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., 10., 20.],[30.,40.,50.]]),
...                   b=ivy.array([[0., 100., 200.],[300.,400.,500.]]))
>>> y = ivy.Container(a=ivy.array([1,0]),
...                   b=ivy.array([0]))
>>> print(ivy.gather_nd(x, y))
{
    a: ivy.array(30.),
    b: ivy.array([0., 100., 200.])
}
ivy.get_all_arrays_in_memory()[source]#

Get all arrays which are currently alive.

Return type:

List[Union[Array, NativeArray]]

Returns:

ret – All arrays which are alive.

Examples

>>> ivy.get_all_arrays_in_memory()
[]
>>> x = ivy.get_all_arrays_in_memory()
>>> x
[]
>>> y = ivy.array([0, 1, 2])
>>> x
[ivy.array([0, 1, 2])]
ivy.get_item(x, /, query, *, copy=None)[source]#

Gather slices from x according to query array, identical to x[query].

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

  • query (Union[Array, NativeArray, Tuple]) – array, index array, integer indices or boolean mask.

  • copy (Optional[bool], default: None) – boolean indicating whether or not to copy the input array. If True, the function must always copy. If False, the function must never copy. In case copy is False we avoid copying by returning a view of the input array.

Return type:

Array

Returns:

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

Examples

>>> x = ivy.array([0, -1, 20])
>>> query = ivy.array([0, 1])
>>> print(ivy.get_item(x, query))
ivy.array([ 0, -1])
>>> x = ivy.array([[4, 5], [20, 128], [-2, -10]])
>>> query = ivy.array([[True, False], [False, False], [True, True]])
>>> print(ivy.get_item(x, query))
ivy.array([  4,  -2, -10])
ivy.get_num_dims(x, /, *, as_array=False)[source]#

Return 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, default: False) – Whether to return the shape as a array, default False.

Return type:

int

Returns:

  • ret – Shape of the array

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

>>> a = ivy.array([[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
...                    [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
...                    [[0, 0, 0], [0, 0, 0], [0, 0, 0]]])
>>> b = ivy.get_num_dims(a, as_array=False)
>>> print(b)
3

With ivy.Container input:

>>> a = ivy.Container(b = ivy.asarray([[0.,1.,1.],[1.,0.,0.],[8.,2.,3.]]))
>>> print(ivy.get_num_dims(a))
{
    b: 2
}
>>> b = ivy.get_num_dims(a, as_array=True)
>>> print(b)
{
    b: ivy.array(2)
}
ivy.get_referrers_recursive(item, *, depth=0, max_depth=None, seen_set=None, local_set=None)[source]#

Recursively retrieve referrers for an object.

This function recursively fetches referrers for the specified item up to a given max_depth.

Parameters:
  • item (object) – The object for which referrers should be retrieved.

  • depth (int, default: 0) – Current depth in the recursion. (default is 0)

  • max_depth (Optional[int], default: None) – Maximum depth of recursion. If None, there’s no depth limit. (default is None)

  • seen_set (Optional[set], default: None) – Set of seen referrer IDs to prevent duplicates. (default is None)

  • local_set (Optional[set], default: None) – Set of local referrer IDs to avoid redundancy. (default is None)

Return type:

Container

Returns:

ret – A container representing referrers and their sub-referrers, respecting the max_depth.

Examples

>>> import gc
>>> example_function = lambda: (obj := [1, 2, 3]) and ivy.get_referrers_recursive(obj, max_depth=2)
>>> result = example_function()
>>> print(result)
{repr:[1,2,3]}
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, default: True) – Whether to include +infinity and -infinity in the check. Default is True.

Return type:

bool

Returns:

ret – Boolean as to whether the array contains nans.

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, include_infs=False)
>>> print(y)
False

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
}
ivy.inplace_arrays_supported()[source]#

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

Return type:

bool

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 input array to be decremented by the defined value.

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

Return type:

Array

Returns:

ret – The array following the in-place decrement.

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([[5.3, 7., 0.],[6.8, 8, 3.9],[0., 10., 6.3]])
>>> y = ivy.inplace_decrement(x, 1.25)
>>> print(y)
ivy.array([[ 4.05,  5.75, -1.25],
   [ 5.55,  6.75,  2.65],
   [-1.25,  8.75,  5.05]])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0.5, -5., 30.]), b=ivy.array([0., -25., 50.]))
>>> y = ivy.inplace_decrement(x, 1.5)
>>> print(y)
{
    a: ivy.array([-1., -6.5, 28.5]),
    b: ivy.array([-1.5, -26.5, 48.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_decrement(x, y)
>>> print(z)
{
    a: ivy.array([0., 0., 0.]),
    b: ivy.array([0., 0., 0.])
}
>>> x = ivy.Container(a=ivy.array([3., 7., 10.]), b=ivy.array([0., 75., 5.5]))
>>> y = ivy.Container(a=ivy.array([2., 5.5, 7.]), b=ivy.array([0., 25., 2.]))
>>> z = ivy.inplace_decrement(x, y)
>>> print(z)
{
    a: ivy.array([1., 1.5, 3.]),
    b: ivy.array([0., 50., 3.5])
}
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.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, keep_input_dtype=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, default: False) – Whether or not to ensure that the ivy.NativeArray is also inplace updated. In cases where it should be, backends which do not natively support inplace updates will raise an exception.

  • keep_input_dtype (bool, default: False) – Whether or not to preserve x data type after the update, otherwise val data type will be applied. Defaults to False.

Return type:

Array

Returns:

ret – The array following the in-place update.

Raises:
  • IvyException – If backend set doesn’t natively support inplace updates and ensure_in_backend is True, above exception will be raised.

  • This function is nestable, and therefore also accepts :code:'ivy.Container'

  • instance in place of the arguments.

Examples

With ivy.Array input and default backend set as numpy:

>>> ivy.set_backend("numpy")
>>> x = ivy.array([1, 2, 3])
>>> y = ivy.array([0])
>>> ivy.inplace_update(x, y)
>>> print(x)
ivy.array([0])

With ivy.Array input and default backend set as numpy:

>>> ivy.set_backend("numpy")
>>> x = ivy.array([1, 2, 3], dtype=ivy.float32)
>>> y = ivy.array([0, 0, 0], dtype=ivy.int32)
>>> ivy.inplace_update(x, y, keep_input_dtype=True)
>>> print(x)
ivy.array([0., 0., 0.])

With ivy.Container instances:, and backend set as torch:

>>> ivy.set_backend("torch")
>>> x = ivy.Container(a=ivy.array([5, 6]), b=ivy.array([7, 8]))
>>> y = ivy.Container(a=ivy.array([1]), b=ivy.array([2]))
>>> ivy.inplace_update(x, y)
>>> print(x)
{
    a: ivy.array([1, 1]),
    b: ivy.array([2, 2])
}

With mix of ivy.Array and ivy.Container instances:, and backend set as torch:

>>> ivy.set_backend("torch")
>>> x = ivy.Container(a=ivy.array([5, 6]), b=ivy.array([7, 8]))
>>> y = ivy.array([1, 2])
>>> ivy.inplace_update(x, y)
>>> print(x)
{
    a: ivy.array([1, 2]),
    b: ivy.array([1, 2])
}
ivy.inplace_variables_supported()[source]#

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

Return type:

bool

Returns:

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

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

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

Parameters:
  • x (Any) – The input to check

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

Return type:

bool

Returns:

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

Examples

>>> x = ivy.array([0, 1, 2])
>>> print(ivy.is_array(x))
True
>>> x = ivy.native_array([9.1, -8.3, 2.8, 3.0])
>>> print(ivy.is_array(x, exclusive=True))
True
>>> x = [2, 3]
>>> print(ivy.is_array(x))
False
ivy.is_ivy_array(x, /, *, exclusive=False)[source]#

Determine whether the input x is a valid Ivy Array.

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

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

Return type:

bool

Returns:

ret – Boolean, whether or not x is a valid Ivy Array.

Examples

>>> x = ivy.array([0, 1, 2])
>>> ivy.is_ivy_array(x)
True
>>> x = ivy.native_array([9.1, -8.3, 2.8, 3.0])
>>> ivy.is_ivy_array(x, exclusive=True)
False
ivy.is_ivy_container(x, /)[source]#

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

Examples

>>> x = ivy.Container()
>>> print(ivy.is_ivy_container(x))
True
>>> x = [2, 3]
>>> print(ivy.is_ivy_container(x))
False
ivy.is_ivy_nested_array(x, /)[source]#

Determine whether the input x is an Ivy Nested Array.

Parameters:

x (Any) – The input to check

Return type:

bool

Returns:

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

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

Determine whether the input x is an ivy.NativeArray instance.

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

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

Return type:

bool

Returns:

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

Examples

>>> x = ivy.array([0, 1, 2])
>>> ivy.is_native_array(x)
False
>>> x = ivy.native_array([9.1, -8.3, 2.8, 3.0])
>>> ivy.is_native_array(x, exclusive=True)
True
ivy.isin(elements, test_elements, /, *, assume_unique=False, invert=False)[source]#

Test 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 (bool, default: False) – If True, assumes both elements and test_elements contain unique elements, which can speed up the calculation. Default value is False.

  • invert (bool, default: False) – If True, inverts the boolean return array, resulting in True values for 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.isscalar(x, /)[source]#
Return type:

bool

ivy.itemsize(x, /)[source]#

Return the size of the input array’s elements.

Parameters:

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

Return type:

int

Returns:

ret – An integer specifying the element size in bytes.

Examples

>>> x = ivy.array([1,2,3], dtype=ivy.float64)
>>> ivy.itemsize(x)
8
>>> x = ivy.array([1,2,3], dtype=ivy.complex128)
>>> ivy.itemsize(x)
16
ivy.match_kwargs(kwargs, *receivers, allow_duplicates=False)[source]#

Match keyword arguments to either class or function receivers.

Parameters:
  • kwargs (Dict) – Keyword arguments to match.

  • receivers (Iterable[Callable]) – Functions and/or classes to match the keyword arguments to.

  • allow_duplicates (bool, default: False) – Whether to allow one keyword argument to be used for multiple receivers. Default is False.

Return type:

Union[List[Dict], Dict]

Returns:

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

Examples

>>> o = ivy.zeros(3)
>>> kwargs = {'out': o, 'bias': ivy.arange(3)}
>>> x = ivy.match_kwargs(kwargs, ivy.add, ivy.linear)
>>> print(x)
[{'out': ivy.array([0., 0., 0.])}, {'bias': ivy.array([0, 1, 2])}]
>>> o = ivy.zeros(3)
>>> kwargs = {'out': o, 'bias': ivy.arange(3)}
>>> x = ivy.match_kwargs(kwargs, ivy.linear, ivy.add)
>>> print(x)
[{'out': ivy.array([0., 0., 0.]), 'bias': ivy.array([0, 1, 2])}, {}]
ivy.multiprocessing(context=None)[source]#

Return backend-specific multiprocessing module.

Parameters:

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

Returns:

ret – Multiprocessing module

Examples

>>> import ivy

Using the default context (None):

>>> mp_default = ivy.multiprocessing()
>>> print(mp_default)
<multiprocessing.context.DefaultContext object at 0x7f4e3193e520>

Specifying ‘fork’ as the context:

>>> mp_fork = ivy.multiprocessing(context='fork')
>>> print(mp_fork)
<multiprocessing.context.ForkContext object at 0x7f4e3193e580>

Specifying ‘spawn’ as the context:

>>> mp_spawn = ivy.multiprocessing(context='spawn')
>>> print(mp_spawn)
<multiprocessing.context.SpawnContext object at 0x7f4e3193e5e0>

Specifying ‘forkserver’ as the context:

>>> mp_forkserver = ivy.multiprocessing(context='forkserver')
>>> print(mp_forkserver)
<multiprocessing.context.ForkServerContext object at 0x7f4e3193e640>
ivy.num_arrays_in_memory()[source]#

Return the number of arrays which are currently alive.

Return type:

int

Returns:

ret – Number of all arrays which are alive.

Examples

>>> ivy.num_arrays_in_memory()
0
>>> x = ivy.num_arrays_in_memory()
>>> x
0
>>> y = ivy.array([0, 1, 2])
>>> x
1
ivy.print_all_arrays_in_memory()[source]#

Print all native Ivy arrays in memory to the console.

Gets all the native Ivy arrays which are currently alive(in the garbage collector) from get_all_arrays_in_memory() function and prints them to the console.

ivy.scatter_flat(indices, updates, /, *, size=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], default: None) – The size of the result. Default is None, in which case tensor argument out must be provided.

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

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

Return type:

Array

Returns:

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

  • This function is *nestable*, and therefore also accepts (code:’ivy.Container’)

  • instance in place of the argument.

Examples

With ivy.Array input: >>> indices = ivy.array([0, 0, 1, 0, 2, 2, 3, 3]) >>> updates = ivy.array([5, 1, 7, 2, 3, 2, 1, 3]) >>> out = ivy.array([0, 0, 0, 0, 0, 0, 0, 0]) >>> ivy.scatter_flat(indices, updates, out=out) >>> print(out) ivy.array([8, 7, 5, 4, 0, 0, 0, 0])

With ivy.Array input: >>> indices = ivy.array([1, 0, 1, 0, 2, 2, 3, 3]) >>> updates = ivy.array([9, 2, 0, 2, 3, 2, 1, 8]) >>> size = 8 >>> print(ivy.scatter_flat(indices, updates, size=size)) ivy.array([2, 0, 2, 8, 0, 0, 0, 0])

With ivy.Container and ivy.Array input: >>> indices = ivy.array([1, 0, 1, 0, 2, 2, 3, 3]) >>> updates = ivy.Container(a=ivy.array([9, 2, 0, 2, 3, 2, 1, 8]), … b=ivy.array([5, 1, 7, 2, 3, 2, 1, 3])) >>> size = 8 >>> print(ivy.scatter_flat(indices, updates, size=size)) {

a: ivy.array([2, 0, 2, 8, 0, 0, 0, 0]), b: ivy.array([2, 7, 2, 3, 0, 0, 0, 0])

}

With ivy.Container input: >>> indices = ivy.Container(a=ivy.array([1, 0, 1, 0, 2, 2, 3, 3]), … b=ivy.array([0, 0, 1, 0, 2, 2, 3, 3])) >>> updates = ivy.Container(a=ivy.array([9, 2, 0, 2, 3, 2, 1, 8]), … b=ivy.array([5, 1, 7, 2, 3, 2, 1, 3])) >>> size = 8 >>> print(ivy.scatter_flat(indices, updates, size=size)) {

a: ivy.array([2, 0, 2, 8, 0, 0, 0, 0]), b: ivy.array([2, 7, 2, 3, 0, 0, 0, 0])

}

ivy.scatter_nd(indices, updates, /, shape=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[tuple, list, Array, Shape, NativeShape]], default: None) – The shape of the result. Default is None, in which case tensor argument must be provided.

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

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

Return type:

Array

Returns:

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

Examples

With ivy.Array input:

>>> indices = ivy.array([[4], [3], [7], [7]])
>>> updates = ivy.array([9, 12, 11, 10])
>>> shape = ivy.array([8])
>>> scatter = ivy.scatter_nd(indices, updates, shape)
>>> print(scatter)
ivy.array([ 0,  0,  0, 12,  9,  0,  0, 21])
>>> indices = ivy.array([[0, 1], [1, 0], [1, 1], [1, 1]])
>>> updates = ivy.array([9, 11, 12, 10])
>>> shape = (2, 2)
>>> scatter = ivy.scatter_nd(indices, updates, shape, reduction="max")
>>> print(scatter)
ivy.array([[ 0,  9], [11, 12]])
>>> indices = ivy.array([[[0], [1]], [[2], [1]]])
>>> updates = ivy.array([[9, 12], [11, 10]])
>>> shape = [4]
>>> scatter = ivy.scatter_nd(indices, updates, shape, reduction="replace")
>>> print(scatter)
ivy.array([ 9, 10, 11,  0])
>>> indices = ivy.array([[[1, 1], [0, 0]], [[1, 1], [0, 0]]])
>>> updates = ivy.array([[-1, 12], [11, 10]])
>>> shape = ivy.Shape([2, 2])
>>> result = ivy.zeros([2, 2])
>>> scatter = ivy.scatter_nd(indices, updates, shape, reduction="min", out=result)
>>> print(result)
ivy.array([[ 0.,  0.], [ 0., -1.]])

With ivy.Container input:

>>> indices = ivy.Container(a=ivy.array([[4],[3],[6]]),
...                         b=ivy.array([[5],[1],[2]]))
>>> updates = ivy.Container(a=ivy.array([100, 200, 200]),
...                         b=ivy.array([20, 30, 40]))
>>> shape = ivy.Container(a=ivy.array([10]),
...                       b=ivy.array([10]))
>>> z = ivy.scatter_nd(indices, updates, shape=shape)
>>> print(z)
{
    a: ivy.array([0, 0, 0, 200, 100, 0, 200, 0, 0, 0]),
    b: ivy.array([0, 30, 40, 0, 0, 20, 0, 0, 0, 0])
}

With ivy.Container and ivy.Array input:

>>> indices = ivy.array([[4],[3],[1]])
>>> updates = ivy.Container(a=ivy.array([10, 20, 30]),
...                         b=ivy.array([200, 300, 400]))
>>> z = ivy.Container(a=ivy.array([1, 2, 3, 4, 5]),
...                   b=ivy.array([10, 20, 30, 40, 50]))
>>> ivy.scatter_nd(indices, updates, reduction="replace", out=z)
>>> print(z)
{
    a: ivy.array([1, 30, 3, 20, 10]),
    b: ivy.array([10, 400, 30, 300, 200])
}
ivy.set_array_mode(mode)[source]#

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

It Stops the conversion of ivy.NativeArray to ivy.Array in the case when it is set to False.

Return type:

None

Parameter#

mode

boolean whether to perform ivy.Array conversions

Examples

>>> ivy.set_array_mode(False)
>>> ivy.array_mode
False
>>> ivy.set_array_mode(True)
>>> ivy.array_mode
True
ivy.set_exception_trace_mode(mode)[source]#

Set the mode of whether to show frontend-truncated exception stack traces, ivy- truncated exception stack traces or full exception stack traces.

Return type:

None

Parameter#

mode

str exception trace mode, one of ivy, full or frontend

Examples

>>> ivy.set_exception_trace_mode("ivy")
>>> ivy.exception_trace_mode
'ivy'
>>> ivy.set_exception_trace_mode("full")
>>> ivy.exception_trace_mode
'full'
ivy.set_inplace_mode(mode='lenient')[source]#

Set the memory management behavior for in-place updates in Ivy.

By default, Ivy creates new arrays in the backend for in-place updates. However, this behavior can be controlled by the user using the ‘inplace_mode’ parameter.

Parameters:

mode (str) –

The mode for memory management during in-place updates. - ‘lenient’: (Default) In this mode, new arrays will be created during

in-place updates to avoid breaking existing code. This is the default behavior.

  • ’strict’: In this mode, an error will be raised if the

    ’inplace_update’ function is called in a backend that doesn’t support inplace updates natively.

Return type:

None

Returns:

None

Examples

>>> set_inplace_mode('lenient')
>>> ivy.inplace_mode
'lenient'
>>> set_inplace_mode('strict')
>>> ivy.inplace_mode
'strict'

Note

Enabling strict mode can help users have more control over memory management but may lead to errors if the backend doesn’t support inplace updates natively.

ivy.set_item(x, query, val, /, *, copy=False)[source]#

Replace slices of x (defined by query) with val, identical to x[query] = val.

Parameters:
  • x (Union[Array, NativeArray]) – the array to be updated.

  • query (Union[Array, NativeArray, Tuple]) – either an index array, or a tuple of integers or slices.

  • val (Union[Array, NativeArray]) – the array containing the values to be infused into x

  • copy (Optional[bool], default: False) – boolean indicating whether to copy x. If True, the function will update and return a copy of x. If False, the function will update x inplace.

Return type:

Array

Returns:

ret – the array with updated values at the specified indices.

Examples

>>> x = ivy.array([0, -1, 20])
>>> query = ivy.array([0, 1])
>>> val = ivy.array([10, 10])
>>> ivy.set_item(x, query, val)
>>> print(x)
ivy.array([10, 10, 20])
>>> x = ivy.array([[0, -1, 20], [5, 2, -8]])
>>> query = ivy.array([1, 1])
>>> val = ivy.array([10, 10])
>>> y = ivy.set_item(x, query, val, copy=True)
>>> print(y)
ivy.array([[ 0, -1, 20],
       [10, 10, 10]])
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

Examples

Retrieve the minimum base >>> x = ivy.min_base >>> print(x) 1e-05

>>> # Set the minimum base to 1e-04:
>>> ivy.set_min_base(1e-04)

Retrieve the minimum base: >>> y = ivy.min_base >>> print(y) 1e-04

>>> # unset the min_base
>>> ivy.unset_min_base()
ivy.set_min_denominator(val)[source]#

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

Parameters:

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

Return type:

None

Examples

>>> x = ivy.min_denominator
>>> print(x)
1e-12
>>> ivy.set_min_denominator(1e-13)
>>> y = ivy.min_denominator
>>> print(y)
1e-13
ivy.set_nestable_mode(mode)[source]#

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

Return type:

None

Parameter#

mode

boolean whether to check if function inputs are ivy.Container

Examples

>>> ivy.set_nestable_mode(False)
>>> ivy.nestable_mode
False
>>> ivy.set_nestable_mode(True)
>>> ivy.nestable_mode
True
ivy.set_precise_mode(mode)[source]#

Set the mode of whether to use a promotion table that avoids any precision loss or a compute efficient table that avoids most wider-than- necessary promotions.

Return type:

None

Parameter#

mode

boolean whether to use high precision promotion table

Examples

>>> ivy.set_precise_mode(False)
>>> ivy.precise_mode
False
>>> ivy.set_precise_mode(True)
>>> ivy.precise_mode
True
ivy.set_queue_timeout(timeout)[source]#

Set a timeout value (in seconds) for the global queue.

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

Parameters:

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

Examples

>>> x = ivy.set_queue_timeout(10)
>>> x = ivy.queue_timeout
>>> print(x)
10.0
>>> 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.

Return type:

None

Parameter#

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
ivy.set_show_func_wrapper_trace_mode(mode)[source]#

Set the mode of whether to show the full stack trace with function wrapping traces.

Return type:

None

Parameter#

mode

boolean whether to perform ivy.Array conversions

Examples

>>> ivy.set_show_func_wrapper_trace_mode(False)
>>> ivy.show_func_wrapper_trace_mode
False
>>> ivy.set_show_func_wrapper_trace_mode(True)
>>> ivy.show_func_wrapper_trace_mode
True
ivy.set_tmp_dir(tmp_dr)[source]#

Set the directory for saving temporary files.

Parameters:

tmp_dr (str) – The new directory for saving temporary files

Return type:

None

Examples

>>> x = ivy.tmp_dir
>>> print(x)
/tmp
>>> ivy.set_tmp_dir("/my_tmp")
>>> y = ivy.tmp_dir
>>> print(y)
/my_tmp
>>> # Unset the tmp_dr
>>> ivy.unset_tmp_dir()
ivy.shape(x, /, *, as_array=False)[source]#

Return the shape of the array x.

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

  • as_array (bool, default: False) – Whether to return the shape as an array. Default is 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.size(x)[source]#

Return the number of elements of the array x.

Parameters:

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

Return type:

int

Returns:

  • ret – Number of elements of the array

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

>>> a = ivy.array([[[0, 0, 0], [0, 0, 0], [0, 0, 0]],
...                    [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
...                    [[0, 0, 0], [0, 0, 0], [0, 0, 0]]])
>>> b = ivy.size(a)
>>> print(b)
27

With ivy.Container input:

>>> a = ivy.Container(b = ivy.asarray([[0.,1.,1.],[1.,0.,0.],[8.,2.,3.]]))
>>> print(ivy.size(a))
{
    b: 9
}
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]) – The numerator of the division.

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

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

Return type:

Union[Number, Array]

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 ivy.min_base added to the base when exponent > 1 for numerical stability.

Parameters:
  • base (Union[Number, Array, NativeArray]) – The base number.

  • exponent (Union[Number, Array, NativeArray]) – The exponent number.

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

Return type:

Any

Returns:

ret – The new item following the numerically stable power.

Examples

With int input:

>>> x = ivy.stable_pow(2, 2)
>>> print(x)
ivy.array(4.00004)
>>> x = ivy.stable_pow(2, 2, min_base=2)
>>> print(x)
ivy.array(16)

With float input:

>>> x = ivy.stable_pow(4.0, .5)
>>> print(x)
ivy.array(2.00000262)

With complex input:

>>> x = ivy.stable_pow(3+4j, 2j)
>>> print(x)
ivy.array(-0.15605032-0.01208451j)

With ivy.Array input:

>>> x = ivy.asarray([[2, 4],
...                  [6, 8]])
>>> y = ivy.stable_pow(x, 2)
>>> print(y)
ivy.array([[ 4.00004, 16.00008],
       [36.00012, 64.00016]])
>>> x = ivy.asarray([2, 4, 6])
>>> y = ivy.asarray([2, 3, 4])
>>> z = ivy.stable_pow(x, y)
>>> print(z)
ivy.array([   4.00004,   64.00048, 1296.00864])

With ivy.Container input:

>>> x = ivy.Container(a=ivy.asarray([2, 4]), b=ivy.asarray([6, 8]))
>>> y = ivy.stable_pow(x, 2)
>>> print(y)
{
    a: ivy.array([4.00004, 16.00008]),
    b: ivy.array([36.00012, 64.00016])
}
>>> x = ivy.Container(a=ivy.asarray([2, 4]), b=ivy.asarray([6, 8]))
>>> y = ivy.Container(a=ivy.asarray([1, 3]), b=ivy.asarray([4, 5]))
>>> z = ivy.stable_pow(x, y)
>>> print(z)
{
    a: ivy.array([2.00001, 64.00048]),
    b: ivy.array([1296.00864, 32768.2048])
}
ivy.strides(x, /)[source]#

Return the input array’s strides across each dimension.

Parameters:

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

Return type:

Tuple[int]

Returns:

ret – A tuple containing the strides.

Examples

>>> x = ivy.array([[1, 5, 9], [2, 6, 10]])
>>> ivy.strides(x)
(4, 8)
ivy.supports_inplace_updates(x, /)[source]#

Return if in-place operations are supported for x’s data type.

Determine whether in-place operations are supported for x’s data type, by the current backend framework setting.

Parameters:

x (Union[Array, NativeArray]) – Input variable for whose data type we check whether the current backend framework supports in-place operations.

Return type:

bool

Returns:

ret – Value depends on whether in-place operations are supported for data type of x.

Raises:
  • IvyException – If x isn’t a class instance of ivy.Array or ivy.NativeArray, an exception will be raised.

  • This function is nestable, and therefore also accepts :code:'ivy.Container'

  • instance in place of the argument.

Examples

With ivy.Array input and default backend set as numpy:

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

With ivy.Container input and backend set as torch:

>>> x = ivy.Container(a=ivy.array([5., 6.]), b=ivy.array([7., 8.]))
>>> y = ivy.supports_inplace_updates(x)
>>> print(y)
{
    a: True,
    b: True
}

With ivy.Array input and backend set as “tensorflow”:

>>> x = ivy.array([1., 4.2, 2.2])
>>> ret = x.supports_inplace_updates()
>>> print(ret)
False
ivy.to_ivy_shape(shape)[source]#

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

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

Examples

With ivy.Array input:

>>> x = ivy.array([-1, 0, 1])
>>> y = ivy.to_list(x)
>>> print(y)
[-1, 0, 1]
>>> 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]]
>>> 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]]]

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]]]
}
ivy.to_native_shape(shape)[source]#

Return the input shape in its native backend framework form.

Parameters:

shape (Union[Array, Shape, NativeShape, tuple, int, list]) – The input to be converted

Return type:

NativeShape

Returns:

ret

the input in its native framework form

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

Convert an array into a numpy array.

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

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

Return type:

ndarray

Returns:

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

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.Container input:

>>> x = ivy.Container(a=ivy.array([-1, 0, 1]))
>>> y = ivy.to_numpy(x)
>>> print(y)
{
    a: 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)
}
ivy.to_scalar(x, /)[source]#

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

  • 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([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
}
>>> 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
}
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) – dictionary of keyword arguments

Return type:

Optional[Callable]

Returns:

Either the function itself or None if an exception was raised during function execution.

Examples

with a function that 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 a function that 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.

Return type:

None

Examples

>>> ivy.set_array_mode(False)
>>> ivy.array_mode
False
>>> ivy.unset_shape_array_mode()
>>> ivy.array_mode
True
ivy.unset_exception_trace_mode()[source]#

Reset the trace mode to the previously set mode.

Return type:

None

Examples

>>> ivy.set_exception_trace_mode("ivy")
>>> ivy.exception_trace_mode
'ivy'
>>> ivy.unset_exception_trace_mode()
>>> ivy.exception_trace_mode
'full'
ivy.unset_inplace_mode()[source]#

Reset the memory management behavior for in-place updates in Ivy to the previous state.

Return type:

None

Examples

>>> set_inplace_mode('strict')
>>> ivy.inplace_mode
'strict'
>>> unset_inplace_mode()
>>> ivy.inplace_mode
'lenient'
ivy.unset_min_base()[source]#

Reset the global minimum base used by ivy for numerically stable power raising to the previous value.

Return type:

None

Examples

>>> ivy.set_min_base(1e-07)
>>> y = ivy.min_base
>>> print(y)
1e-07
>>> ivy.unset_min_base()
>>> ivy.min_base
1e-05
ivy.unset_min_denominator()[source]#

Reset the global minimum denominator used by ivy for numerically stable division to the previous value.

Return type:

None

Examples

>>> ivy.set_min_denominator(1e-10)
>>> y = ivy.min_denominator
>>> print(y)
1e-10
>>> ivy.unset_min_denominator()
>>> ivy.min_denominator
1e-12
ivy.unset_nestable_mode()[source]#

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

Return type:

None

Examples

>>> ivy.set_nestable_mode(False)
>>> ivy.nestable_mode
False
>>> ivy.unset_nestable_mode()
>>> ivy.nestable_mode
True
ivy.unset_precise_mode()[source]#

Reset the mode of whether to use a promotion table that avoids any precision loss or a compute efficient table that avoids most wider-than- necessary promotions.

Return type:

None

Examples

>>> ivy.set_precise_mode(False)
>>> ivy.precise_mode
False
>>> ivy.unset_precise_mode()
>>> ivy.precise_mode
True
ivy.unset_queue_timeout()[source]#

Reset the global queue timeout value (in seconds) to the previous state.

Return type:

None

Examples

>>> ivy.set_queue_timeout(10.0)
>>> y = ivy.queue_timeout
>>> print(y)
10.0
>>> ivy.unset_queue_timeout()
>>> ivy.queue_timeout
15.0
ivy.unset_shape_array_mode()[source]#

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

Return type:

None

Examples

>>> ivy.set_shape_array_mode(True)
>>> ivy.shape_array_mode
True
>>> ivy.unset_shape_array_mode()
>>> ivy.shape_array_mode
False
ivy.unset_show_func_wrapper_trace_mode()[source]#

Reset the mode of whether to show the full stack trace with function wrapping traces.

Return type:

None

Examples

>>> ivy.set_show_func_wrapper_trace_mode(False)
>>> ivy.show_func_wrapper_trace_mode
False
>>> ivy.unset_show_func_wrapper_trace_mode()
>>> ivy.show_func_wrapper_trace_mode
True
ivy.unset_tmp_dir()[source]#

Reset the directory for saving temporary files to the previous value.

Return type:

None

Examples

>>> ivy.set_tmp_dir("/my_dir")
>>> y = ivy.tmp_dir
>>> print(y)
/my_dir
>>> ivy.unset_tmp_dir()
>>> ivy.tmp_dir
/tmp
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, default: True) – Whether to include infs and -infs in the check. Default is True.

Return type:

bool

Returns:

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

Examples

>>> x = ivy.array([451])
>>> y = ivy.value_is_nan(x)
>>> print(y)
False
>>> x = ivy.array([float('inf')])
>>> y = ivy.value_is_nan(x)
>>> print(y)
True
>>> x = ivy.array([float('inf')])
>>> y = ivy.value_is_nan(x, include_infs=False)
>>> print(y)
False
>>> x = ivy.array([float('nan')])
>>> y = ivy.value_is_nan(x, include_infs=False)
>>> print(y)
True
>>> x = ivy.array([0])
>>> y = ivy.value_is_nan(x)
>>> print(y)
False
ivy.vmap(func, in_axes=0, out_axes=0)[source]#

Vectorizing map. Creates a function which maps func over argument axes.

Parameters:
  • func (Callable) – Function to be mapped over additional axes.

  • in_axes (Union[int, Sequence[int], Sequence[None]], default: 0) – An integer, None, or (nested) standard Python container (tuple/list) thereof specifying which input array axes to map over.If each positional argument to fun is an array, then in_axes can be an integer, a None, or a tuple of integers and Nones with length equal to the number of positional arguments to fun. An integer or None indicates which array axis to map over for all arguments (with None indicating not to map any axis), and a tuple indicates which axis to map for each corresponding positional argument. Axis integers must be in the range [-ndim, ndim) for each array, where ndim is the number of dimensions (axes) of the corresponding input array.

  • out_axes (int, default: 0) – An integer indicating where the mapped axis should appear in the output.

Return type:

Callable

Returns:

ret – Batched/vectorized version of func with arguments that correspond to those of func, but with extra array axes at positions indicated by in_axes, and a return value that corresponds to that of fun, but with extra array axes at positions indicated by out_axes.

This docstring is a summarised version of the docstring for vmap from JAX documentation.

Examples

With ivy.matmul() and ivy.Array input:

>>> x = ivy.array(ivy.arange(60).reshape((3, 5, 4)))
>>> y = ivy.array(ivy.arange(40).reshape((5, 4, 2)))
>>> z = ivy.vmap(ivy.matmul, (1, 0), 1)(x, y)
>>> z.shape
(3, 5, 2)
class ivy.ArrayMode(array_mode)[source]#

Array Mode Context Manager.

class ivy.PreciseMode(precise_mode)[source]#

Precise Mode Context Manager.

This should have hopefully given you an overview of the general submodule, if you have any questions, please feel free to reach out on our discord!