1 /* --------------------------------------------------------------------------
2 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-20 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 WARNING: This file is used an an example by omh/FunConstruct.omh.
14 
15 $begin independent.cpp$$
16 
17 $comment ! NOTE the title states that this example is used two places !$$
18 $section Independent and ADFun Constructor: Example and Test$$
19 
20 
21 $srcthisfile%0%// BEGIN C++%// END C++%1%$$
22 
23 $end
24 */
25 // BEGIN C++
26 # include <cppad/cppad.hpp>
27 
28 namespace { // --------------------------------------------------------
29 // define the template function Test<ADVector>(void) in empty namespace
30 template <class ADVector>
Test(void)31 bool Test(void)
32 {   bool ok = true;
33     using CppAD::AD;
34     using CppAD::NearEqual;
35     double eps99 = 99.0 * std::numeric_limits<double>::epsilon();
36 
37     // domain space vector
38     size_t  n  = 2;
39     ADVector X(n);  // ADVector is the template parameter in call to Test
40     X[0] = 0.;
41     X[1] = 1.;
42 
43     // declare independent variables and start recording
44     // use the template parameter ADVector for the vector type
45     CppAD::Independent(X);
46 
47     AD<double> a = X[0] + X[1];      // first AD operation
48     AD<double> b = X[0] * X[1];      // second AD operation
49 
50     // range space vector
51     size_t m = 2;
52     ADVector Y(m);  // ADVector is the template paraemter in call to Test
53     Y[0] = a;
54     Y[1] = b;
55 
56     // create f: X -> Y and stop tape recording
57     // use the template parameter ADVector for the vector type
58     CppAD::ADFun<double> f(X, Y);
59 
60     // check value
61     ok &= NearEqual(Y[0] , 1.,  eps99 , eps99);
62     ok &= NearEqual(Y[1] , 0.,  eps99 , eps99);
63 
64     // compute f(1, 2)
65     CPPAD_TESTVECTOR(double) x(n);
66     CPPAD_TESTVECTOR(double) y(m);
67     x[0] = 1.;
68     x[1] = 2.;
69     y    = f.Forward(0, x);
70     ok &= NearEqual(y[0] , 3.,  eps99 , eps99);
71     ok &= NearEqual(y[1] , 2.,  eps99 , eps99);
72 
73     // compute partial of f w.r.t x[0] at (1, 2)
74     CPPAD_TESTVECTOR(double) dx(n);
75     CPPAD_TESTVECTOR(double) dy(m);
76     dx[0] = 1.;
77     dx[1] = 0.;
78     dy    = f.Forward(1, dx);
79     ok &= NearEqual(dy[0] ,   1.,  eps99 , eps99);
80     ok &= NearEqual(dy[1] , x[1],  eps99 , eps99);
81 
82     // compute partial of f w.r.t x[1] at (1, 2)
83     dx[0] = 0.;
84     dx[1] = 1.;
85     dy    = f.Forward(1, dx);
86     ok &= NearEqual(dy[0] ,   1.,  eps99 , eps99);
87     ok &= NearEqual(dy[1] , x[0],  eps99 , eps99);
88 
89     return ok;
90 }
91 } // End of empty namespace -------------------------------------------
92 
93 # include <vector>
94 # include <valarray>
Independent(void)95 bool Independent(void)
96 {   bool ok = true;
97     typedef CppAD::AD<double> ADdouble;
98     // Run with ADVector equal to three different cases
99     // all of which are Simple Vectors with elements of type AD<double>.
100     ok &= Test< CppAD::vector  <ADdouble> >();
101     ok &= Test< std::vector    <ADdouble> >();
102     ok &= Test< std::valarray  <ADdouble> >();
103     return ok;
104 }
105 
106 // END C++
107