scatter_flat#

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

}

Array.scatter_flat(self, updates, /, *, size=None, reduction='sum', out=None)#

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

Parameters:
  • self (Array) – input array containing the indices where the new values will 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 (default: 'sum') ‘replace’

  • out (Optional[Array]) – optional output array, for writing the result to. It must have a shape that (default: None) 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([0, 0, 1, 0, 2, 2, 3, 3]) >>> updates = ivy.array([5, 1, 7, 2, 3, 2, 1, 3]) >>> size = 8 >>> out = indices.scatter_flat(updates, size=size) >>> print(out) ivy.array([8, 7, 5, 4, 0, 0, 0, 0])

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]) >>> indices.scatter_flat(updates, out=out) >>> print(out) ivy.array([8, 7, 5, 4, 0, 0, 0, 0])

Container.scatter_flat(self, updates, /, *, size=None, reduction='sum', key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)#

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

Parameters:
  • self (Container) – Index array or container.

  • updates (Union[Array, NativeArray, Container]) – values to update input tensor with

  • 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’ (default: 'sum') or ‘replace’

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

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

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

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

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

Return type:

Container

Returns:

ret – New container of given shape, with the values updated at the indices.

Examples

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

}