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

}

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

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], 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.

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([2, 7, 2, 3, 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)[source]#

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

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

  • 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 container, for writing the result to. It must have a shape 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([2, 0, 2, 8, 0, 0, 0, 0]), b: ivy.array([2, 7, 2, 3, 0, 0, 0, 0])

}