|
Sacado Package Browser (Single Doxygen Collection) Version of the Day
|
00001 // $Id$ 00002 // $Source$ 00003 // @HEADER 00004 // *********************************************************************** 00005 // 00006 // Sacado Package 00007 // Copyright (2006) Sandia Corporation 00008 // 00009 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation, 00010 // the U.S. Government retains certain rights in this software. 00011 // 00012 // This library is free software; you can redistribute it and/or modify 00013 // it under the terms of the GNU Lesser General Public License as 00014 // published by the Free Software Foundation; either version 2.1 of the 00015 // License, or (at your option) any later version. 00016 // 00017 // This library is distributed in the hope that it will be useful, but 00018 // WITHOUT ANY WARRANTY; without even the implied warranty of 00019 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00020 // Lesser General Public License for more details. 00021 // 00022 // You should have received a copy of the GNU Lesser General Public 00023 // License along with this library; if not, write to the Free Software 00024 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00025 // USA 00026 // Questions? Contact David M. Gay (dmgay@sandia.gov) or Eric T. Phipps 00027 // (etphipp@sandia.gov). 00028 // 00029 // *********************************************************************** 00030 // @HEADER 00031 00032 // dfad_dfad_example 00033 // 00034 // usage: 00035 // dfad_dfad_example 00036 // 00037 // output: 00038 // prints the results of computing the second derivative a simple function // with forward nested forward mode AD using the Sacado::Fad::DFad class 00039 // (uses dynamic memory allocation for number of derivative components). 00040 00041 #include <iostream> 00042 #include <iomanip> 00043 00044 #include "Sacado.hpp" 00045 00046 // The function to differentiate 00047 template <typename ScalarT> 00048 ScalarT func(const ScalarT& a, const ScalarT& b, const ScalarT& c) { 00049 ScalarT r = c*std::log(b+1.)/std::sin(a); 00050 return r; 00051 } 00052 00053 // The analytic derivative of func(a,b,c) with respect to a and b 00054 void func_deriv(double a, double b, double c, double& drda, double& drdb) 00055 { 00056 drda = -(c*std::log(b+1.)/std::pow(std::sin(a),2))*std::cos(a); 00057 drdb = c / ((b+1.)*std::sin(a)); 00058 } 00059 00060 // The analytic second derivative of func(a,b,c) with respect to a and b 00061 void func_deriv2(double a, double b, double c, double& d2rda2, double& d2rdb2, 00062 double& d2rdadb) 00063 { 00064 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); 00065 d2rdb2 = -c / (std::pow(b+1.,2)*std::sin(a)); 00066 d2rdadb = -c / ((b+1.)*std::pow(std::sin(a),2))*std::cos(a); 00067 } 00068 00069 int main(int argc, char **argv) 00070 { 00071 double pi = std::atan(1.0)*4.0; 00072 00073 // Values of function arguments 00074 double a = pi/4; 00075 double b = 2.0; 00076 double c = 3.0; 00077 00078 // Number of independent variables 00079 int num_deriv = 2; 00080 00081 // Fad objects 00082 Sacado::Fad::DFad< Sacado::Fad::DFad<double> > afad(num_deriv, 0, a); 00083 Sacado::Fad::DFad< Sacado::Fad::DFad<double> > bfad(num_deriv, 1, b); 00084 Sacado::Fad::DFad< Sacado::Fad::DFad<double> > cfad = c; 00085 Sacado::Fad::DFad< Sacado::Fad::DFad<double> > rfad; 00086 00087 afad.val() = Sacado::Fad::DFad<double>(num_deriv, 0, a); 00088 bfad.val() = Sacado::Fad::DFad<double>(num_deriv, 1, b); 00089 00090 // Compute function 00091 double r = func(a, b, c); 00092 00093 // Compute derivative analytically 00094 double drda, drdb; 00095 func_deriv(a, b, c, drda, drdb); 00096 00097 // Compute second derivative analytically 00098 double d2rda2, d2rdb2, d2rdadb; 00099 func_deriv2(a, b, c, d2rda2, d2rdb2, d2rdadb); 00100 00101 // Compute function and derivative with AD 00102 rfad = func(afad, bfad, cfad); 00103 00104 // Extract value and derivatives 00105 double r_ad = rfad.val().val(); // r 00106 double drda_ad = rfad.dx(0).val(); // dr/da 00107 double drdb_ad = rfad.dx(1).val(); // dr/db 00108 double d2rda2_ad = rfad.dx(0).dx(0); // d^2r/da^2 00109 double d2rdadb_ad = rfad.dx(0).dx(1); // d^2r/dadb 00110 double d2rdbda_ad = rfad.dx(1).dx(0); // d^2r/dbda 00111 double d2rdb2_ad = rfad.dx(1).dx(1); // d^2/db^2 00112 00113 // Print the results 00114 int p = 4; 00115 int w = p+7; 00116 std::cout.setf(std::ios::scientific); 00117 std::cout.precision(p); 00118 std::cout << " r = " << std::setw(w) << r << " (original) == " 00119 << std::setw(w) << r_ad << " (AD) Error = " << std::setw(w) 00120 << r - r_ad << std::endl 00121 << " dr/da = " << std::setw(w) << drda << " (analytic) == " 00122 << std::setw(w) << drda_ad << " (AD) Error = " << std::setw(w) 00123 << drda - drda_ad << std::endl 00124 << " dr/db = " << std::setw(w) << drdb << " (analytic) == " 00125 << std::setw(w) << drdb_ad << " (AD) Error = " << std::setw(w) 00126 << drdb - drdb_ad << std::endl 00127 << "d^2r/da^2 = " << std::setw(w) << d2rda2 << " (analytic) == " 00128 << std::setw(w) << d2rda2_ad << " (AD) Error = " << std::setw(w) 00129 << d2rda2 - d2rda2_ad << std::endl 00130 << "d^2r/db^2 = " << std::setw(w) << d2rdb2 << " (analytic) == " 00131 << std::setw(w) << d2rdb2_ad << " (AD) Error = " << std::setw(w) 00132 << d2rdb2 - d2rdb2_ad << std::endl 00133 << "d^2r/dadb = " << std::setw(w) << d2rdadb << " (analytic) == " 00134 << std::setw(w) << d2rdadb_ad << " (AD) Error = " << std::setw(w) 00135 << d2rdadb - d2rdadb_ad << std::endl 00136 << "d^2r/dbda = " << std::setw(w) << d2rdadb << " (analytic) == " 00137 << std::setw(w) << d2rdbda_ad << " (AD) Error = " << std::setw(w) 00138 << d2rdadb - d2rdbda_ad << std::endl; 00139 00140 double tol = 1.0e-14; 00141 if (std::fabs(r - r_ad) < tol && 00142 std::fabs(drda - drda_ad) < tol && 00143 std::fabs(drdb - drdb_ad) < tol && 00144 std::fabs(d2rda2 - d2rda2_ad) < tol && 00145 std::fabs(d2rdb2 - d2rdb2_ad) < tol && 00146 std::fabs(d2rdadb - d2rdadb_ad) < tol) { 00147 std::cout << "\nExample passed!" << std::endl; 00148 return 0; 00149 } 00150 else { 00151 std::cout <<"\nSomething is wrong, example failed!" << std::endl; 00152 return 1; 00153 } 00154 }
1.7.4