SUPPORT THE WORK

GetWiki

atan2

ARTICLE SUBJECTS
aesthetics  →
being  →
complexity  →
database  →
enterprise  →
ethics  →
fiction  →
history  →
internet  →
knowledge  →
language  →
licensing  →
linux  →
logic  →
method  →
news  →
perception  →
philosophy  →
policy  →
purpose  →
religion  →
science  →
sociology  →
software  →
truth  →
unix  →
wiki  →
ARTICLE TYPES
essay  →
feed  →
help  →
system  →
wiki  →
ARTICLE ORIGINS
critical  →
discussion  →
forked  →
imported  →
original  →
atan2
[ temporary import ]
please note:
- the content below is remote from Wikipedia
- it has been imported raw for GetWiki
{{Short description|Arctangent function with two arguments}}{{lowercase title}}File:Atan2definition.svg|thumb|{{math|atan2(y, x)}} returns the angle {{mvar|θ}} between the positive x-axis and the rayray(File:Arctangent2.svg|thumb|Graph of operatorname{atan2}(y, x) over y / x)In computing and mathematics, the function atan2 is the 2-argument arctangent. By definition, theta = operatorname{atan2}(y, x) is the angle measure (in radians, with -pi < theta leq pi) between the positive x-axis and the ray from the origin to the point (x,,y) in the Cartesian plane. Equivalently, operatorname{atan2}(y, x) is the argument (also called phase or angle) of the complex number x + iy.The operatorname{atan2} function first appeared in the programming language Fortran in 1961. It was originally intended to return a correct and unambiguous value for the angle {{mvar|θ}} in converting from Cartesian coordinates {{math|(x, y)}} to polar coordinates {{math|(r, θ)}}. If theta = operatorname{atan2}(y, x) and r = sqrt{x^2 + y^2}, then x = r cos theta and y = r sin theta.If {{math|x > 0}}, the desired angle measure is theta = operatorname{atan2}(y,x) = arctanleft( y / x right). However, when {{math|x < 0}}, the angle arctan(y / x) is diametrically opposite the desired angle, and ±{{pi}} (a half turn) must be added to place the point in the correct quadrant.WEB,scipp.ucsc.edu/~haber/ph116A/arg_11.pdf, The argument of a complex number, Winter 2011, Santa Cruz Institute for Particle Physics, Using the operatorname{atan2} function does away with this correction, simplifying code and mathematical formulas.

Motivation

(File:Atan2 argument sign graph.svg|thumb|Graph of the tangent function from −{{pi}} to +{{pi}} with the corresponding signs of y/x. The green arrows point to the results of atan2(−1, âˆ’1) and atan2(1, 1).)The ordinary single-argument arctangent function only returns angle measures in the interval {left(-frac{pi}{2}, +frac{pi}{2}right)}, and when invoking it to find the angle measure between the {{mvar|x}}-axis and an arbitrary vector in the Cartesian plane, there is no simple way to indicate a direction in the left half-plane (that is, a point (x,,y) with x < 0). Diametrically opposite angle measures have the same tangent because y/x = (-y) / (-x), so the tangent y/x is not in itself sufficient to uniquely specify an angle.To determine an angle measure using the arctangent function given a point or vector (x, y), mathematical formulas or computer code must handle multiple cases; at least one for positive values of x and one for negative values of x, and sometimes additional cases when y is negative or one coordinate is zero. Finding angle measures and converting Cartesian to polar coordinates are common in scientific computing, and this code is redundant and error-prone.To remedy this, computer programming languages introduced the {{math|atan2}} function, at least as early as the Fortran IV language of the 1960s.BOOK
, Organick
, Elliott I.
, 1966
, A FORTRAN IV Primer
, Addison-Wesley
, Some processors also offer the library function called ATAN2, a function of two arguments (opposite and adjacent).
, 42
, The quantity {{math|atan2(y,x)}} is the angle measure between the {{mvar|x}}-axis and a ray from the origin to a point {{math|(x, y)}} anywhere in the Cartesian plane. The signs of {{mvar|x}} and {{mvar|y}} are used to determine the quadrant of the result and select the correct branch of the multivalued function {{math|Arctan(y/x)}}.The {{math|atan2}} function is useful in many applications involving Euclidean vectors such as finding the direction from one point to another or converting a rotation matrix to Euler angles.The {{math|atan2}} function is now included in many other programming languages, and is also commonly found in mathematical formulas throughout science and engineering.

Argument order

