1 | /* $Id: copy.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 Copy example now used just for valiadation testing |
---|
15 | */ |
---|
16 | |
---|
17 | # include <cppad/cppad.hpp> |
---|
18 | |
---|
19 | namespace { // begin empty namespace |
---|
20 | |
---|
21 | bool copy_older(void) |
---|
22 | { bool ok = true; |
---|
23 | |
---|
24 | using namespace CppAD; |
---|
25 | |
---|
26 | // independent variable vector, indices, values, and declaration |
---|
27 | CPPAD_TESTVECTOR(AD<double>) U(1); |
---|
28 | size_t is = 0; |
---|
29 | U[is] = 2.; |
---|
30 | Independent(U); |
---|
31 | |
---|
32 | // create an AD<double> that does not depend on s |
---|
33 | AD<double> t = 3.; |
---|
34 | |
---|
35 | // use copy constructor |
---|
36 | AD<double> x(U[is]); |
---|
37 | AD<double> y(t); |
---|
38 | |
---|
39 | // check which are parameters |
---|
40 | ok &= ! Parameter(x); |
---|
41 | ok &= Parameter(y); |
---|
42 | |
---|
43 | // dependent variable vector, indices, and values |
---|
44 | CPPAD_TESTVECTOR(AD<double>) Z(2); |
---|
45 | size_t ix = 0; |
---|
46 | size_t iy = 1; |
---|
47 | Z[ix] = x; |
---|
48 | Z[iy] = y; |
---|
49 | |
---|
50 | // create f: U -> Z and vectors used for derivative calculations |
---|
51 | ADFun<double> f(U, Z); |
---|
52 | CPPAD_TESTVECTOR(double) v( f.Domain() ); |
---|
53 | CPPAD_TESTVECTOR(double) w( f.Range() ); |
---|
54 | |
---|
55 | // check parameters flags |
---|
56 | ok &= ! f.Parameter(ix); |
---|
57 | ok &= f.Parameter(iy); |
---|
58 | |
---|
59 | // check function values |
---|
60 | ok &= ( Z[ix] == 2. ); |
---|
61 | ok &= ( Z[iy] == 3. ); |
---|
62 | |
---|
63 | // forward computation of partials w.r.t. s |
---|
64 | v[is] = 1.; |
---|
65 | w = f.Forward(1, v); |
---|
66 | ok &= ( w[ix] == 1. ); // dx/ds |
---|
67 | ok &= ( w[iy] == 0. ); // dy/ds |
---|
68 | |
---|
69 | return ok; |
---|
70 | } |
---|
71 | |
---|
72 | bool copy_ad(void) |
---|
73 | { bool ok = true; // initialize test result flag |
---|
74 | using CppAD::AD; // so can use AD in place of CppAD::AD |
---|
75 | |
---|
76 | // domain space vector |
---|
77 | size_t n = 1; |
---|
78 | CPPAD_TESTVECTOR(AD<double>) x(n); |
---|
79 | x[0] = 2.; |
---|
80 | |
---|
81 | // declare independent variables and start tape recording |
---|
82 | CppAD::Independent(x); |
---|
83 | |
---|
84 | // create an AD<double> that does not depend on x |
---|
85 | AD<double> b = 3.; |
---|
86 | |
---|
87 | // use copy constructor |
---|
88 | AD<double> u(x[0]); |
---|
89 | AD<double> v = b; |
---|
90 | |
---|
91 | // check which are parameters |
---|
92 | ok &= Variable(u); |
---|
93 | ok &= Parameter(v); |
---|
94 | |
---|
95 | // range space vector |
---|
96 | size_t m = 2; |
---|
97 | CPPAD_TESTVECTOR(AD<double>) y(m); |
---|
98 | y[0] = u; |
---|
99 | y[1] = v; |
---|
100 | |
---|
101 | // create f: x -> y and vectors used for derivative calculations |
---|
102 | CppAD::ADFun<double> f(x, y); |
---|
103 | CPPAD_TESTVECTOR(double) dx(n); |
---|
104 | CPPAD_TESTVECTOR(double) dy(m); |
---|
105 | |
---|
106 | // check parameters flags |
---|
107 | ok &= ! f.Parameter(0); |
---|
108 | ok &= f.Parameter(1); |
---|
109 | |
---|
110 | // check function values |
---|
111 | ok &= ( y[0] == 2. ); |
---|
112 | ok &= ( y[1] == 3. ); |
---|
113 | |
---|
114 | // forward computation of partials w.r.t. x[0] |
---|
115 | dx[0] = 1.; |
---|
116 | dy = f.Forward(1, dx); |
---|
117 | ok &= ( dy[0] == 1. ); // du / dx |
---|
118 | ok &= ( dy[1] == 0. ); // dv / dx |
---|
119 | |
---|
120 | return ok; |
---|
121 | } |
---|
122 | bool copy_base(void) |
---|
123 | { bool ok = true; // initialize test result flag |
---|
124 | using CppAD::AD; // so can use AD in place of CppAD::AD |
---|
125 | |
---|
126 | // construct directly from Base where Base is double |
---|
127 | AD<double> x(1.); |
---|
128 | |
---|
129 | // construct from a type that converts to Base where Base is double |
---|
130 | AD<double> y = 2; |
---|
131 | |
---|
132 | // construct from a type that converts to Base where Base = AD<double> |
---|
133 | AD< AD<double> > z(3); |
---|
134 | |
---|
135 | // check that resulting objects are parameters |
---|
136 | ok &= Parameter(x); |
---|
137 | ok &= Parameter(y); |
---|
138 | ok &= Parameter(z); |
---|
139 | |
---|
140 | // check values of objects (compare AD<double> with double) |
---|
141 | ok &= ( x == 1.); |
---|
142 | ok &= ( y == 2.); |
---|
143 | ok &= ( Value(z) == 3.); |
---|
144 | |
---|
145 | // user constructor through the static_cast template function |
---|
146 | x = static_cast < AD<double> >( 4 ); |
---|
147 | z = static_cast < AD< AD<double> > >( 5 ); |
---|
148 | |
---|
149 | ok &= ( x == 4. ); |
---|
150 | ok &= ( Value(z) == 5. ); |
---|
151 | |
---|
152 | return ok; |
---|
153 | } |
---|
154 | bool default_ctor(void) |
---|
155 | { bool ok = true; |
---|
156 | using CppAD::AD; |
---|
157 | |
---|
158 | // default AD constructor |
---|
159 | AD<double> x, y; |
---|
160 | |
---|
161 | // check that they are parameters |
---|
162 | ok &= Parameter(x); |
---|
163 | ok &= Parameter(y); |
---|
164 | |
---|
165 | // assign them values |
---|
166 | x = 3.; |
---|
167 | y = 4.; |
---|
168 | |
---|
169 | // just check a simple operation |
---|
170 | ok &= (x + y == 7.); |
---|
171 | |
---|
172 | return ok; |
---|
173 | } |
---|
174 | |
---|
175 | // END PROGRAM |
---|
176 | } // end empty namespace |
---|
177 | bool copy(void) |
---|
178 | { bool ok = true; |
---|
179 | ok &= copy_older(); |
---|
180 | ok &= copy_ad(); |
---|
181 | ok &= copy_base(); |
---|
182 | ok &= default_ctor(); |
---|
183 | return ok; |
---|
184 | } |
---|