atan2

出典: フリー百科事典『ウィキペディア(Wikipedia)』

これはこのページの過去の版です。Loxyplst (会話 | 投稿記録) による 2021年3月7日 (日) 03:24個人設定で未設定ならUTC)時点の版 (→‎微分)であり、現在の版とは大きく異なる場合があります。

関数atan2(yx)は、点(xy)方向への半直線(レイ)と、x軸の正の向きとの間の角度θを返す。ただし、範囲は(−π,π]である
したときののグラフ

atan2アークタンジェント2)は、関数の一種。「2つの引数を取るarctan(アークタンジェント)」という意味である。x軸の正の向きと、点(xy) (ただし、(0, 0)ではない)まで伸ばした半直線(レイ)との間の、ユークリッド平面上における角度として定義される。関数はもしくはの形をとり、値はラジアンで返ってくる。

関数は、プログラミング言語のFortran (IBM社が1961年に実装したFORTRAN-IV)において最初に登場した。元々は、角度θ直交座標系(xy)から極座標系(rθ)に変換する際に、正確で一意な値が返ってくることを意図して導入された。また、複素数偏角 (「位相」とも言う)を求める際にも利用される。

関数 は、π < θπの範囲で、

(ただし、r > 0)となる単一の値θを返す。

なお、arctanを使って角度θを求める際に注意すべきことであるが、が真であるからと言って、

であるということが常に言えるとは限らない。

これは、x > 0だった場合にのみ当てはまる。もしx < 0だった場合は、上の式から導出された角度は、正しい角度とは正反対の方向を指し示している。そして、デカルト点(xy)をユークリッド平面上の正しい象限に配置するには、Π(度数法で言うと180°)の値をθに加算または減算する必要がある[1]。これはつまり、正しい角度を出すためにはxおよびyの符号の情報がそれぞれ別個に必要とされると言うことであり、もし単にyxで除算した場合は、その情報が失われてしまうというわけである。その点、atan2は細かいことを気にしなくてもyとxに代入すれば普通に正しい角度を返してくれるので便利である。

関数の戻り値である角度θについては、xyの値を変更しないままでも角度θ2πの任意の整数倍を加算することが可能であるため、つまり区間をちゃんと設定しないと戻り値の値が曖昧になってしまうため、関数atan2はその主値として、区間を(−π, π]としたものが戻り値として返ってくるようになっている(関数arctanの戻り値の区間が(−π/2 , π/2)である点との違いに注意)。角度θ符号付き角度であり、反時計回りの角度は正、時計回りは負となる。具体的に言うと、もしy ≥ 0だった場合は[0, π] の区間にあり、もしy < 0だった場合には(−π, 0)の区間にある、と言う感じになるように関数の内部で場合分けをしている。

atan2が生み出された経緯と動機

Graph of the tangent function from −π to +π with the corresponding signs of y/x. The green arrows point to the results of atan2(−1, −1) and atan2(1, 1).

