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