1 | /* $Id: cos.cpp 2506 2012-10-24 19:36:49Z bradbell $ */ |
---|
2 | /* -------------------------------------------------------------------------- |
---|
3 | CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-12 Bradley M. Bell |
---|
4 | |
---|
5 | CppAD is distributed under multiple licenses. This distribution is under |
---|
6 | the terms of the |
---|
7 | Eclipse 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 now just used for validation testing |
---|
15 | */ |
---|
16 | # include <cppad/cppad.hpp> |
---|
17 | |
---|
18 | bool Cos(void) |
---|
19 | { bool ok = true; |
---|
20 | |
---|
21 | using CppAD::sin; |
---|
22 | using CppAD::cos; |
---|
23 | using namespace CppAD; |
---|
24 | |
---|
25 | // independent variable vector |
---|
26 | CPPAD_TESTVECTOR(AD<double>) U(1); |
---|
27 | U[0] = 1.; |
---|
28 | Independent(U); |
---|
29 | |
---|
30 | // dependent variable vector |
---|
31 | CPPAD_TESTVECTOR(AD<double>) Z(1); |
---|
32 | Z[0] = cos(U[0]); |
---|
33 | |
---|
34 | // create f: U -> Z and vectors used for derivative calculations |
---|
35 | ADFun<double> f(U, Z); |
---|
36 | CPPAD_TESTVECTOR(double) v(1); |
---|
37 | CPPAD_TESTVECTOR(double) w(1); |
---|
38 | |
---|
39 | // check value |
---|
40 | double sin_u = sin( Value(U[0]) ); |
---|
41 | double cos_u = cos( Value(U[0]) ); |
---|
42 | |
---|
43 | ok &= NearEqual(cos_u, Value(Z[0]), 1e-10 , 1e-10); |
---|
44 | |
---|
45 | // forward computation of partials w.r.t. u |
---|
46 | size_t j; |
---|
47 | size_t p = 5; |
---|
48 | double jfac = 1.; |
---|
49 | v[0] = 1.; |
---|
50 | for(j = 1; j < p; j++) |
---|
51 | { w = f.Forward(j, v); |
---|
52 | |
---|
53 | double value; |
---|
54 | if( j % 4 == 1 ) |
---|
55 | value = -sin_u; |
---|
56 | else if( j % 4 == 2 ) |
---|
57 | value = -cos_u; |
---|
58 | else if( j % 4 == 3 ) |
---|
59 | value = sin_u; |
---|
60 | else value = cos_u; |
---|
61 | |
---|
62 | jfac *= j; |
---|
63 | ok &= NearEqual(jfac*w[0], value, 1e-10 , 1e-10); // d^jz/du^j |
---|
64 | v[0] = 0.; |
---|
65 | } |
---|
66 | |
---|
67 | // reverse computation of partials of Taylor coefficients |
---|
68 | CPPAD_TESTVECTOR(double) r(p); |
---|
69 | w[0] = 1.; |
---|
70 | r = f.Reverse(p, w); |
---|
71 | jfac = 1.; |
---|
72 | for(j = 0; j < p; j++) |
---|
73 | { |
---|
74 | double value; |
---|
75 | if( j % 4 == 0 ) |
---|
76 | value = -sin_u; |
---|
77 | else if( j % 4 == 1 ) |
---|
78 | value = -cos_u; |
---|
79 | else if( j % 4 == 2 ) |
---|
80 | value = sin_u; |
---|
81 | else value = cos_u; |
---|
82 | |
---|
83 | ok &= NearEqual(jfac*r[j], value, 1e-10 , 1e-10); // d^jz/du^j |
---|
84 | |
---|
85 | jfac *= (j + 1); |
---|
86 | } |
---|
87 | |
---|
88 | return ok; |
---|
89 | } |
---|