atan2関数は、元々は特定のプログラミング言語に実装された関数の一つに過ぎなかったが、現在では他の科学技術の分野でもよく使われるものとなっている。その起源は少なくとも、古のプログラミング言語であるFORTRANにまで遡ることができるが[2]、現在においては他の現代的なプログラミング言語においても実装されている。例を挙げると、C言語のmath.h 標準CライブラリJavaのMathライブラリ、.NETのSystem.Math (C#VB.NETなどから利用できる)、Pythonのmathモジュール、RubyのMathモジュール、Goのmathパッケージ[3]、などであるが、もちろん他にもたくさんの言語に実装されている。さらに言うと、Perlを始めとするスクリプト言語にも、C言語風のatan2(y, x)関数が実装されていることが多い。

atan2関数が生み出された背景として、単一引数のみを取るアークタンジェント(arctan関数)は正反対の方向を区別できない、と言う使い勝手の悪さがある。例えば、x軸とベクトル(1, 1)がなす反時計回りの角度を、arctan関数を使って「arctan(1/1)」のような形で計算した場合、「π/4ラジアン(つまり度数法で言うと45°)」という答えが返ってくる。しかし、x軸とベクトル(−1, −1)の間の角度を同様に、「arctan(−1/−1)」のような形で計算してみると、期待される答えは「−3π/4ラジアン(−135°)」または「5π/4ラジアン(225°)」であるにもかかわらず、やっぱり「π/4ラジアン」が返ってくる。arctan関数の使い勝手の悪さをさらに言うと、x軸とベクトル(0, y)(ただし、y ≠ 0とする)がなす角度をarctan関数で計算しようとすると、これはarctan(y/0)の評価が必要になるわけだが、こんなことをさせるとコンピューターはゼロ除算のエラーを吐いてバグる。

atan2関数は、2つの変数 yxから一意なアークタンジェントの値を算出するが、そのとき両変数の正負の符号が、実行結果の象限を決定するために利用される。それに基づいて、アークタンジェントで「y/x」したときの結果の中から、求める値が出るような分岐先に飛ばしている。例を挙げると、「atan2(1, 1) = π/4」 と 「atan2(−1, −1) = −3π/4」のどちらに分岐するのか、と言ったことを判別している。また、例えば「ゼロ除算のエラーを吐いてバグる」の代わりに「atan2(1, 0) = π/2」に分岐させるときも、同様の方法で飛ばしている。

もちろん、場合によって必要な象限補正と例外処理をいちいち精査することで、上記の計算を力技で行うことは可能だが、そんなことをするよりも、常に一意な正しい結果を提供するような1つの関数を使った方が便利である。そんな経緯でatan2関数が存在する。atan2関数は様々な応用(アプリケーション)において有用であり、例えばユークリッド空間上におけるユークリッド点に関して、1つの点から別の点へと至る方向ベクトルを調べるのに便利である。主な用途としては、コンピューターグラフィックス(CG)における回転において、回転行列をオイラー角に変換する際に使われる。

定義と計算

定義

関数 atan2複素数 x + yi偏角関数 arg を適用した時の主値を計算する。すなわち、atan2(yx) = Pr arg(x + yi) = Arg(x + yi) である。偏角は、(原点を中心としたちょうど1周の回転に対応)の整数倍を加えたものも同じ角度になるが、atan2 を一意に定義するために範囲 の主値を使用する。つまり、π < atan2(yx) ≤ π とする。

標準の arctan 関数(値域 (−π/2, π/2))を用いて、次のように表すことができる:

重複する4つの半平面を用いたコンパクトな式:

アイバーソンの記法を用いれば、さらにコンパクトな式が可能:

[note 1]

一見すると条件式のない数式:

タンジェントの半角の公式から派生した次の式で atan2 を定義することもできる:

この式は、上記の定義よりもシンボリックな使用に適している場合がある。ただし、 の丸め誤差の影響が領域 x < 0, y = 0 の近くで拡大するため、一般的な浮動小数点数の計算用途には適していない(これにより、yがゼロで除算されることもある)。

これらの膨らんだ丸め誤差を回避するため、先ほどの式を変形:

偏角の主値の導出は、この図を参照している。

注意:

  • これにより、範囲 (−π, π] の結果が生成される。[note 2]
  • 上記のように、偏角の主値 atan2(yx) は、三角法によって arctan(y/x) に関連付けることができる。 導出は次のようになる:
(xy) = (r cos θ, r sin θ) のとき tan(θ/2) = y / (r + x) となる。その結果、次式が成立する。
問題の領域は x2 + y2 + x ≠ 0 であることに注意。

微分

関数 atan2 は2変数関数であるため、2つの偏導関数がある。これらの導関数が存在する点では、atan2 は定数項を除いた arctan(y/x) に等しくなる。したがって、x > 0 または y ≠ 0 の場合、

したがって、atan2 の勾配は次式で与えられる。

関数 atan2 を角度関数 θ(x, y) = atan2(y, x) (定数を除いて定義)として省略して表すと、全微分について次の式が得られる:

関数 atan2 は、負の x 軸に沿って不連続であるが、角度を連続的に定義できないという事実を反映して、この導関数は原点を除いて連続的に定義される。これは、角度の微小な(そして実際には局所的な)変化が原点を除くすべての場所で定義できるという事実を反映している。この導関数をパスに沿って積分するとパス全体の角度が全体的に変化し、閉ループで積分すると回転数が得られる。

微分幾何学の言語では、この導関数は1-形式であり、である(導関数が0である)が、完全ではない(0-形式の導関数、つまり関数ではない)。実際、1点を除いた平面(Punctured Plane)の1次のド・ラームコホモロジーを生成する。これはそのような形式の最も基本的な例であり、微分幾何学の基本である。

atan2 の偏導関数には三角関数が含まれていないため、三角関数の評価にコストがかかる可能性がある多くのアプリケーション(例:組み込みシステム)で特に役立つ。

Illustrations

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 atan2(y, x) computes the angle corresponding to the point (x, y).

Comparison of arctan and atan2 functions

This figure shows the values of along with for . Both functions are odd and periodic with periods and , respectively, and thus can easily be supplemented to any region of real values of . One can clearly see the branch cuts of the -function at , and of the -function at .[4]

The two figures below show 3D views of respectively atan2(y, x) and arctan(y/x) over a region of the plane. Note that for atan2(y, x), rays in the X/Y-plane emanating from the origin have constant values, but for arctan(y/x) lines in the X/Y-plane passing through the origin have constant values. For x > 0, the two diagrams give identical values.


Angle sum and difference identity

Sums of may be collapsed into a single operation according to the following identity

...provided that .

The proof involves considering two cases, one where or and one where and .

We only consider the case where or . To start, we make the following observations:

  1. provided that or .
  2. , where is the complex argument function.
  3. whenever , a consequence of Euler's formula.
  4. .

To see (4), we have the identity where , hence . Furthermore, since for any positive real value , then if we let and then we have .

From these observations have following equivalences:

Corollary: if and are 2-dimensional vectors, the difference formula is frequently used in practice to compute the angle between those vectors with the help of , since the resulting computation behaves benign in the range and can thus be used without range checks in many practical situations.

Realizations of the function in common computer languages

The realization of the function differs from one computer language to another:

  • 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 [−π, π] 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).[5]
  • In Mathematica, the form ArcTan[x, y] is used where the one parameter form supplies the normal arctangent (note that the order of arguments is reversed relative to the convention used in the discussion above). Mathematica classifies ArcTan[0, 0] as an indeterminate expression.
  • In Microsoft Excel,[6] OpenOffice.org Calc, LibreOffice Calc,[7] Google Spreadsheets,[8] iWork Numbers,[9] and ANSI SQL:2008 standard,[10] the atan2 function has the two arguments reversed.
  • In the Intel Architecture assembler code, atan2 is known as the FPATAN (floating-point partial arctangent) instruction.[11] It can deal with infinities and results lie in the closed interval [−π, π], e.g. atan2(∞, x) = +π/2 for finite x. Particularly, FPATAN is defined when both arguments are zero:
    atan2(+0, +0) = +0;
    atan2(+0, −0) = +π;
    atan2(−0, +0) = −0;
    atan2(−0, −0) = −π.