In 1961, Fortran introduced the {{math|atan2}} function with argument order (y, x) so that the argument (phase angle) of a complex number is operatorname{arg}z = operatorname{atan2}(operatorname{Im}z, operatorname{Re}z). This follows the left-to-right order of a fraction written y / x, so that operatorname{atan2}(y, x) = operatorname{atan}(y / x) for positive values of x. However, this is the opposite of the conventional component order for complex numbers, z = x + iy, or as coordinates (operatorname{Re}z, operatorname{Im}z). See section Definition and computation.Some other programming languages (see § Realizations of the function in common computer languages) picked the opposite order instead. For example Microsoft Excel uses operatorname{Atan2}(x,y), OpenOffice Calc uses operatorname{arctan2}(x,y), and Mathematica uses operatorname{ArcTan}[x,y], defaulting to one-argument arctangent if called with one argument.

Definition and computation

{{anchor|Definition}}The function {{math|atan2}} computes the principal value of the argument function applied to the complex number {{math|x + iy}}. That is, {{math|1=atan2(y, x) = Pr arg(x + iy) = Arg(x + iy)}}. The argument could be changed by an arbitrary multiple of {{math|2π}} (corresponding to a complete turn around the origin) without making any difference to the angle, but to define {{math|atan2}} uniquely one uses the principal value in the range ( -pi, pi ], that is, {{math|−π < atan2(y, x) ≤ π}}.In terms of the standard {{math|arctan}} function, whose range is {{open-open|−π/2, π/2}}, it can be expressed as follows to define a surface that has no discontinuities except along the semi-infinite line x 0, [5mu]
arctanleft(frac y xright) + pi &text{if } x < 0 text{ and } y ge 0, [5mu]
arctanleft(frac y xright) - pi &text{if } x < 0 text{ and } y < 0, [5mu]
+frac{pi}{2} &text{if } x = 0 text{ and } y > 0, [5mu]
-frac{pi}{2} &text{if } x = 0 text{ and } y < 0, [5mu]
text{undefined} &text{if } x = 0 text{ and } y = 0.
end{cases}A compact expression with four overlapping half-planes is
operatorname{atan2}(y, x) =
begin{cases}
arctanleft(frac{y}{x}right) &text{if } x > 0, [5mu]
frac{pi}{2} - {arctan}bigl(frac x ybigr) &text{if } y > 0, [5mu]
-frac{pi}{2} -{arctan}bigl(frac x ybigr) &text{if } y < 0, [5mu]
arctanleft(frac y xright) pm pi &text{if } x < 0, [5mu]
text{undefined} &text{if } x = 0 text{ and } y = 0.
end{cases}The Iverson bracket notation allows for an even more compact expression: Assuming the definitions text{undefined} cdot 0 = 0, text{undefined} cdot 1 = text{undefined}, andz + text{undefined} = text{undefined} for any z.begin{align}operatorname{atan2}(y, x)&= arctan left( frac{y}{x} right)[xneq 0] [5mu]&qquad + bigl(1-2[y 0,
2 arctanleft(frac{sqrt{x^2 + y^2} - x}{y}right) &text{if } x leq 0 text{ and } y neq 0,
pi &text{if } x < 0 text{ and } y = 0,
text{undefined} &text{if } x = 0 text{ and } y = 0.
end{cases}(File:The principal value of the argument (-atan2- in some circles).svg|thumb|The derivation of the principal value of the argument refers to this figure)Notes:
  • This produces results in the range {{open-closed|−π, Ï€}}.One can apply the periodicity of the result to map to another desired range, e.g. mapping to {{closed-open|0, 2Ï€}} by adding {{math|2Ï€}} to the negative results.
  • As mentioned above, the principal value of the argument {{math|atan2(y, x)}} can be related to {{math|arctan(y/x)}} by trigonometry. The derivation goes as follows:{{pb}} If {{math|1=(x, y) = (r cos θ, r sin θ)}}, then {{math|1=tan(θ/2) = y / (r + x)}}. It follows that operatorname{atan2}(y, x) = theta = 2,theta/2 = 2arctanfrac{y}{sqrt{x^2 + y^2} + x}. Note that {{math|{{sqrt|x{{sup|{{small|2}}}} + y{{sup|{{small|2}}}}}} + x ≠ 0}} in the domain in question.

Derivative

{{details|Winding number}}As the function {{math|atan2}} is a function of two variables, it has two partial derivatives. At points where these derivatives exist, {{math|atan2}} is, except for a constant, equal to {{math|arctan(y/x)}}. Hence for {{math|x > 0}} or {{math|y ≠ 0}},
begin{align}& frac{partial}{partial x}operatorname{atan2}(y,, x) = frac{partial}{partial x} arctanleft(frac y x right) = -frac{y}{x^2 + y^2}, [5pt]& frac{partial}{partial y}operatorname{atan2}(y,, x) = frac{partial}{partial y} arctanleft(frac y x right) = frac x {x^2 + y^2}.end{align}Thus the gradient of atan2 is given by
nabla text{atan2}(y,x)=left({-yover x^2+y^2}, {xover x^2+y^2}right).
Informally representing the function {{math|atan2}} as the angle function {{math|1=θ(x, y) = atan2(y, x)}} (which is only defined up to a constant) yields the following formula for the total differential:
begin{align}
mathrm{d}theta&= frac{partial}{partial x}operatorname{atan2}(y,, x),mathrm{d}x + frac{partial}{partial y}operatorname{atan2}(y,, x),mathrm{d}y [5pt]&= -frac{y}{x^2 + y^2},mathrm{d}x + frac{x}{x^2 + y^2},mathrm{d}y.end{align}While the function {{math|atan2}} is discontinuous along the negative {{mvar|x}}-axis, reflecting the fact that angle cannot be continuously defined, this derivative is continuously defined except at the origin, reflecting the fact that infinitesimal (and indeed local) changes in angle can be defined everywhere except the origin. Integrating this derivative along a path gives the total change in angle over the path, and integrating over a closed loop gives the winding number.In the language of differential geometry, this derivative is a one-form, and it is closed (its derivative is zero) but not exact (it is not the derivative of a 0-form, i.e., a function), and in fact it generates the first de Rham cohomology of the punctured plane. This is the most basic example of such a form, and it is fundamental in differential geometry.The partial derivatives of {{math|atan2}} do not contain trigonometric functions, making it particularly useful in many applications (e.g. embedded systems) where trigonometric functions can be expensive to evaluate.

Illustrations

(File:Atan2 60.svg|thumb|right|300px|atan2 for selected rays)This figure shows values of atan2 along selected rays from the origin, labelled at the unit circle. The values, in radians, are shown inside the circle. The diagram uses the standard mathematical convention that angles increase counterclockwise from zero along the ray to the right. Note that the order of arguments is reversed; the function {{math|atan2(y, x)}} computes the angle corresponding to the point {{math|(x, y)}}.{{clear}}File:Atan2atan.png|thumb|right|300px|Comparison of arctan and atan2 functions]]This figure shows the values of arctan(tan(theta)) along with operatorname {atan2}(sin(theta),cos(theta)) for 0le theta le 2pi. Both functions are odd and periodic with periods pi and 2pi, respectively, and thus can easily be supplemented to any region of real values of theta. One can clearly see the branch cuts of the operatorname {atan2}-function at theta = pi, and of the arctan-function at theta in {tfrac{pi}{2},;tfrac{3pi}{2}}.WEB,www.mndynamics.com/indexp.html, Wolf Jung: Mandel, software for complex dynamics, www.mndynamics.com, 20 April 2018, The two figures below show 3D views of respectively {{math|atan2(y, x)}} and {{math|arctan({{sfrac|y|x}})}} over a region of the plane. Note that for {{math|atan2(y, x)}}, rays in the X/Y-plane emanating from the origin have constant values, but for {{math|arctan({{sfrac|y|x}})}} lines in the X/Y-plane passing through the origin have constant values. For {{math|x > 0}}, the two diagrams give identical values.{| class=“wikitable” style="text-align: center”
300px)300px)

