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

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', 'compile_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:

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

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

>>> x = ivy.array([1, 2, 3])
>>> print(x.assert_supports_inplace())
IvyBackendException: jax: assert_supports_inplace: Inplace operations     are not supported <class 'jaxlib.xla_extension.DeviceArray'> types with jax backend

With ivy.Container input and default backend set as 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 jax:

>>> x = ivy.Container(a=ivy.array([5, 6]), b=ivy.array([7, 8]))
>>> print(x.assert_supports_inplace())
IvyBackendException: jax: assert_supports_inplace: Inplace operations     are not supported <class 'jaxlib.xla_extension.DeviceArray'> types with jax backend
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)) # Compute the output
8
>>> print(cached_sum(10, 34)) # Compute the output
44
>>> print(cached_sum(3, 5)) # Returns the cached value
8
>>> print(cached_sum(5, 3)) # Compute the output
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
>>> print(cached_line_eq(3, slp=2, itc=5)) # Returns the cached value
11

Note: providing keyword arguments by position, or using the default keyword argument values will prevent the cache from being used.

>>> print(cached_line_eq(5, slp=2)) # Output is re-computed
10
>>> print(cached_line_eq(5)) # Output is re-computed
10
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: 2.0) Default is 2.

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

Return type:

Array

Returns:

ret – An array 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, 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) – The p-value for computing the p-norm. (default: 2.0) Default is 2.

  • out (Optional[Array]) – optional output array, for writing the result to. (default: None) 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.

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, p=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, p=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, p=3.0, out=x)
>>> print(x)
ivy.array([[ 0.131,  0.263,  0.394],
           [-0.526, -0.657, -0.788]])

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

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

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

Return type:

Any

Returns:

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

Functional 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]) – 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.

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)
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) >>> images = ivy.asarray([ivy.random_normal(shape=(30, 40, 3)) for _ in range(32)])

Concatenate images along height (vertical axis), 960 = 32 * 30 >>> 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 >>> 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 >>> 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 >>> 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 >>> 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 >>> 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]) – 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.

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

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

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

Return type:

Dict

Returns:

ret – The unsupported devices 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.

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

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

Return type:

Dict

Returns:

ret – The unsupported combination of 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) – optional int, the axis from which to gather from. (default: -1) Default is -1.

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

  • out (Optional[Union[Array, NativeArray]]) – optional array, for writing the result to. It must have a shape (default: None) 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.array([[0., 0.],[0., 0.]])
>>> 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]])
>>> ivy.gather(x, y, axis=0, out=x)
>>> print(x)
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) – optional int, lets you gather different items from each element of a batch. (default: 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:

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_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_exception_trace_mode()[source]#

Get the current state of exception_trace_mode.

Examples

>>> ivy.set_exception_trace_mode("full")
>>> ivy.get_exception_trace_mode()
'full'
Return type:

str

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.

Return type:

Array

Returns:

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

Functional 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_min_base()[source]#

Get 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 – The value 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_exception_trace_mode()
True
>>> ivy.set_nestable_mode(False)
>>> ivy.get_exception_trace_mode()
False
Return type:

bool

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) – Whether to return the shape as a array, default False. (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.]]))
>>> ivy.get_num_dims(a)
2
>>> b = ivy.get_num_dims(a, as_array=True)
>>> print(b)
ivy.array(3)
ivy.get_precise_mode()[source]#

Get the current state of precise_mode.

Examples

>>> ivy.get_precise_mode()
True
>>> ivy.set_precise_mode(False)
>>> ivy.get_precise_mode()
False
Return type:

bool

ivy.get_queue_timeout()[source]#

Get the global queue timeout value (in seconds).

The default value without this function being called is 15 seconds.

Return type:

float

Returns:

ret – The global queue timeout value (in seconds).

Examples

>>> ivy.set_queue_timeout(10.0)
>>> y = ivy.get_queue_timeout()
>>> print(y)
10.0
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.get_show_func_wrapper_trace_mode()[source]#

Get the current state of whether to show the full stack trace with function wrapping traces. Default is True (function wrapping traces are shown)

Examples

>>> ivy.get_show_func_wrapper_trace_mode()
True
>>> ivy.set_show_func_wrapper_trace_mode(False)
>>> ivy.get_show_func_wrapper_trace_mode()
False
Return type:

bool

ivy.get_tmp_dir()[source]#

Get the path for directory that saves temporary files.

Returns:

ret – The path of directory that saves temporary files.

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: True) 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, 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) – 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.

  • keep_input_dtype (bool) – Whether or not to preserve x data type after the update, otherwise val (default: False) 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:

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

>>> 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, x.dtype)
ivy.array([0., 0., 0.]) float32

With ivy.Container instances:, and backend set as 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]),
    b: ivy.array([2])
}

With mix of ivy.Array and ivy.Container instances:, and backend set as 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) – 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])
>>> 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]) – 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 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) – 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 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) – If True, assumes both elements and test_elements contain unique elements, (default: False) which can speed up the calculation. Default value is False.

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

Return type:

Array

Returns:

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

Examples

