inplace_update#

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])
}
Array.inplace_update(self, val, /, *, ensure_in_backend=False, keep_input_dtype=False)[source]#

ivy.Array instance method variant of ivy.inplace_update. This method simply wraps the function, and so the docstring for ivy.inplace_update also applies to this method with minimal changes.

Parameters:
  • self (Array) – input array to update

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

  • ensure_in_backend (bool, default: False) – Whether 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.

Examples

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

>>> x = ivy.array([1, 2, 3])
>>> y = ivy.array([0])
>>> x.inplace_update(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)
>>> x.inplace_update(y, keep_input_dtype=True)
>>> print(x)
ivy.array([0., 0., 0.])

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

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

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

>>> x = ivy.array([4, 5, 6])
>>> y = ivy.array([1])
>>> x.inplace_update(y)
IvyBackendException: jax: inplace_update: JAX does not natively
support inplace updates
Container.inplace_update(self, val, /, *, ensure_in_backend=False, keep_input_dtype=False, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

ivy.Container instance method variant of ivy.inplace_update. This method simply wraps the function, and so the docstring for ivy.inplace_update also applies to this method with minimal changes.

Parameters:
  • self (Container) – input container to be updated inplace

  • val (Union[Container, Array, NativeArray]) – value to update the input container with

  • ensure_in_backend (Union[bool, Container], default: False) – Whether 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 (Union[bool, Container], default: False) – Whether or not to preserve x data type after the update, otherwise val data type will be applied. Defaults to False.

  • key_chains (Optional[Union[List[str], Dict[str, str], Container]], default: None) – The key-chains to apply or not apply the method to. Default is None.

  • to_apply (Union[bool, Container], default: True) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default is True.

  • prune_unapplied (Union[bool, Container], default: False) – Whether to prune key_chains for which the function was not applied. Default is False.

  • map_sequences (Union[bool, Container], default: False) – Whether to also map method to sequences (lists, tuples). Default is False.

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

Return type:

Container

Returns:

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

Examples

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

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