abs#
- ivy.abs(x, /, *, out=None)[source]#
Calculate the absolute value for each element
x_i
of the input arrayx
(i.e., the element-wise result has the same magnitude as the respective element inx
but has positive sign).Note
For signed integer data types, the absolute value of the minimum representable integer is implementation-dependent.
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)
andb = 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+infinity
, the result is+infinity
.If
a
is either+0
or-0
, the result isabs(b)
.If
b
is+0
or-0
, the result isabs(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
isNa``N and ``b
isNaN
, the result isNaN
.
- Parameters:
- Return type:
- Returns:
ret – an array containing the absolute value of each element in
x
. The returned array must have the same data type asx
.
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 argumentsExamples
With
ivy.Array
input:>>> x = ivy.array([-1,0,-6]) >>> y = ivy.abs(x) >>> print(y) ivy.array([1, 0, 6])
>>> x = ivy.array([3.7, -7.7, 0, -2, -0]) >>> y = ivy.abs(x) >>> print(y) ivy.array([ 3.7, 7.7, 0., 2., 0.])
>>> x = ivy.array([[1.1, 2.2, 3.3], [-4.4, -5.5, -6.6]]) >>> ivy.abs(x, out=x) >>> print(x) ivy.array([[ 1.1, 2.2, 3.3], [4.4, 5.5, 6.6]])
With
ivy.Container
input:>>> x = ivy.Container(a=ivy.array([0., 2.6, -3.5]), b=ivy.array([4.5, -5.3, -0, -2.3])) # noqa >>> y = ivy.abs(x) >>> print(y) { a: ivy.array([0., 2.6, 3.5]), b: ivy.array([4.5, 5.3, 0., 2.3]) }
- Array.abs(self, /, *, out=None)[source]#
ivy.Array instance method variant of ivy.abs. This method simply wraps the function, and so the docstring for ivy.abs also applies to this method with minimal changes.
- Parameters:
self (
Union
[float
,Array
,NativeArray
]) – 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 absolute value of each element in
self
. The returned array must have the same data type asself
.
Examples
>>> x = ivy.array([2.6, -6.6, 1.6, -0]) >>> y = x.abs() >>> print(y) ivy.array([ 2.6, 6.6, 1.6, 0.])
- Container.abs(self, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#
ivy.Container instance method variant of ivy.abs. This method simply wraps the function, and so the docstring for ivy.abs 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 isNone
.to_apply (
Union
[bool
,Container
], default:True
) – If True, the method will be applied to key_chains, otherwise key_chains will be skipped. Default isTrue
.prune_unapplied (
Union
[bool
,Container
], default:False
) – Whether to prune key_chains for which the function was not applied. Default isFalse
.map_sequences (
Union
[bool
,Container
], default:False
) – Whether to also map method to sequences (lists, tuples). Default isFalse
.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 absolute value of each element in
self
. The returned container must have the same data type asself
.
Examples
>>> x = ivy.Container(a=ivy.array([-1.6, 2.6, -3.5]), ... b=ivy.array([4.5, -5.3, -2.3])) >>> y = x.abs() >>> print(y) { a: ivy.array([1.6, 2.6, 3.5]), b: ivy.array([4.5, 5.3, 2.3]) }