This definition is related to the concept of signed zero.
  • On most TI graphing calculators (excluding the TI-85 and TI-86), the equivalent function is called R►Pθ and has the arguments reversed.
  • On TI-85 the arg function is called angle(x,y) and although it appears to take two arguments, it really only has one complex argument which is denoted by a pair of numbers: x + yi = (x, y).
  • In mathematical writings other than source code, such as in books and articles, the notations Arctan[12] and Tan−1[13] 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 Atan(y, x) = Arg(x + yi).
  • On HP calculators, treat the coordinates as a complex number and then take the ARG. Or << C->R ARG >> 'ATAN2' STO.
  • On scientific calculators the function can often be calculated as the angle given when (x, y) is converted from rectangular coordinates to polar coordinates.
  • Systems supporting symbolic mathematics normally return an undefined value for atan2(0, 0) or otherwise signal that an abnormal condition has arisen.
  • 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 −π and −0. These also may return NaN or raise an exception when given a NaN argument.
  • For systems implementing signed zero (for example, IEEE floating point), atan2(−0, x), x < 0 poses the risk of returning the value −π, in the case where the implementation of atan2(y, x) fails to properly handle −0 inputs.
  • 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 atan2 can be implemented in a numerically reliable manner by the CORDIC method. Thus implementations of atan(y) will probably choose to compute atan2(y, 1).

See also

References

  1. ^ http://scipp.ucsc.edu/~haber/ph116A/arg_11.pdf
  2. ^ Organick, Elliott I. (1966). A FORTRAN IV Primer. Addison-Wesley. pp. 42. "Some processors also offer the library function called ATAN2, a function of two arguments (opposite and adjacent)." 
  3. ^ src/math/atan2.go”. The Go Programming Language. 2018年4月20日閲覧。
  4. ^ Wolf Jung: Mandel, software for complex dynamics”. www.mndynamics.com. 2018年4月20日閲覧。
  5. ^ CLHS: Function ASIN, ACOS, ATAN”. LispWorks. Template:Cite webの呼び出しエラー:引数 accessdate は必須です。
  6. ^ Microsoft Excel Atan2 Method”. Microsoft. Template:Cite webの呼び出しエラー:引数 accessdate は必須です。
  7. ^ LibreOffice Calc ATAN2”. Libreoffice.org. Template:Cite webの呼び出しエラー:引数 accessdate は必須です。
  8. ^ Google Spreadsheets' Function List”. Template:Cite webの呼び出しエラー:引数 accessdate は必須です。
  9. ^ Numbers' Trigonometric Function List”. Apple. Template:Cite webの呼び出しエラー:引数 accessdate は必須です。
  10. ^ ANSI SQL:2008 standard”. Teradata. 2015年8月20日時点のオリジナルよりアーカイブ。 Template:Cite webの呼び出しエラー:引数 accessdate は必須です。
  11. ^ IA-32 Intel Architecture Software Developer’s Manual. Volume 2A: Instruction Set Reference, A-M, 2004.
  12. ^ Burger, Wilhelm; Burge, Mark J. (7 July 2010). Principles of Digital Image Processing: Fundamental Techniques. Springer Science & Business Media. ISBN 9781848001916. https://books.google.com/books?id=2LIMMD9FVXkC&q=four+quadrant+inverse+tangent+mathematical+notation&pg=PA234 2018年4月20日閲覧。 
  13. ^ Glisson, Tildon H. (18 February 2011). Introduction to Circuit Analysis and Design. Springer Science & Business Media. ISBN 9789048194438. https://books.google.com/books?id=7nNjaH9B0_0C&q=four+quadrant+inverse+tangent+mathematical+notation&pg=PA345 2018年4月20日閲覧。 

External links

Other implementations/code for atan2

Notes

  1. ^ Thereby
      and
      for every
  2. ^ 結果の周期性を適用して、別の目的の範囲に写像できる。e.g. 負の結果に を加算すると [0, 2π) に写像できる。