1 | /* $Id: atan_2.cpp 1370 2009-05-31 05:31:50Z bradbell $ */ |
---|
2 | /* -------------------------------------------------------------------------- |
---|
3 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-07 Bradley M. Bell |
---|
4 | |
---|
5 | CppAD is distributed under multiple licenses. This distribution is under |
---|
6 | the terms of the |
---|
7 | Common Public License Version 1.0. |
---|
8 | |
---|
9 | A copy of this license is included in the COPYING file of this distribution. |
---|
10 | Please visit http://www.coin-or.org/CppAD/ for information on other licenses. |
---|
11 | -------------------------------------------------------------------------- */ |
---|
12 | |
---|
13 | /* |
---|
14 | Old example and test now just used for validation testing. |
---|
15 | */ |
---|
16 | |
---|
17 | # include <cppad/cppad.hpp> |
---|
18 | # include <cmath> |
---|
19 | |
---|
20 | bool Atan2(void) |
---|
21 | { bool ok = true; |
---|
22 | |
---|
23 | using CppAD::atan; |
---|
24 | using CppAD::sin; |
---|
25 | using CppAD::cos; |
---|
26 | using namespace CppAD; |
---|
27 | |
---|
28 | // independent variable vector |
---|
29 | CPPAD_TEST_VECTOR< AD<double> > U(1); |
---|
30 | U[0] = 1.; |
---|
31 | Independent(U); |
---|
32 | |
---|
33 | // a temporary values |
---|
34 | AD<double> x = cos(U[0]); |
---|
35 | AD<double> y = sin(U[0]); |
---|
36 | |
---|
37 | // dependent variable vector |
---|
38 | CPPAD_TEST_VECTOR< AD<double> > Z(1); |
---|
39 | Z[0] = atan2(y, x); |
---|
40 | |
---|
41 | // create f: U -> Z and vectors used for derivative calculations |
---|
42 | ADFun<double> f(U, Z); |
---|
43 | CPPAD_TEST_VECTOR<double> v(1); |
---|
44 | CPPAD_TEST_VECTOR<double> w(1); |
---|
45 | |
---|
46 | // check original value (u in first quadrant) |
---|
47 | ok &= NearEqual(U[0] , Z[0], 1e-10 , 1e-10); |
---|
48 | |
---|
49 | // check case where u is in second quadrant |
---|
50 | v[0] = 3.; |
---|
51 | w = f.Forward(0, v); |
---|
52 | ok &= NearEqual(w[0] , v[0], 1e-10 , 1e-10); |
---|
53 | |
---|
54 | // check case where u is in third quadrant |
---|
55 | v[0] = -3.; |
---|
56 | w = f.Forward(0, v); |
---|
57 | ok &= NearEqual(w[0] , v[0], 1e-10 , 1e-10); |
---|
58 | |
---|
59 | // check case where u is in fourth quadrant |
---|
60 | v[0] = -1.; |
---|
61 | w = f.Forward(0, v); |
---|
62 | ok &= NearEqual(w[0] , v[0], 1e-10 , 1e-10); |
---|
63 | |
---|
64 | // forward computation of partials w.r.t. u |
---|
65 | size_t j; |
---|
66 | size_t p = 5; |
---|
67 | double jfac = 1.; |
---|
68 | double value = 1.; |
---|
69 | v[0] = 1.; |
---|
70 | for(j = 1; j < p; j++) |
---|
71 | { jfac *= j; |
---|
72 | w = f.Forward(j, v); |
---|
73 | ok &= NearEqual(jfac*w[0], value, 1e-10 , 1e-10); // d^jz/du^j |
---|
74 | v[0] = 0.; |
---|
75 | value = 0.; |
---|
76 | } |
---|
77 | |
---|
78 | // reverse computation of partials of Taylor coefficients |
---|
79 | CPPAD_TEST_VECTOR<double> r(p); |
---|
80 | w[0] = 1.; |
---|
81 | r = f.Reverse(p, w); |
---|
82 | jfac = 1.; |
---|
83 | value = 1.; |
---|
84 | for(j = 0; j < p; j++) |
---|
85 | { ok &= NearEqual(jfac*r[j], value, 1e-10 , 1e-10); // d^jz/du^j |
---|
86 | jfac *= (j + 1); |
---|
87 | value = 0.; |
---|
88 | } |
---|
89 | |
---|
90 | return ok; |
---|
91 | } |
---|