File: ad_fun.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 (122 lines) | stat: -rw-r--r-- 3,264 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// 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
// ----------------------------------------------------------------------------

/*
{xrst_begin ad_fun.cpp}

Creating Your Own Interface to an ADFun Object
##############################################

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

{xrst_end ad_fun.cpp}
*/
// BEGIN C++

# include <cppad/cppad.hpp>

namespace {

   // This class is an example of a different interface to an AD function object
   template <class Base>
   class my_ad_fun {

   private:
      CppAD::ADFun<Base> f;

   public:
      // default constructor
      my_ad_fun(void)
      { }

      // destructor
      ~ my_ad_fun(void)
      { }

      // Construct an my_ad_fun object with an operation sequence.
      // This is the same as for ADFun<Base> except that no zero
      // order forward sweep is done. Note Hessian and Jacobian do
      // their own zero order forward mode sweep.
      template <class ADvector>
      my_ad_fun(const ADvector& x, const ADvector& y)
      {  f.Dependent(x, y); }

      // same as ADFun<Base>::Jacobian
      template <class BaseVector>
      BaseVector jacobian(const BaseVector& x)
      {  return f.Jacobian(x); }

      // same as ADFun<Base>::Hessian
         template <class BaseVector>
      BaseVector hessian(const BaseVector &x, const BaseVector &w)
      {  return f.Hessian(x, w); }
   };

} // End empty namespace

bool ad_fun(void)
{  // This example is similar to example/jacobian.cpp, except that it
   // uses my_ad_fun instead of ADFun.

   bool ok = true;
   using CppAD::AD;
   using CppAD::NearEqual;
   double eps99 = 99.0 * std::numeric_limits<double>::epsilon();
   using CppAD::exp;
   using CppAD::sin;
   using CppAD::cos;

   // domain space vector
   size_t n = 2;
   CPPAD_TESTVECTOR(AD<double>)  X(n);
   X[0] = 1.;
   X[1] = 2.;

   // declare independent variables and starting recording
   CppAD::Independent(X);

   // a calculation between the domain and range values
   AD<double> Square = X[0] * X[0];

   // range space vector
   size_t m = 3;
   CPPAD_TESTVECTOR(AD<double>)  Y(m);
   Y[0] = Square * exp( X[1] );
   Y[1] = Square * sin( X[1] );
   Y[2] = Square * cos( X[1] );

   // create f: X -> Y and stop tape recording
   my_ad_fun<double> f(X, Y);

   // new value for the independent variable vector
   CPPAD_TESTVECTOR(double) x(n);
   x[0] = 2.;
   x[1] = 1.;

   // compute the derivative at this x
   CPPAD_TESTVECTOR(double) jac( m * n );
   jac = f.jacobian(x);

   /*
   F'(x) = [ 2 * x[0] * exp(x[1]) ,  x[0] * x[0] * exp(x[1]) ]
         [ 2 * x[0] * sin(x[1]) ,  x[0] * x[0] * cos(x[1]) ]
         [ 2 * x[0] * cos(x[1]) , -x[0] * x[0] * sin(x[i]) ]
   */
   ok &=  NearEqual( 2.*x[0]*exp(x[1]), jac[0*n+0], eps99, eps99);
   ok &=  NearEqual( 2.*x[0]*sin(x[1]), jac[1*n+0], eps99, eps99);
   ok &=  NearEqual( 2.*x[0]*cos(x[1]), jac[2*n+0], eps99, eps99);

   ok &=  NearEqual( x[0] * x[0] *exp(x[1]), jac[0*n+1], eps99, eps99);
   ok &=  NearEqual( x[0] * x[0] *cos(x[1]), jac[1*n+1], eps99, eps99);
   ok &=  NearEqual(-x[0] * x[0] *sin(x[1]), jac[2*n+1], eps99, eps99);

   return ok;
}


// END C++