xp_sign

xp_sign(x: Any, /, *, xp: ModuleType | None = None) Any[source][source]

Return the sign of each element in the input array.

Parameters:
  • x (Array) – The input array.

  • xp (module, optional) –

    The array API namespace to use. If not provided, the namespace is

    inferred from the array.

Returns:

An array with the same shape as x, where each element has the sign of

the corresponding element in x. The sign is defined as:

  • 1 for positive values

  • 0 for zero

  • -1 for negative values

  • NaN for NaN values

Return type:

Array

Notes

This is a partial substitute for xp.sign, which does not cover the NaN
special case in some array API implementations. See

https://github.com/data-apis/array-api-compat/issues/136 for more details

Examples

>>> import numpy as np
>>> x = np.array([-5.0, 0.0, 3.0, np.nan])
>>> xp_sign(x, xp=np)
array([-1.,  0.,  1., nan])
>>> # Equivalent to NumPy's sign
>>> np.sign(x)
array([-1.,  0.,  1., nan])