Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dfad_dfad_example.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Sacado Package
4 //
5 // Copyright 2006 NTESS and the Sacado contributors.
6 // SPDX-License-Identifier: LGPL-2.1-or-later
7 // *****************************************************************************
8 // @HEADER
9 
10 // dfad_dfad_example
11 //
12 // usage:
13 // dfad_dfad_example
14 //
15 // output:
16 // prints the results of computing the second derivative a simple function // with forward nested forward mode AD using the Sacado::Fad::DFad class
17 // (uses dynamic memory allocation for number of derivative components).
18 
19 #include <iostream>
20 #include <iomanip>
21 
22 #include "Sacado.hpp"
23 
24 // The function to differentiate
25 template <typename ScalarT>
26 ScalarT func(const ScalarT& a, const ScalarT& b, const ScalarT& c) {
27  ScalarT r = c*std::log(b+1.)/std::sin(a);
28  return r;
29 }
30 
31 // The analytic derivative of func(a,b,c) with respect to a and b
32 void func_deriv(double a, double b, double c, double& drda, double& drdb)
33 {
34  drda = -(c*std::log(b+1.)/std::pow(std::sin(a),2.))*std::cos(a);
35  drdb = c / ((b+1.)*std::sin(a));
36 }
37 
38 // The analytic second derivative of func(a,b,c) with respect to a and b
39 void func_deriv2(double a, double b, double c, double& d2rda2, double& d2rdb2,
40  double& d2rdadb)
41 {
42  d2rda2 = c*std::log(b+1.)/std::sin(a) + 2.*(c*std::log(b+1.)/std::pow(std::sin(a),3.))*std::pow(std::cos(a),2.);
43  d2rdb2 = -c / (std::pow(b+1.,2.)*std::sin(a));
44  d2rdadb = -c / ((b+1.)*std::pow(std::sin(a),2.))*std::cos(a);
45 }
46 
47 int main(int argc, char **argv)
48 {
49  double pi = std::atan(1.0)*4.0;
50 
51  // Values of function arguments
52  double a = pi/4;
53  double b = 2.0;
54  double c = 3.0;
55 
56  // Number of independent variables
57  int num_deriv = 2;
58 
59  // Fad objects
61  Sacado::Fad::DFad<DFadType> afad(num_deriv, 0, a);
62  Sacado::Fad::DFad<DFadType> bfad(num_deriv, 1, b);
65 
66  afad.val() = Sacado::Fad::DFad<double>(num_deriv, 0, a);
67  bfad.val() = Sacado::Fad::DFad<double>(num_deriv, 1, b);
68 
69  // Compute function
70  double r = func(a, b, c);
71 
72  // Compute derivative analytically
73  double drda, drdb;
74  func_deriv(a, b, c, drda, drdb);
75 
76  // Compute second derivative analytically
77  double d2rda2, d2rdb2, d2rdadb;
78  func_deriv2(a, b, c, d2rda2, d2rdb2, d2rdadb);
79 
80  // Compute function and derivative with AD
81  rfad = func(afad, bfad, cfad);
82 
83  // Extract value and derivatives
84  double r_ad = rfad.val().val(); // r
85  double drda_ad = rfad.dx(0).val(); // dr/da
86  double drdb_ad = rfad.dx(1).val(); // dr/db
87  double d2rda2_ad = rfad.dx(0).dx(0); // d^2r/da^2
88  double d2rdadb_ad = rfad.dx(0).dx(1); // d^2r/dadb
89  double d2rdbda_ad = rfad.dx(1).dx(0); // d^2r/dbda
90  double d2rdb2_ad = rfad.dx(1).dx(1); // d^2/db^2
91 
92  // Print the results
93  int p = 4;
94  int w = p+7;
95  std::cout.setf(std::ios::scientific);
96  std::cout.precision(p);
97  std::cout << " r = " << std::setw(w) << r << " (original) == "
98  << std::setw(w) << r_ad << " (AD) Error = " << std::setw(w)
99  << r - r_ad << std::endl
100  << " dr/da = " << std::setw(w) << drda << " (analytic) == "
101  << std::setw(w) << drda_ad << " (AD) Error = " << std::setw(w)
102  << drda - drda_ad << std::endl
103  << " dr/db = " << std::setw(w) << drdb << " (analytic) == "
104  << std::setw(w) << drdb_ad << " (AD) Error = " << std::setw(w)
105  << drdb - drdb_ad << std::endl
106  << "d^2r/da^2 = " << std::setw(w) << d2rda2 << " (analytic) == "
107  << std::setw(w) << d2rda2_ad << " (AD) Error = " << std::setw(w)
108  << d2rda2 - d2rda2_ad << std::endl
109  << "d^2r/db^2 = " << std::setw(w) << d2rdb2 << " (analytic) == "
110  << std::setw(w) << d2rdb2_ad << " (AD) Error = " << std::setw(w)
111  << d2rdb2 - d2rdb2_ad << std::endl
112  << "d^2r/dadb = " << std::setw(w) << d2rdadb << " (analytic) == "
113  << std::setw(w) << d2rdadb_ad << " (AD) Error = " << std::setw(w)
114  << d2rdadb - d2rdadb_ad << std::endl
115  << "d^2r/dbda = " << std::setw(w) << d2rdadb << " (analytic) == "
116  << std::setw(w) << d2rdbda_ad << " (AD) Error = " << std::setw(w)
117  << d2rdadb - d2rdbda_ad << std::endl;
118 
119  double tol = 1.0e-14;
120  if (std::fabs(r - r_ad) < tol &&
121  std::fabs(drda - drda_ad) < tol &&
122  std::fabs(drdb - drdb_ad) < tol &&
123  std::fabs(d2rda2 - d2rda2_ad) < tol &&
124  std::fabs(d2rdb2 - d2rdb2_ad) < tol &&
125  std::fabs(d2rdadb - d2rdadb_ad) < tol) {
126  std::cout << "\nExample passed!" << std::endl;
127  return 0;
128  }
129  else {
130  std::cout <<"\nSomething is wrong, example failed!" << std::endl;
131  return 1;
132  }
133 }
const char * p
void func_deriv2(double a, double b, double c, double &d2rda2, double &d2rdb2, double &d2rdadb)
atan(expr.val())
expr expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c *expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr1 c expr2 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 expr2 expr1 expr2 expr1 expr1 expr1 c
int main()
Definition: ad_example.cpp:171
Sacado::Fad::DFad< double > DFadType
void func_deriv(double a, double b, double c, double &drda, double &drdb)
sin(expr.val())
log(expr.val())
const double tol
const T func(int n, T *x)
Definition: ad_example.cpp:29
SACADO_INLINE_FUNCTION mpl::enable_if_c< ExprLevel< Expr< T1 > >::value==ExprLevel< Expr< T2 > >::value, Expr< PowerOp< Expr< T1 >, Expr< T2 > > > >::type pow(const Expr< T1 > &expr1, const Expr< T2 > &expr2)
fabs(expr.val())
cos(expr.val())