# sign#

ivy.sign(x, /, *, np_variant=True, out=None)[source]#

Return an indication of the sign of a number for each element x_i of the input array x.

The sign function (also known as the signum function) of a number $$x_{i}$$ is defined as

$\begin{split}\operatorname{sign}(x_i) = \begin{cases} 0 & \textrm{if } x_i = 0 \\ \frac{x}{|x|} & \textrm{otherwise} \end{cases}\end{split}$

where $$|x_i|$$ is the absolute value of $$x_i$$.

Special cases

• If x_i is less than 0, the result is -1.

• If x_i is either -0 or +0, the result is 0.

• If x_i is greater than 0, the result is +1.

• For complex numbers sign(x.real) + 0j if x.real != 0 else sign(x.imag) + 0j

For complex floating-point operands, let a = real(x_i), b = imag(x_i), and

• If a is either -0 or +0 and b is either -0 or +0, the result is 0 + 0j.

• If a is NaN or b is NaN, the result is NaN + NaN j.

• In the remaining cases, special cases must be handled according to the rules of complex number division.

Parameters:
• x (Union[Array, NativeArray]) – input array. Should have a numeric data type.

• np_variant (Optional[bool], default: True) – Handles complex numbers like numpy does If True, sign(x.real) + 0j if x.real != 0 else sign(x.imag) + 0j. otherwise, For complex numbers, y = sign(x) = x / |x| if x != 0, otherwise y = 0.

• 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 – an array containing the evaluated result for each element in x. The returned array must have the same data type as x.

This function conforms to the Array API Standard. This docstring is an extension of the docstring in the standard.

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([8.3, -0, 6.8, 0.07])
>>> y = ivy.sign(x)
>>> print(y)
ivy.array([1., 0., 1., 1.])

>>> x = ivy.array([[5.78, -4., -6.9, 0],
...                [-.4, 0.5, 8, -0.01]])
>>> y = ivy.sign(x)
>>> print(y)
ivy.array([[ 1., -1., -1.,  0.],
[-1.,  1.,  1., -1.]])


With ivy.Container input:

>>> x = ivy.Container(a=ivy.array([0., -0.]),
...                   b=ivy.array([1.46, 5.9, -0.0]),
...                   c=ivy.array([-8.23, -4.9, -2.6, 7.4]))
>>> y = ivy.sign(x)
>>> print(y)
{
a: ivy.array([0., 0.]),
b: ivy.array([1., 1., 0.]),
c: ivy.array([-1., -1., -1., 1.])
}

Array.sign(self, *, np_variant=True, out=None)[source]#

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

Parameters:
• self (Array) – input array. Should have a numeric data type.

• 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 – an array containing the evaluated result for each element in self. The returned array must have the same data type as self.

Examples

>>> x = ivy.array([5.7, -7.1, 0, -0, 6.8])
>>> y = x.sign()
>>> print(y)
ivy.array([ 1., -1.,  0.,  0.,  1.])

>>> x = ivy.array([-94.2, 256.0, 0.0001, -0.0001, 36.6])
>>> y = x.sign()
>>> print(y)
ivy.array([-1.,  1.,  1., -1.,  1.])

>>> x = ivy.array([[ -1., -67.,  0.,  15.5,  1.], [3, -45, 24.7, -678.5, 32.8]])
>>> y = x.sign()
>>> print(y)
ivy.array([[-1., -1.,  0.,  1.,  1.],
[ 1., -1.,  1., -1.,  1.]])

Container.sign(self, *, np_variant=True, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#

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

Parameters:
• self (Container) – input container. Should have a numeric data type.

• 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 – a container containing the evaluated result for each element in self. The returned container must have the same data type as self.

Examples

>>> x = ivy.Container(a=ivy.array([-6.7, 2.4, -8.5]),
...                   b=ivy.array([1.5, -0.3, 0]),
...                   c=ivy.array([-4.7, -5.4, 7.5]))
>>> y = x.sign()
>>> print(y)
{
a: ivy.array([-1., 1., -1.]),
b: ivy.array([1., -1., 0.]),
c: ivy.array([-1., -1., 1.])
}