Angle sum and difference identity

Sums of operatorname{atan2} may be collapsed into a single operation according to the following identity
operatorname{atan2} (y_1, x_1) pm operatorname{atan2} (y_2, x_2) = operatorname{atan2} (y_1 x_2 pm y_2 x_1, x_1 x_2 mp y_1 y_2)
...provided that operatorname{atan2} (y_1, x_1) pm operatorname{atan2} (y_2, x_2) in (-pi, pi].The proof involves considering two cases, one where y_2 neq 0 or x_2 > 0 and one where y_2 = 0 and x_2 < 0.We only consider the case where y_2 neq 0 or x_2 > 0. To start, we make the following observations:
  1. -operatorname{atan2}(y,x) = operatorname{atan2}(-y,x) provided that y neq 0 or x > 0.
  2. operatorname{Arg} (x + i y) = operatorname{atan2} (y, x), where operatorname{Arg} is the complex argument function.
  3. theta = operatorname{Arg} e^{i theta} whenever theta in (-pi, pi], a consequence of Euler’s formula.
  4. operatorname{Arg} (e^{i operatorname{Arg} zeta_1} e^{i operatorname{Arg} zeta_2}) = operatorname{Arg} (zeta_1 zeta_2).
To see (4), we have the identity e^{i operatorname{Arg} zeta} = bar{zeta} where bar{zeta} = zeta / left|zetaright|, hence operatorname{Arg} (e^{i operatorname{Arg} zeta_1} e^{i operatorname{Arg} zeta_2}) = operatorname{Arg} (bar{zeta_1} bar{zeta_2}). Furthermore, since operatorname{Arg} zeta = operatorname{Arg} a zeta for any positive real value a, then if we let zeta = zeta_1 zeta_2 and a = frac{1}{left|zeta_1right|left|zeta_2right|} then we have operatorname{Arg} (bar{zeta_1} bar{zeta_2}) = operatorname{Arg} (zeta_1 zeta_2).From these observations have following equivalences:
begin{align}
operatorname{atan2} (y_1, x_1) pm operatorname{atan2} (y_2, x_2)&{} = operatorname{atan2} (y_1, x_1) + operatorname{atan2} (pm y_2, x_2) & text{by (1)} &{} = operatorname{Arg} (x_1 + i y_1) + operatorname{Arg} (x_2 pm i y_2) & text{by (2)} &{} = operatorname{Arg} e^{i (operatorname{Arg} (x_1 + i y_1) + operatorname{Arg} (x_2 pm i y_2))} & text{by (3)} &{} = operatorname{Arg} (e^{i operatorname{Arg} (x_1 + i y_1)} e^{i operatorname{Arg} (x_2 pm i y_2)}) &{} = operatorname{Arg} ((x_1 + i y_1) ( x_2 pm i y_2)) & text{by (4)} &{} = operatorname{Arg} (x_1 x_2 mp y_1 y_2 + i (y_1 x_2 pm y_2 x_1)) &{} = operatorname{atan2} (y_1 x_2 pm y_2 x_1, x_1 x_2 mp y_1 y_2) & text{by (2)}end{align}Corollary: if (y_1, x_1) and (y_2, x_2) are 2-dimensional vectors, the difference formula is frequently used in practice to compute the angle between those vectors with the help of operatorname{atan2}, since the resulting computation behaves benign in the range (-pi, pi] and can thus be used without range checks in many practical situations.

