to_scalar#

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
}
Array.to_scalar(self)[source]#

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

Parameters:

self (Array) – input array.

Return type:

Number

Returns:

ret – a scalar copying the element of the array x.

Examples

With ivy.Array instance method:

>>> x = ivy.array([3])
>>> y = x.to_scalar()
>>> print(y)
3
Container.to_scalar(self, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False)[source]#

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

Parameters:
  • self (Container) – input container.

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

Return type:

Container

Returns:

ret – a container of scalar values copying all the element of the container self.

Examples

With one ivy.Container instance:

>>> x = ivy.Container(a=ivy.array([1]), b=ivy.array([0]),
...                   c=ivy.array([-1]))
>>> y = x.to_scalar()
>>> print(y)
{
    a: 1,
    b: 0,
    c: -1
}