00001 // $Id: sparse_example.cpp,v 1.1 2008/06/16 17:07:18 etphipp Exp $ 00002 // $Source: /space/CVS/Trilinos/packages/sacado/example/sparse_example.cpp,v $ 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_example 00033 // 00034 // usage: 00035 // dfad_example 00036 // 00037 // output: 00038 // prints the results of differentiating a simple function with forward 00039 // mode AD using the Sacado::Fad::DFad class (uses dynamic memory 00040 // allocation for number of derivative components). 00041 00042 #include <iostream> 00043 #include <iomanip> 00044 00045 #include "Sacado.hpp" 00046 00047 // The function to differentiate 00048 template <typename ScalarT> 00049 ScalarT func(const ScalarT& a, const ScalarT& b, const ScalarT& c) { 00050 ScalarT r = std::log(b+1.)/std::sin(a); 00051 00052 return r; 00053 } 00054 00055 typedef Sacado::LFad::LogicalSparse<double,bool> FadType; 00056 00057 int main(int argc, char **argv) 00058 { 00059 double pi = std::atan(1.0)*4.0; 00060 00061 // Values of function arguments 00062 double a = pi/4; 00063 double b = 2.0; 00064 double c = 3.0; 00065 00066 // Number of independent variables 00067 int num_deriv = 3; 00068 00069 // Fad objects 00070 FadType afad(num_deriv, 0, a); // First (0) indep. var 00071 FadType bfad(num_deriv, 1, b); // Second (1) indep. var 00072 FadType cfad(num_deriv, 2, c); // Third (2) indep. var 00073 FadType rfad; // Result 00074 00075 // Compute function 00076 double r = func(a, b, c); 00077 00078 // Compute function and derivative with AD 00079 rfad = func(afad, bfad, cfad); 00080 00081 std::cout << rfad << std::endl; 00082 00083 // Extract value and derivatives 00084 double r_ad = rfad.val(); // r 00085 bool drda_ad = rfad.dx(0); // dr/da 00086 bool drdb_ad = rfad.dx(1); // dr/db 00087 bool drdc_ad = rfad.dx(2); // dr/dc 00088 00089 double tol = 1.0e-14; 00090 if (std::fabs(r - r_ad) < tol && drda_ad && drdb_ad && !drdc_ad) { 00091 std::cout << "\nExample passed!" << std::endl; 00092 return 0; 00093 } 00094 else { 00095 std::cout <<"\nSomething is wrong, example failed!" << std::endl; 00096 return 1; 00097 } 00098 }
1.4.7