abs¶
- abs(x: array, /) array ¶
Calculates the absolute value for each element
x_i
of the input arrayx
.For real-valued input arrays, the element-wise result has the same magnitude as the respective element in
x
but has positive sign.Note
For signed integer data types, the absolute value of the minimum representable integer is implementation-dependent.
Note
For complex floating-point operands, the complex absolute value is known as the norm, modulus, or magnitude and, for a complex number \(z = a + bj\) is computed as
\[\operatorname{abs}(z) = \sqrt{a^2 + b^2}\]Note
For complex floating-point operands, conforming implementations should take care to avoid undue overflow or underflow during intermediate stages of computation.
- Parameters:
x (array) – input array. Should have a numeric data type.
- Returns:
out (array) – an array containing the absolute value of each element in
x
. Ifx
has a real-valued data type, the returned array must have the same data type asx
. Ifx
has a complex floating-point data type, the returned array must have a real-valued floating-point data type whose precision matches the precision ofx
(e.g., ifx
iscomplex128
, then the returned array must have afloat64
data type).
Notes
Special Cases
For real-valued floating-point operands,
If
x_i
isNaN
, the result isNaN
.If
x_i
is-0
, the result is+0
.If
x_i
is-infinity
, the result is+infinity
.
For complex floating-point operands, let
a = real(x_i)
,b = imag(x_i)
, andIf
a
is either+infinity
or-infinity
andb
is any value (includingNaN
), the result is+infinity
.If
a
is any value (includingNaN
) andb
is either+infinity
or-infinity
, the result is+infinity
.If
a
is either+0
or-0
, the result is equal toabs(b)
.If
b
is either+0
or-0
, the result is equal toabs(a)
.If
a
isNaN
andb
is a finite number, the result isNaN
.If
a
is a finite number andb
isNaN
, the result isNaN
.If
a
isNaN
andb
isNaN
, the result isNaN
.
Changed in version 2022.12: Added complex data type support.