1 /* --------------------------------------------------------------------------
2 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-17 Bradley M. Bell
3
4 CppAD is distributed under the terms of the
5 Eclipse Public License Version 2.0.
6
7 This Source Code may also be made available under the following
8 Secondary License when the conditions for such availability set forth
9 in the Eclipse Public License, Version 2.0 are satisfied:
10 GNU General Public License, Version 2.0 or later.
11 ---------------------------------------------------------------------------- */
12
13 /*
14 Test the use of the special parameters zero and one with the multiply operator
15 */
16
17 # include <cppad/cppad.hpp>
18
19 typedef CppAD::AD<double> ADdouble;
20 typedef CppAD::AD< ADdouble > ADDdouble;
21
MulZeroOne(void)22 bool MulZeroOne(void)
23 {
24 using namespace CppAD;
25
26 bool ok = true;
27
28 size_t i;
29 for(i = 0; i < 3; i++)
30 { // run through the cases x = 0, 1, 2
31
32 size_t j;
33 for(j = 0; j < 3; j++)
34 { // run through the cases y = 0, 1, 2
35
36 CPPAD_TESTVECTOR( ADdouble ) x(1);
37 x[0] = double(i);
38 Independent(x);
39
40 CPPAD_TESTVECTOR( ADDdouble ) y(1);
41 y[0] = ADDdouble(j);
42 Independent(y);
43
44 CPPAD_TESTVECTOR( ADDdouble ) z(2);
45 z[0] = x[0] * y[0];
46 z[1] = y[0] * x[0];
47 z[1] *= x[0];
48
49 // f(y) = z = { x * y , y * x * x }
50 ADFun< ADdouble > f(y, z);
51 CPPAD_TESTVECTOR( ADdouble ) u( f.Domain() );
52 CPPAD_TESTVECTOR( ADdouble ) v( f.Range() );
53
54 // v = f'(y)
55 u[0] = ADdouble(1.);
56 v = f.Forward(1, u);
57
58 // check derivatives of f
59 ok &= v[0] == x[0];
60 ok &= v[1] == x[0] * x[0];
61
62 // g(x) = f'(y) = {x , x * x}
63 ADFun<double> g(x, v);
64 CPPAD_TESTVECTOR( double ) a( g.Domain() );
65 CPPAD_TESTVECTOR( double ) b( g.Range() );
66
67 // b = g'(x)
68 a[0] = 1.;
69 b = g.Forward(1, a);
70
71 // check derivatives of g
72 ok &= (b[0] == 1.);
73 ok &= (b[1] == 2. * x[0]);
74 }
75 }
76
77 return ok;
78 }
79