atan2#
- ivy.atan2(x1, x2, /, *, out=None)[source]#
Calculate an implementation-dependent approximation of the inverse tangent of the quotient
x1/x2
, having domain[-infinity, +infinity] x. [-infinity, +infinity]
(where thex
notation denotes the set of ordered pairs of elements(x1_i, x2_i)
) and codomain[-π, +π]
, for each pair of elements(x1_i, x2_i)
of the input arraysx1
andx2
, respectively. Each element-wise result is expressed in radians. The mathematical signs ofx1_i and x2_i
determine the quadrant of each element-wise result. The quadrant (i.e., branch) is chosen such that each element-wise result is the signed angle in radians between the ray ending at the origin and passing through the point(1,0)
and the ray ending at the origin and passing through the point(x2_i, x1_i)
.Special cases
For floating-point operands,
If either
x1_i
orx2_i
isNaN
, the result isNaN
.If
x1_i
is greater than0
andx2_i
is+0
, the result is an approximation to+π/2
.If
x1_i
is greater than0
andx2_i
is-0
, the result is an approximation to+π/2
.If
x1_i
is+0
andx2_i
is greater than0
, the result is+0
.If
x1_i
is+0
andx2_i
is+0
, the result is+0
.If
x1_i
is+0
andx2_i
is-0
, the result is an approximation to+π
.If
x1_i
is+0
andx2_i
is less than 0, the result is an approximation to+π
.If
x1_i
is-0
andx2_i
is greater than0
, the result is-0
.If
x1_i
is-0
andx2_i
is+0
, the result is-0
.If
x1_i
is-0
andx2_i
is-0
, the result is an approximation to-π
.If
x1_i
is-0
andx2_i
is less than0
, the result is an approximation to-π
.If
x1_i
is less than0
andx2_i
is+0
, the result is an approximation to-π/2
.If
x1_i
is less than0
andx2_i
is-0
, the result is an approximation to-π/2
.If
x1_i
is greater than0
,x1_i
is a finite number, andx2_i
is+infinity
, the result is+0
.If
x1_i
is greater than0
,x1_i
is a finite number, andx2_i
is-infinity
, the result is an approximation to+π
.If
x1_i
is less than0
,x1_i
is a finite number, andx2_i
is+infinity
, the result is-0
.If
x1_i
is less than0
,x1_i
is a finite number, andx2_i
is-infinity
, the result is an approximation to-π
.If
x1_i
is+infinity
andx2_i
is finite, the result is an approximation to+π/2
.If
x1_i
is-infinity
andx2_i
is finite, the result is an approximation to-π/2
.If
x1_i
is+infinity
andx2_i
is+infinity
, the result is an approximation to+π/4
.If
x1_i
is+infinity
andx2_i
is-infinity
, the result is an approximation to+3π/4
.If
x1_i
is-infinity
andx2_i
is+infinity
, the result is an approximation to-π/4
.If
x1_i
is-infinity
andx2_i
is-infinity
, the result is an approximation to-3π/4
.
- Parameters:
x1 (
Union
[Array
,NativeArray
]) – input array corresponding to the y-coordinates. Should have a floating-point data type.x2 (
Union
[Array
,NativeArray
]) – input array corresponding to the x-coordinates. Must be compatible withx1
. Should have a floating-point 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:
- Returns:
ret – an array containing the inverse tangent of the quotient
x1/x2
. The returned array must have a floating-point data type.
This method 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([1.0, -1.0, -2.0]) >>> y = ivy.array([2.0, 0.0, 3.0]) >>> z = ivy.atan2(x, y) >>> print(z) ivy.array([ 0.464, -1.57 , -0.588])
>>> x = ivy.array([1.0, 2.0]) >>> y = ivy.array([-2.0, 3.0]) >>> z = ivy.zeros(2) >>> ivy.atan2(x, y, out=z) >>> print(z) ivy.array([2.68 , 0.588])
>>> nan = float("nan") >>> x = ivy.array([nan, 1.0, 1.0, -1.0, -1.0]) >>> y = ivy.array([1.0, +0, -0, +0, -0]) >>> z = ivy.atan2(x, y) >>> print(z) ivy.array([ nan, 1.57, 1.57, -1.57, -1.57])
>>> x = ivy.array([+0, +0, +0, +0, -0, -0, -0, -0]) >>> y = ivy.array([1.0, +0, -0, -1.0, 1.0, +0, -0, -1.0]) >>> z = ivy.atan2(x, y) >>> print(z) ivy.array([0. , 0. , 0. , 3.14, 0. , 0. , 0. , 3.14])
>>> inf = float("infinity") >>> x = ivy.array([inf, -inf, inf, inf, -inf, -inf]) >>> y = ivy.array([1.0, 1.0, inf, -inf, inf, -inf]) >>> z = ivy.atan2(x, y) >>> print(z) ivy.array([ 1.57 , -1.57 , 0.785, 2.36 , -0.785, -2.36 ])
>>> x = ivy.array([2.5, -1.75, 3.2, 0, -1.0]) >>> y = ivy.array([-3.5, 2, 0, 0, 5]) >>> z = ivy.atan2(x, y) >>> print(z) ivy.array([ 2.52 , -0.719, 1.57 , 0. , -0.197])
>>> x = ivy.array([[1.1, 2.2, 3.3], [-4.4, -5.5, -6.6]]) >>> y = ivy.atan2(x, x) >>> print(y) ivy.array([[ 0.785, 0.785, 0.785], [-2.36 , -2.36 , -2.36 ]])
With
ivy.Container
input:>>> x = ivy.Container(a=ivy.array([0., 2.6, -3.5]), ... b=ivy.array([4.5, -5.3, -0])) >>> y = ivy.array([3.0, 2.0, 1.0]) >>> z = ivy.atan2(x, y) { a: ivy.array([0., 0.915, -1.29]), b: ivy.array([0.983, -1.21, 0.]) }
>>> x = ivy.Container(a=ivy.array([0., 2.6, -3.5]), ... b=ivy.array([4.5, -5.3, -0, -2.3])) >>> y = ivy.Container(a=ivy.array([-2.5, 1.75, 3.5]), ... b=ivy.array([2.45, 6.35, 0, 1.5])) >>> z = ivy.atan2(x, y) >>> print(z) { a: ivy.array([3.14, 0.978, -0.785]), b: ivy.array([1.07, -0.696, 0., -0.993]) }
- Array.atan2(self, x2, /, *, out=None)[source]#
ivy.Array instance method variant of ivy.atan2. This method simply wraps the function, and so the docstring for ivy.atan2 also applies to this method with minimal changes.
- Parameters:
self (
Array
) – first input array corresponding to the y-coordinates. Should have a real-valued floating-point data type.x2 (
Union
[Array
,NativeArray
]) – second input array corresponding to the x-coordinates. Must be compatible with ``self``(see broadcasting). Should have a real-valued floating-point 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 inverse tangent of the quotient
self/x2
. The returned array must have a real-valued floating-point data type determined by type-promotion.
Examples
>>> x = ivy.array([1.0, 0.5, 0.0, -0.5, 0.0]) >>> y = ivy.array([1.0, 2.0, -1.5, 0, 1.0]) >>> z = x.atan2(y) >>> print(z) ivy.array([ 0.785, 0.245, 3.14 , -1.57 , 0. ])
>>> x = ivy.array([1.0, 2.0]) >>> y = ivy.array([-2.0, 3.0]) >>> z = ivy.zeros(2) >>> x.atan2(y, out=z) >>> print(z) ivy.array([2.68 , 0.588])
>>> nan = float("nan") >>> x = ivy.array([nan, 1.0, 1.0, -1.0, -1.0]) >>> y = ivy.array([1.0, +0, -0, +0, -0]) >>> x.atan2(y) ivy.array([ nan, 1.57, 1.57, -1.57, -1.57])
>>> x = ivy.array([+0, +0, +0, +0, -0, -0, -0, -0]) >>> y = ivy.array([1.0, +0, -0, -1.0, 1.0, +0, -0, -1.0]) >>> x.atan2(y) ivy.array([0. , 0. , 0. , 3.14, 0. , 0. , 0. , 3.14]) >>> y.atan2(x) ivy.array([ 1.57, 0. , 0. , -1.57, 1.57, 0. , 0. , -1.57])
>>> inf = float("infinity") >>> x = ivy.array([inf, -inf, inf, inf, -inf, -inf]) >>> y = ivy.array([1.0, 1.0, inf, -inf, inf, -inf]) >>> z = x.atan2(y) >>> print(z) ivy.array([ 1.57 , -1.57 , 0.785, 2.36 , -0.785, -2.36 ])
>>> x = ivy.array([2.5, -1.75, 3.2, 0, -1.0]) >>> y = ivy.array([-3.5, 2, 0, 0, 5]) >>> z = x.atan2(y) >>> print(z) ivy.array([ 2.52 , -0.719, 1.57 , 0. , -0.197])
>>> x = ivy.array([[1.1, 2.2, 3.3], [-4.4, -5.5, -6.6]]) >>> y = x.atan2(x) >>> print(y) ivy.array([[ 0.785, 0.785, 0.785], [-2.36 , -2.36 , -2.36 ]])
- Container.atan2(self, x2, /, *, key_chains=None, to_apply=True, prune_unapplied=False, map_sequences=False, out=None)[source]#
ivy.Container instance method variant of ivy.atan2. This method simply wraps the function, and so the docstring for ivy.atan2 also applies to this method with minimal changes.
- Parameters:
self (
Container
) – first input array or container corresponding to the y-coordinates. Should have a real-valued floating-point data type.x2 (
Union
[Container
,Array
,NativeArray
]) – second input array or container corresponding to the x-coordinates. Must be compatible withself
(see broadcasting). Should have a real-valued floating-point 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 inverse tangent of the quotient
self/x2
. The returned array must have a real-valued floating-point data type determined by type-promotion.
Examples
>>> x = ivy.Container(a=ivy.array([0., 2.6, -3.5]), ... b=ivy.array([4.5, -5.3, -0])) >>> y = ivy.array([3.0, 2.0, 1.0]) >>> x.atan2(y) { a: ivy.array([0., 0.915, -1.29]), b: ivy.array([0.983, -1.21, 0.]) }
>>> x = ivy.Container(a=ivy.array([0., 2.6, -3.5]), ... b=ivy.array([4.5, -5.3, -0, -2.3])) >>> y = ivy.Container(a=ivy.array([-2.5, 1.75, 3.5]), ... b=ivy.array([2.45, 6.35, 0, 1.5])) >>> z = x.atan2(y) >>> print(z) { a: ivy.array([3.14, 0.978, -0.785]), b: ivy.array([1.07, -0.696, 0., -0.993]) }