>>> x = ivy.array([[10, 7, 4], [3, 2, 1]])
>>> y = ivy.array([1, 2, 3])
>>> ivy.isin(x, y)
ivy.array([[False, False, False], [ True,  True,  True]])
>>> x = ivy.array([3, 2, 1, 0])
>>> y = ivy.array([1, 2, 3])
>>> ivy.isin(x, y, invert=True)
ivy.array([False, False, False,  True])
ivy.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) – Whether to allow one keyword argument to be used for multiple receivers. (default: False) 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]) – 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]#

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

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

  • 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 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([4, 9, 5, 9, 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([4, 9, 5, 9, 0, 0, 0, 0]), b: ivy.array([3, 12, 5, 4, 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([4, 9, 5, 9, 0, 0, 0, 0]), b: ivy.array([8, 7, 5, 4, 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[Shape, NativeShape]]) – The shape of the result. Default is None, in which case tensor (default: None) argument must be provided.

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

  • 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 of given shape, with the values scattered at the indices.

Examples

scatter values into an empty array, With ivy.Array input:

>>> indices = ivy.array([[4], [3], [1], [7]])
>>> updates = ivy.array([9, 10, 11, 12])
>>> shape = ivy.array([8])
>>> scatter = ivy.scatter_nd(indices, updates, shape)
>>> print(scatter)
ivy.array([ 0, 11,  0, 10,  9,  0,  0, 12])

With scatter into an empty array, 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, reduction='replace')
>>> 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.

Parameter#

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

None

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.

Parameter#

mode

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

Examples

>>> ivy.set_exception_trace_mode("ivy")
>>> ivy.get_exception_trace_mode()
'ivy'
>>> ivy.set_exception_trace_mode("full")
>>> ivy.get_exception_trace_mode()
'full'
rtype:

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.

Examples

>>> x = ivy.get_min_base()
>>> print(x)
1e-05
>>> ivy.set_min_base(1e-04)
>>> y = ivy.get_min_base()
>>> print(y)
1e-04
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 value to set the global minimum denominator to.

Examples

>>> x = ivy.get_min_denominator()
>>> print(x)
1e-12
>>> ivy.set_min_denominator(1e-13)
>>> y = ivy.get_min_denominator()
>>> print(y)
1e-13
Return type:

None

ivy.set_nestable_mode(mode)[source]#

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

Parameter#

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

None

ivy.set_precise_mode(mode)[source]#

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

Parameter#

mode

boolean whether to use high precision promtion table

Examples

>>> ivy.set_precise_mode(False)
>>> ivy.get_precise_mode()
False
>>> ivy.set_precise_mode(True)
>>> ivy.get_precise_mode()
True
rtype:

None

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.get_queue_timeout()
>>> print(x)
10.0
>>> ivy.set_queue_timeout(30)
>>> y = ivy.get_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.

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

None

ivy.set_show_func_wrapper_trace_mode(mode)[source]#

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

Parameter#

mode

boolean whether to perform ivy.Array conversions

Examples

>>> ivy.set_show_func_wrapper_trace_mode(False)
>>> ivy.get_show_func_wrapper_trace_mode()
False
>>> ivy.set_show_func_wrapper_trace_mode(True)
>>> ivy.get_show_func_wrapper_trace_mode()
True
rtype:

None

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

Examples

>>> x = ivy.get_tmp_dir()
>>> print(x)
/tmp
>>> ivy.set_tmp_dir("/my_tmp")
>>> y = ivy.get_tmp_dir()
>>> print(y)
/my_tmp
Return type:

None

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) – Whether to return the shape as an array. (default: False) 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.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]) – The numerator of the division.

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

  • min_denominator (Optional[Union[Number, Array, NativeArray]]) – The minimum denominator to use, use global ivy._MIN_DENOMINATOR (1e-12) (default: None) 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 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]) – 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 power.

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) – whether to copy the array to a new address or not. (default: True) 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.

Functional 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) – 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 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.

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_exception_trace_mode()[source]#

Reset the trace mode to the previously set mode.

Examples

>>> ivy.set_exception_trace_mode("ivy")
>>> ivy.get_exception_trace_mode()
'ivy'
>>> ivy.unset_exception_trace_mode()
>>> ivy.get_exception_trace_mode()
'full'
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_precise_mode()[source]#

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

Examples

>>> ivy.set_precise_mode(False)
>>> ivy.get_precise_mode()
False
>>> ivy.unset_precise_mode()
>>> ivy.get_array_mode()
True
Return type:

None

ivy.unset_queue_timeout()[source]#

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

Examples

>>> ivy.set_queue_timeout(10.0)
>>> y = ivy.get_queue_timeout()
>>> print(y)
10.0
>>> ivy.unset_shape_array_mode()
>>> ivy.get_queue_timeout()
15.0
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.unset_show_func_wrapper_trace_mode()[source]#

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

Examples

>>> ivy.set_show_func_wrapper_trace_mode(False)
>>> ivy.get_show_func_wrapper_trace_mode()
False
>>> ivy.unset_show_func_wrapper_trace_mode()
>>> ivy.get_show_func_wrapper_trace_mode()
True
Return type:

None

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: True) 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]]) – An integer, None, or (nested) standard Python container (default: 0) (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) – An integer indicating where the mapped axis should appear in the output. (default: 0)

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. # noqa

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)
>>> print(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 in the general channel or in the general forum!