1 | /* $Id: assign.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 | Old example, now used just for validation testing |
---|
14 | */ |
---|
15 | |
---|
16 | # include <cppad/cppad.hpp> |
---|
17 | |
---|
18 | bool assign(void) |
---|
19 | { bool ok = true; |
---|
20 | using CppAD::AD; |
---|
21 | |
---|
22 | // domain space vector |
---|
23 | size_t n = 3; |
---|
24 | CPPAD_TESTVECTOR(AD<double>) x(n); |
---|
25 | x[0] = 2; // AD<double> = int |
---|
26 | x[1] = 3.; // AD<double> = double |
---|
27 | x[2] = x[1]; // AD<double> = AD<double> |
---|
28 | |
---|
29 | // declare independent variables and start tape recording |
---|
30 | CppAD::Independent(x); |
---|
31 | |
---|
32 | // range space vector |
---|
33 | size_t m = 3; |
---|
34 | CPPAD_TESTVECTOR(AD<double>) y(m); |
---|
35 | |
---|
36 | // assign an AD<Base> object equal to an independent variable |
---|
37 | // (choose the first independent variable to check a special case) |
---|
38 | // use the value returned by the assignment (for another assignment) |
---|
39 | y[0] = y[1] = x[0]; |
---|
40 | |
---|
41 | // assign an AD<Base> object equal to an expression |
---|
42 | y[1] = x[1] + 1.; |
---|
43 | y[2] = x[2] + 2.; |
---|
44 | |
---|
45 | // check that all the resulting components of y depend on x |
---|
46 | ok &= Variable(y[0]); // y[0] = x[0] |
---|
47 | ok &= Variable(y[1]); // y[1] = x[1] + 1 |
---|
48 | ok &= Variable(y[2]); // y[2] = x[2] + 2 |
---|
49 | |
---|
50 | // construct f : x -> y and stop the tape recording |
---|
51 | CppAD::ADFun<double> f(x, y); |
---|
52 | |
---|
53 | // check variable values |
---|
54 | ok &= ( y[0] == 2.); |
---|
55 | ok &= ( y[1] == 4.); |
---|
56 | ok &= ( y[2] == 5.); |
---|
57 | |
---|
58 | // compute partials w.r.t x[1] |
---|
59 | CPPAD_TESTVECTOR(double) dx(n); |
---|
60 | CPPAD_TESTVECTOR(double) dy(m); |
---|
61 | dx[0] = 0.; |
---|
62 | dx[1] = 1.; |
---|
63 | dx[2] = 0.; |
---|
64 | dy = f.Forward(1, dx); |
---|
65 | ok &= (dy[0] == 0.); // dy[0] / dx[1] |
---|
66 | ok &= (dy[1] == 1.); // dy[1] / dx[1] |
---|
67 | ok &= (dy[2] == 0.); // dy[2] / dx[1] |
---|
68 | |
---|
69 | // compute the derivative y[2] |
---|
70 | CPPAD_TESTVECTOR(double) w(m); |
---|
71 | CPPAD_TESTVECTOR(double) dw(n); |
---|
72 | w[0] = 0.; |
---|
73 | w[1] = 0.; |
---|
74 | w[2] = 1.; |
---|
75 | dw = f.Reverse(1, w); |
---|
76 | ok &= (dw[0] == 0.); // dy[2] / dx[0] |
---|
77 | ok &= (dw[1] == 0.); // dy[2] / dx[1] |
---|
78 | ok &= (dw[2] == 1.); // dy[2] / dx[2] |
---|
79 | |
---|
80 | // assign a VecAD<Base>::reference |
---|
81 | CppAD::VecAD<double> v(1); |
---|
82 | AD<double> zero(0); |
---|
83 | v[zero] = 5.; |
---|
84 | ok &= (v[0] == 5.); |
---|
85 | |
---|
86 | return ok; |
---|
87 | } |
---|