CppAD: A C++ Algorithmic Differentiation Package  20171217
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
atan2.hpp
Go to the documentation of this file.
1 # ifndef CPPAD_CORE_ATAN2_HPP
2 # define CPPAD_CORE_ATAN2_HPP
3 
4 /* --------------------------------------------------------------------------
5 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
6 
7 CppAD is distributed under multiple licenses. This distribution is under
8 the terms of the
9  Eclipse Public License Version 1.0.
10 
11 A copy of this license is included in the COPYING file of this distribution.
12 Please visit http://www.coin-or.org/CppAD/ for information on other licenses.
13 -------------------------------------------------------------------------- */
14 
15 /*
16 -------------------------------------------------------------------------------
17 $begin atan2$$
18 $spell
19  Vec
20  CppAD
21  namespace
22  std
23  atan
24  const
25 $$
26 
27 
28 $section AD Two Argument Inverse Tangent Function$$
29 $mindex tan atan2$$
30 
31 $head Syntax$$
32 $icode%theta% = atan2(%y%, %x%)%$$
33 
34 
35 $head Purpose$$
36 Determines an angle $latex \theta \in [ - \pi , + \pi ]$$
37 such that
38 $latex \[
39 \begin{array}{rcl}
40  \sin ( \theta ) & = & y / \sqrt{ x^2 + y^2 } \\
41  \cos ( \theta ) & = & x / \sqrt{ x^2 + y^2 }
42 \end{array}
43 \] $$
44 
45 $head y$$
46 The argument $icode y$$ has one of the following prototypes
47 $codei%
48  const AD<%Base%> &%y%
49  const VecAD<%Base%>::reference &%y%
50 %$$
51 
52 $head x$$
53 The argument $icode x$$ has one of the following prototypes
54 $codei%
55  const AD<%Base%> &%x%
56  const VecAD<%Base%>::reference &%x%
57 %$$
58 
59 $head theta$$
60 The result $icode theta$$ has prototype
61 $codei%
62  AD<%Base%> %theta%
63 %$$
64 
65 $head Operation Sequence$$
66 The AD of $icode Base$$
67 operation sequence used to calculate $icode theta$$ is
68 $cref/independent/glossary/Operation/Independent/$$
69 of $icode x$$ and $icode y$$.
70 
71 $head Example$$
72 $children%
73  example/general/atan2.cpp
74 %$$
75 The file
76 $cref atan2.cpp$$
77 contains an example and test of this function.
78 It returns true if it succeeds and false otherwise.
79 
80 $end
81 -------------------------------------------------------------------------------
82 */
83 
84 namespace CppAD { // BEGIN CppAD namespace
85 
86 inline float atan2(float x, float y)
87 { return std::atan2(x, y); }
88 
89 inline double atan2(double x, double y)
90 { return std::atan2(x, y); }
91 
92 // The code below is used as an example by the CondExp documentation.
93 // BEGIN CondExp
94 template <class Base>
95 AD<Base> atan2 (const AD<Base> &y, const AD<Base> &x)
96 { AD<Base> alpha;
97  AD<Base> beta;
98  AD<Base> theta;
99 
100  AD<Base> zero(0.);
101  AD<Base> pi2(2. * atan(1.));
102  AD<Base> pi(2. * pi2);
103 
104  AD<Base> ax = fabs(x);
105  AD<Base> ay = fabs(y);
106 
107  // if( ax > ay )
108  // theta = atan(ay / ax);
109  // else theta = pi2 - atan(ax / ay);
110  alpha = atan(ay / ax);
111  beta = pi2 - atan(ax / ay);
112  theta = CondExpGt(ax, ay, alpha, beta); // use of CondExp
113 
114  // if( x <= 0 )
115  // theta = pi - theta;
116  theta = CondExpLe(x, zero, pi - theta, theta); // use of CondExp
117 
118  // if( y <= 0 )
119  // theta = - theta;
120  theta = CondExpLe(y, zero, -theta, theta); // use of CondExp
121 
122  return theta;
123 }
124 // END CondExp
125 
126 template <class Base>
127 inline AD<Base> atan2 (const VecAD_reference<Base> &y, const AD<Base> &x)
128 { return atan2( y.ADBase() , x ); }
129 
130 template <class Base>
131 inline AD<Base> atan2 (const AD<Base> &y, const VecAD_reference<Base> &x)
132 { return atan2( y , x.ADBase() ); }
133 
134 template <class Base>
135 inline AD<Base> atan2
137 { return atan2( y.ADBase() , x.ADBase() ); }
138 
139 } // END CppAD namespace
140 
141 # endif
Definition: ad.hpp:34
CPPAD_INLINE_FRIEND_TEMPLATE_FUNCTION AD< Base > CondExpGt(const AD< Base > &left, const AD< Base > &right, const AD< Base > &if_true, const AD< Base > &if_false)
Definition: cond_exp.hpp:340
std::complex< double > atan(const std::complex< double > &x)
float atan2(float x, float y)
Definition: atan2.hpp:86
AD< Base > ADBase(void) const
Conversion from VecAD_reference to AD&lt;Base&gt;. puts the correspond vecad load instruction in the tape...
Definition: vec_ad.hpp:392
CPPAD_INLINE_FRIEND_TEMPLATE_FUNCTION AD< Base > CondExpLe(const AD< Base > &left, const AD< Base > &right, const AD< Base > &if_true, const AD< Base > &if_false)
Definition: cond_exp.hpp:337
Class used to hold a reference to an element of a VecAD object.
Definition: vec_ad.hpp:352
std::complex< double > fabs(const std::complex< double > &x)