abs¶
- abs(x: array, /) array¶
Calculates the absolute value for each element
x_iof the input arrayx.For real-valued input arrays, the element-wise result has the same magnitude as the respective element in
xbut 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. Ifxhas a real-valued data type, the returned array must have the same data type asx. Ifxhas 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., ifxiscomplex128, then the returned array must have afloat64data type).
Notes
Special Cases
For real-valued floating-point operands,
If
x_iisNaN, the result isNaN.If
x_iis-0, the result is+0.If
x_iis-infinity, the result is+infinity.
For complex floating-point operands, let
a = real(x_i),b = imag(x_i), andIf
ais either+infinityor-infinityandbis any value (includingNaN), the result is+infinity.If
ais any value (includingNaN) andbis either+infinityor-infinity, the result is+infinity.If
ais either+0or-0, the result is equal toabs(b).If
bis either+0or-0, the result is equal toabs(a).If
aisNaNandbis a finite number, the result isNaN.If
ais a finite number andbisNaN, the result isNaN.If
aisNaNandbisNaN, the result isNaN.
Changed in version 2022.12: Added complex data type support.