File: independent.cpp

package info (click to toggle)
cppad 2025.00.00.2-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 11,552 kB
  • sloc: cpp: 112,594; sh: 5,972; ansic: 179; python: 71; sed: 12; makefile: 10
file content (102 lines) | stat: -rw-r--r-- 3,000 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------
/*
WARNING: This file is used an an example by fun_construct.

{xrst_begin independent.cpp}

{xrst_comment ! NOTE the title states that this example is used two places !}
Independent and ADFun Constructor: Example and Test
###################################################

{xrst_literal
   // BEGIN C++
   // END C++
}

{xrst_end independent.cpp}
*/
// BEGIN C++
# include <cppad/cppad.hpp>

namespace { // --------------------------------------------------------
// define the template function Test<ADVector>(void) in empty namespace
template <class ADVector>
bool Test(void)
{  bool ok = true;
   using CppAD::AD;
   using CppAD::NearEqual;
   double eps99 = 99.0 * std::numeric_limits<double>::epsilon();

   // domain space vector
   size_t  n  = 2;
   ADVector X(n);  // ADVector is the template parameter in call to Test
   X[0] = 0.;
   X[1] = 1.;

   // declare independent variables and start recording
   // use the template parameter ADVector for the vector type
   CppAD::Independent(X);

   AD<double> a = X[0] + X[1];      // first AD operation
   AD<double> b = X[0] * X[1];      // second AD operation

   // range space vector
   size_t m = 2;
   ADVector Y(m);  // ADVector is the template paraemter in call to Test
   Y[0] = a;
   Y[1] = b;

   // create f: X -> Y and stop tape recording
   // use the template parameter ADVector for the vector type
   CppAD::ADFun<double> f(X, Y);

   // check value
   ok &= NearEqual(Y[0] , 1.,  eps99 , eps99);
   ok &= NearEqual(Y[1] , 0.,  eps99 , eps99);

   // compute f(1, 2)
   CPPAD_TESTVECTOR(double) x(n);
   CPPAD_TESTVECTOR(double) y(m);
   x[0] = 1.;
   x[1] = 2.;
   y    = f.Forward(0, x);
   ok &= NearEqual(y[0] , 3.,  eps99 , eps99);
   ok &= NearEqual(y[1] , 2.,  eps99 , eps99);

   // compute partial of f w.r.t x[0] at (1, 2)
   CPPAD_TESTVECTOR(double) dx(n);
   CPPAD_TESTVECTOR(double) dy(m);
   dx[0] = 1.;
   dx[1] = 0.;
   dy    = f.Forward(1, dx);
   ok &= NearEqual(dy[0] ,   1.,  eps99 , eps99);
   ok &= NearEqual(dy[1] , x[1],  eps99 , eps99);

   // compute partial of f w.r.t x[1] at (1, 2)
   dx[0] = 0.;
   dx[1] = 1.;
   dy    = f.Forward(1, dx);
   ok &= NearEqual(dy[0] ,   1.,  eps99 , eps99);
   ok &= NearEqual(dy[1] , x[0],  eps99 , eps99);

   return ok;
}
} // End of empty namespace -------------------------------------------

# include <vector>
# include <valarray>
bool Independent(void)
{  bool ok = true;
   typedef CppAD::AD<double> ADdouble;
   // Run with ADVector equal to three different cases
   // all of which are Simple Vectors with elements of type AD<double>.
   ok &= Test< CppAD::vector  <ADdouble> >();
   ok &= Test< std::vector    <ADdouble> >();
   ok &= Test< std::valarray  <ADdouble> >();
   return ok;
}

// END C++