Sacado Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
sparse_example.cpp
Go to the documentation of this file.
1 // $Id$
2 // $Source$
3 // @HEADER
4 // ***********************************************************************
5 //
6 // Sacado Package
7 // Copyright (2006) Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // This library is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as
14 // published by the Free Software Foundation; either version 2.1 of the
15 // License, or (at your option) any later version.
16 //
17 // This library is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 // Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License along with this library; if not, write to the Free Software
24 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
25 // USA
26 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps
27 // (etphipp@sandia.gov).
28 //
29 // ***********************************************************************
30 // @HEADER
31 
32 // dfad_example
33 //
34 // usage:
35 // dfad_example
36 //
37 // output:
38 // prints the results of differentiating a simple function with forward
39 // mode AD using the Sacado::Fad::DFad class (uses dynamic memory
40 // allocation for number of derivative components).
41 
42 #include <iostream>
43 #include <iomanip>
44 
45 #include "Sacado_No_Kokkos.hpp"
46 
47 // The function to differentiate
48 template <typename ScalarT>
49 ScalarT func(const ScalarT& a, const ScalarT& b, const ScalarT& c) {
50  ScalarT r = std::log(b+1.)/std::sin(a);
51 
52  return r;
53 }
54 
56 
57 int main(int argc, char **argv)
58 {
59  double pi = std::atan(1.0)*4.0;
60 
61  // Values of function arguments
62  double a = pi/4;
63  double b = 2.0;
64  double c = 3.0;
65 
66  // Number of independent variables
67  int num_deriv = 3;
68 
69  // Fad objects
70  FadType afad(num_deriv, 0, a); // First (0) indep. var
71  FadType bfad(num_deriv, 1, b); // Second (1) indep. var
72  FadType cfad(num_deriv, 2, c); // Third (2) indep. var
73  FadType rfad; // Result
74 
75  // Compute function
76  double r = func(a, b, c);
77 
78  // Compute function and derivative with AD
79  rfad = func(afad, bfad, cfad);
80 
81  std::cout << rfad << std::endl;
82 
83  // Extract value and derivatives
84  double r_ad = rfad.val(); // r
85  bool drda_ad = rfad.dx(0); // dr/da
86  bool drdb_ad = rfad.dx(1); // dr/db
87  bool drdc_ad = rfad.dx(2); // dr/dc
88 
89  double tol = 1.0e-14;
90  if (std::fabs(r - r_ad) < tol && drda_ad && drdb_ad && !drdc_ad) {
91  std::cout << "\nExample passed!" << std::endl;
92  return 0;
93  }
94  else {
95  std::cout <<"\nSomething is wrong, example failed!" << std::endl;
96  return 1;
97  }
98 }
Sacado::Fad::DFad< double > FadType
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:191
sin(expr.val())
log(expr.val())
const double tol
const T func(int n, T *x)
Definition: ad_example.cpp:49
fabs(expr.val())