00001 # ifndef CPPAD_ATAN2_INCLUDED 00002 # define CPPAD_ATAN2_INCLUDED 00003 00004 /* -------------------------------------------------------------------------- 00005 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell 00006 00007 CppAD is distributed under multiple licenses. This distribution is under 00008 the terms of the 00009 Common Public License Version 1.0. 00010 00011 A copy of this license is included in the COPYING file of this distribution. 00012 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00013 -------------------------------------------------------------------------- */ 00014 00015 /* 00016 ------------------------------------------------------------------------------- 00017 $begin atan2$$ 00018 $spell 00019 Vec 00020 CppAD 00021 namespace 00022 std 00023 atan 00024 const 00025 $$ 00026 00027 $index tan, AD inverse$$ 00028 $index inverse, AD tan$$ 00029 $index atan2, AD$$ 00030 00031 $section AD Two Argument Inverse Tangent Function$$ 00032 00033 $head Syntax$$ 00034 $syntax%%theta% = atan2(%y%, %x%)%$$ 00035 00036 00037 $head Purpose$$ 00038 Determines an angle $latex \theta \in [ - \pi , + \pi ]$$ 00039 such that 00040 $latex \[ 00041 \begin{array}{rcl} 00042 \sin ( \theta ) & = & y / \sqrt{ x^2 + y^2 } \\ 00043 \cos ( \theta ) & = & x / \sqrt{ x^2 + y^2 } 00044 \end{array} 00045 \] $$ 00046 00047 $head Base$$ 00048 A definition of $code atan2$$, 00049 for the case where both arguments are $code float$$ 00050 or both arguments are $code double$$, 00051 is included in the $code CppAD$$ namespace 00052 (the corresponding results has the same type as the arguments). 00053 The type $italic Base$$ can be any type in the 00054 $cref/AD levels above/glossary/AD Levels Above Base/$$ 00055 above $code float$$ or $code double$$. 00056 00057 $head y$$ 00058 The argument $italic y$$ has one of the following prototypes 00059 $syntax% 00060 const AD<%Base%> &%y% 00061 const VecAD<%Base%>::reference &%y% 00062 %$$ 00063 00064 $head x$$ 00065 The argument $italic x$$ has one of the following prototypes 00066 $syntax% 00067 const AD<%Base%> &%x% 00068 const VecAD<%Base%>::reference &%x% 00069 %$$ 00070 00071 $head theta$$ 00072 The result $italic theta$$ has prototype 00073 $syntax% 00074 AD<%Base%> %theta% 00075 %$$ 00076 00077 $head Operation Sequence$$ 00078 The AD of $italic Base$$ 00079 operation sequence used to calculate $italic theta$$ is 00080 $xref/glossary/Operation/Independent/independent/1/$$ 00081 of $italic x$$ and $italic y$$. 00082 00083 $head Example$$ 00084 $children% 00085 example/atan_2.cpp 00086 %$$ 00087 The file 00088 $xref/Atan2.cpp/$$ 00089 contains an example and test of this function. 00090 It returns true if it succeeds and false otherwise. 00091 00092 $end 00093 ------------------------------------------------------------------------------- 00094 */ 00095 00096 namespace CppAD { // BEGIN CppAD namespace 00097 00098 inline float atan2(float x, float y) 00099 { return std::atan2(x, y); } 00100 00101 inline double atan2(double x, double y) 00102 { return std::atan2(x, y); } 00103 00104 // The code below is used as an example by the CondExp documentation. 00105 // BEGIN CondExp 00106 template <class Base> 00107 AD<Base> atan2 (const AD<Base> &y, const AD<Base> &x) 00108 { AD<Base> alpha; 00109 AD<Base> beta; 00110 AD<Base> theta; 00111 00112 AD<Base> zero = 0; 00113 AD<Base> pi2 = 2. * atan(1.); 00114 AD<Base> pi = 2. * pi2; 00115 00116 AD<Base> ax = abs(x); 00117 AD<Base> ay = abs(y); 00118 00119 // if( ax > ay ) 00120 // theta = atan(ay / ax); 00121 // else theta = pi2 - atan(ax / ay); 00122 alpha = atan(ay / ax); 00123 beta = pi2 - atan(ax / ay); 00124 theta = CondExpGt(ax, ay, alpha, beta); // use of CondExp 00125 00126 // if( x <= 0 ) 00127 // theta = pi - theta; 00128 theta = CondExpLe(x, zero, pi - theta, theta); // use of CondExp 00129 00130 // if( y <= 0 ) 00131 // theta = - theta; 00132 theta = CondExpLe(y, zero, -theta, theta); // use of CondExp 00133 00134 return theta; 00135 } 00136 // END CondExp 00137 00138 template <class Base> 00139 inline AD<Base> atan2 (const VecAD_reference<Base> &y, const AD<Base> &x) 00140 { return atan2( y.ADBase() , x ); } 00141 00142 template <class Base> 00143 inline AD<Base> atan2 (const AD<Base> &y, const VecAD_reference<Base> &x) 00144 { return atan2( y , x.ADBase() ); } 00145 00146 template <class Base> 00147 inline AD<Base> atan2 00148 (const VecAD_reference<Base> &y, const VecAD_reference<Base> &x) 00149 { return atan2( y.ADBase() , x.ADBase() ); } 00150 00151 } // END CppAD namespace 00152 00153 # endif