East-counterclockwise, north-clockwise and south-clockwise conventions, etc.

The mathrm{atan2} function was originally designed for the convention in pure mathematics that can be termed east-counterclockwise. In practical applications, however, the north-clockwise and south-clockwise conventions are often the norm. In atmospheric sciences, for instance, the wind direction can be calculated using the mathrm{atan2} function with the east- and north-components of the wind vector as its arguments;WEB,www.eol.ucar.edu/content/wind-direction-quick-reference, Wind Direction Quick Reference, NCAR UCAR Earth Observing Laboratory, the solar azimuth angle can be calculated similarly with the east- and north-components of the solar vector as its arguments. The wind direction is normally defined in the north-clockwise sense, and the solar azimuth angle uses both the north-clockwise and south-clockwise conventions widely.JOURNAL, 10.1016/j.renene.2021.03.047, A solar azimuth formula that renders circumstantial treatment unnecessary without compromising mathematical rigor: Mathematical setup, application and extension of a formula based on the subsolar point and atan2 function, 2021, Zhang, Taiping, Stackhouse, Paul W., MacPherson, Bradley, Mikovitz, J. Colleen, Renewable Energy, 172, 1333–1340, 233631040, free, These different conventions can be realized by swapping the positions and changing the signs of the x- and y-arguments as follows:
  • mathrm{atan2}(y, x),;;;;; (East-Counterclockwise Convention)
  • mathrm{atan2}(x, y),;;;;; (North-Clockwise Convention)
  • mathrm{atan2}(-x, -y). (South-Clockwise Convention)
As an example, let x_{0}=frac{sqrt{3}}{2} and y_{0}=frac{1}{2}, then the east-counterclockwise format gives mathrm{atan2}(y_{0}, x_{0})cdotfrac{180}{pi}=30^{circ}, the north-clockwise format gives mathrm{atan2}(x_{0}, y_{0})cdotfrac{180}{pi}=60^{circ}, and the south-clockwise format gives mathrm{atan2}(-x_{0}, -y_{0})cdotfrac{180}{pi}=-120^{circ}.Changing the sign of the x- and/or y-arguments and/or swapping their positions can create 8 possible variations of the mathrm{atan2} function and they, interestingly, correspond to 8 possible definitions of the angle, namely, clockwise or counterclockwise starting from each of the 4 cardinal directions, north, east, south and west.

Realizations of the function in common computer languages

