1 | /* $Id: acos.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 | $begin Acos.cpp$$ |
---|
15 | $spell |
---|
16 | cos |
---|
17 | acos |
---|
18 | $$ |
---|
19 | |
---|
20 | $section The AD acos Function: Example and Test$$ |
---|
21 | |
---|
22 | $index acos, AD example$$ |
---|
23 | $index example, AD acos$$ |
---|
24 | $index test, AD acos$$ |
---|
25 | |
---|
26 | $code |
---|
27 | $verbatim%example/acos.cpp%0%// BEGIN C++%// END C++%1%$$ |
---|
28 | $$ |
---|
29 | |
---|
30 | $end |
---|
31 | */ |
---|
32 | // BEGIN C++ |
---|
33 | |
---|
34 | # include <cppad/cppad.hpp> |
---|
35 | |
---|
36 | bool Acos(void) |
---|
37 | { bool ok = true; |
---|
38 | |
---|
39 | using CppAD::AD; |
---|
40 | using CppAD::NearEqual; |
---|
41 | |
---|
42 | // domain space vector |
---|
43 | size_t n = 1; |
---|
44 | double x0 = 0.5; |
---|
45 | CPPAD_TESTVECTOR(AD<double>) x(n); |
---|
46 | x[0] = x0; |
---|
47 | |
---|
48 | // declare independent variables and start tape recording |
---|
49 | CppAD::Independent(x); |
---|
50 | |
---|
51 | // a temporary value |
---|
52 | AD<double> cos_of_x0 = CppAD::cos(x[0]); |
---|
53 | |
---|
54 | // range space vector |
---|
55 | size_t m = 1; |
---|
56 | CPPAD_TESTVECTOR(AD<double>) y(m); |
---|
57 | y[0] = CppAD::acos(cos_of_x0); |
---|
58 | |
---|
59 | // create f: x -> y and stop tape recording |
---|
60 | CppAD::ADFun<double> f(x, y); |
---|
61 | |
---|
62 | // check value |
---|
63 | ok &= NearEqual(y[0] , x0, 1e-10 , 1e-10); |
---|
64 | |
---|
65 | // forward computation of first partial w.r.t. x[0] |
---|
66 | CPPAD_TESTVECTOR(double) dx(n); |
---|
67 | CPPAD_TESTVECTOR(double) dy(m); |
---|
68 | dx[0] = 1.; |
---|
69 | dy = f.Forward(1, dx); |
---|
70 | ok &= NearEqual(dy[0], 1., 1e-10, 1e-10); |
---|
71 | |
---|
72 | // reverse computation of derivative of y[0] |
---|
73 | CPPAD_TESTVECTOR(double) w(m); |
---|
74 | CPPAD_TESTVECTOR(double) dw(n); |
---|
75 | w[0] = 1.; |
---|
76 | dw = f.Reverse(1, w); |
---|
77 | ok &= NearEqual(dw[0], 1., 1e-10, 1e-10); |
---|
78 | |
---|
79 | // use a VecAD<Base>::reference object with acos |
---|
80 | CppAD::VecAD<double> v(1); |
---|
81 | AD<double> zero(0); |
---|
82 | v[zero] = cos_of_x0; |
---|
83 | AD<double> result = CppAD::acos(v[zero]); |
---|
84 | ok &= NearEqual(result, x0, 1e-10, 1e-10); |
---|
85 | |
---|
86 | return ok; |
---|
87 | } |
---|
88 | |
---|
89 | // END C++ |
---|