Changeset 708 for trunk/test_more
 Timestamp:
 Dec 10, 2006 12:06:11 PM (14 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test_more/pow.cpp
r683 r708 11 11 12 12 /* 13 Old example now just used for validation testing.13 Old examples now just used for validation testing. 14 14 */ 15 15 # include <cppad/cppad.hpp> 16 16 17 bool Pow(void) 17 namespace { // BEGIN empty namespace 18 19 bool PowTestOne(void) 20 { bool ok = true; 21 22 using CppAD::AD; 23 using CppAD::NearEqual; 24 25 // domain space vector 26 size_t n = 2; 27 double x = 0.5; 28 double y = 2.; 29 CppADvector< AD<double> > XY(n); 30 XY[0] = x; 31 XY[1] = y; 32 33 // declare independent variables and start tape recording 34 CppAD::Independent(XY); 35 36 // range space vector 37 size_t m = 3; 38 CppADvector< AD<double> > Z(m); 39 Z[0] = CppAD::pow(XY[0], XY[1]); // pow(variable, variable) 40 Z[1] = CppAD::pow(XY[0], y); // pow(variable, parameter) 41 Z[2] = CppAD::pow(x, XY[1]); // pow(parameter, variable) 42 43 // create f: XY > Z and stop tape recording 44 CppAD::ADFun<double> f(XY, Z); 45 46 // check value 47 double check = std::pow(x, y); 48 size_t i; 49 for(i = 0; i < m; i++) 50 ok &= NearEqual(Z[i] , check, 1e10 , 1e10); 51 52 // forward computation of first partial w.r.t. x 53 CppADvector<double> dxy(n); 54 CppADvector<double> dz(m); 55 dxy[0] = 1.; 56 dxy[1] = 0.; 57 dz = f.Forward(1, dxy); 58 check = y * std::pow(x, y1.); 59 ok &= NearEqual(dz[0], check, 1e10, 1e10); 60 ok &= NearEqual(dz[1], check, 1e10, 1e10); 61 ok &= NearEqual(dz[2], 0., 1e10, 1e10); 62 63 // forward computation of first partial w.r.t. y 64 dxy[0] = 0.; 65 dxy[1] = 1.; 66 dz = f.Forward(1, dxy); 67 check = std::log(x) * std::pow(x, y); 68 ok &= NearEqual(dz[0], check, 1e10, 1e10); 69 ok &= NearEqual(dz[1], 0., 1e10, 1e10); 70 ok &= NearEqual(dz[2], check, 1e10, 1e10); 71 72 // reverse computation of derivative of z[0] + z[1] + z[2] 73 CppADvector<double> w(m); 74 CppADvector<double> dw(n); 75 w[0] = 1.; 76 w[1] = 1.; 77 w[2] = 1.; 78 dw = f.Reverse(1, w); 79 check = y * std::pow(x, y1.); 80 ok &= NearEqual(dw[0], 2. * check, 1e10, 1e10); 81 check = std::log(x) * std::pow(x, y); 82 ok &= NearEqual(dw[1], 2. * check, 1e10, 1e10); 83 84 // use a VecAD<Base>::reference object with pow 85 CppAD::VecAD<double> v(2); 86 AD<double> zero(0); 87 AD<double> one(1); 88 v[zero] = XY[0]; 89 v[one] = XY[1]; 90 AD<double> result = CppAD::pow(v[zero], v[one]); 91 ok &= NearEqual(result, Z[0], 1e10, 1e10); 92 93 return ok; 94 } 95 96 bool PowTestTwo(void) 18 97 { bool ok = true; 19 98 … … 104 183 return ok; 105 184 } 185 186 } // END empty namespace 187 188 bool Pow(void) 189 { bool ok = true; 190 ok &= PowTestOne(); 191 ok &= PowTestTwo(); 192 return ok; 193 }
Note: See TracChangeset
for help on using the changeset viewer.