The realization of the function differs from one computer language to another: The (operatorname{Im}, operatorname{Re}) convention is used by:
  • The C function atan2, and most other computer implementations, are designed to reduce the effort of transforming cartesian to polar coordinates and so always define atan2(0, 0). On implementations without signed zero, or when given positive zero arguments, it is normally defined as 0. It will always return a value in the range {{closed-closed|−π, Ï€}} rather than raising an error or returning a NaN (Not a Number).
  • In Common Lisp, where optional arguments exist, the atan function allows one to optionally supply the x coordinate: (atan y x).WEB,www.lispworks.com/documentation/HyperSpec/Body/f_asin_.htm, CLHS: Function ASIN, ACOS, ATAN, LispWorks,
  • In Julia, the situation is similar to Common Lisp: instead of atan2, the language has a one-parameter and a two-parameter form for atan.WEB,docs.julialang.org/en/v1/base/math/, Mathematics · The Julia Language, docs.julialang.org, However, it has many more than two methods, to allow for aggressive optimisation at compile time (see the section “Why don’t you compile Matlab/Python/R/… code to Julia?” WEB,docs.julialang.org/en/v1/manual/faq/, Frequently Asked Questions · The Julia Language, docs.julialang.org, ).
  • For systems implementing signed zero, infinities, or Not a Number (for example, IEEE floating point), it is common to implement reasonable extensions which may extend the range of values produced to include −{{math|Ï€}} and −0 when {{math|y}} = −0. These also may return NaN or raise an exception when given a NaN argument.
  • In the Intel x86 Architecture assembler code, atan2 is known as the FPATAN (floating-point partial arctangent) instruction.IA-32 Intel Architecture Software Developer’s Manual. Volume 2A: Instruction Set Reference, A-M, 2004. It can deal with infinities and results lie in the closed interval {{closed-closed|−π, Ï€}}, e.g. atan2(∞, x) = +{{math|Ï€}}/2 for finite x. Particularly, FPATAN is defined when both arguments are zero:
  • : atan2(+0, +0) = +0;
  • : atan2(+0, −0) = +{{math|Ï€}};
  • : atan2(−0, +0) = −0;
  • : atan2(−0, −0) = −{{math|Ï€}}.


This definition is related to the concept of signed zero.
  • In mathematical writings other than source code, such as in books and articles, the notations ArctanBOOK,books.google.com/books?id=2LIMMD9FVXkC&q=four+quadrant+inverse+tangent+mathematical+notation&pg=PA234, Principles of Digital Image Processing: Fundamental Techniques, Wilhelm, Burger, Mark J., Burge, 7 July 2010, Springer Science & Business Media, 978-1-84800-191-6, 20 April 2018, Google Books, and Tan−1BOOK,books.google.com/books?id=7nNjaH9B0_0C&q=four+quadrant+inverse+tangent+mathematical+notation&pg=PA345, Introduction to Circuit Analysis and Design, Tildon H., Glisson, 18 February 2011, Springer Science & Business Media, 9789048194438, 20 April 2018, Google Books, have been utilized; these are capitalized variants of the regular arctan and tan−1. This usage is consistent with the complex argument notation, such that {{math|Atan(y, x) {{=}} Arg(x + iy)}}.
  • On HP calculators, treat the coordinates as a complex number and then take the ARG. Or R ARG >> ‘ATAN2’ STO.
  • On scientific calculators the function can often be calculated as the angle given when {{math|(x, y)}} is converted from rectangular coordinates to polar coordinates.
  • Systems supporting symbolic mathematics normally return an undefined value for {{math|atan2(0, 0)}} or otherwise signal that an abnormal condition has arisen.
  • The free math library FDLIBM (Freely Distributable LIBM) available from netlib has source code showing how it implements atan2 including handling the various IEEE exceptional values.
  • For systems without a hardware multiplier the function {{math|atan2}} can be implemented in a numerically reliable manner by the CORDIC method. Thus implementations of {{math|atan(y)}} will probably choose to compute {{math|atan2(y, 1)}}.

See also

References

{{reflist}}

External links



Other implementations/code for atan2

Notes

{{Trigonometric and hyperbolic functions}}

- content above as imported from Wikipedia
- "atan2" does not exist on GetWiki (yet)
- time: 10:20am EDT - Wed, May 22 2024
[ this remote article is provided by Wikipedia ]
LATEST EDITS [ see all ]
GETWIKI 21 MAY 2024
GETWIKI 09 JUL 2019
Eastern Philosophy
History of Philosophy
GETWIKI 09 MAY 2016
GETWIKI 18 OCT 2015
M.R.M. Parrott
Biographies
GETWIKI 20 AUG 2014
CONNECT