Changeset 3495 for trunk/test_more
 Timestamp:
 Dec 23, 2014 8:16:15 PM (5 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/test_more/erf.cpp
r3060 r3495 23 23 using CppAD::exp; 24 24 using CppAD::sqrt; 25 # if CPPAD_COMPILER_HAS_ERF 26 double eps = 100.0 * std::numeric_limits<double>::epsilon(); 27 # endif 28 // Construct function object corresponding to erf 29 CPPAD_TESTVECTOR(AD<double>) ax(1); 30 CPPAD_TESTVECTOR(AD<double>) ay(1); 31 ax[0] = 0.; 32 Independent(ax); 33 ay[0] = erf(ax[0]); 34 ADFun<double> f(ax, ay); 25 35 26 // Construct function object corresponding to erf 27 CPPAD_TESTVECTOR(AD<double>) X(1); 28 CPPAD_TESTVECTOR(AD<double>) Y(1); 29 X[0] = 0.; 30 Independent(X); 31 Y[0] = erf(X[0]); 32 ADFun<double> Erf(X, Y); 36 // Construct function object corresponding to derivative of erf 37 Independent(ax); 38 double pi = 4.0 * atan(1.0); 39 ay[0] = exp(  ax[0] * ax[0] ) * 2.0 / sqrt(pi); 40 ADFun<double> df(ax, ay); 33 41 34 42 // vectors to use with function object 35 CPPAD_TESTVECTOR(double) x(1); 36 CPPAD_TESTVECTOR(double) y(1); 37 CPPAD_TESTVECTOR(double) dx(1); 38 CPPAD_TESTVECTOR(double) dy(1); 43 CPPAD_TESTVECTOR(double) x0(1), y0(1), x1(1), y1(1), check(1); 39 44 40 45 // check value at zero 41 x[0] = 0.; 42 y = Erf.Forward(0, x); 43 ok &= NearEqual(0., y[0], 4e4, 0.); 46 x0[0] = 1.5; 47 y0 = f.Forward(0, x0); 48 check[0] = 0.96611; 49 ok &= std::fabs(check[0]  y0[0]) <= 4e4; 44 50 45 51 // check the derivative of error function 46 dx[0] = 1.;47 double pi = 4. * atan(1.);48 double factor = 2. / sqrt( pi);49 int i;50 for(i = 10; i <= 10; i++) 51 { x[0] = i / 4.;52 y = Erf.Forward(0, x); 52 x1[0] = 1.0; 53 y1 = f.Forward(1, x1); 54 check = df.Forward(0, x0); 55 ok &= NearEqual(check[0], y1[0], 0., 2e3); 56 # if CPPAD_COMPILER_HAS_ERF 57 ok &= NearEqual(check[0], y1[0], eps, eps); 58 # endif 53 59 54 // check derivative 55 double derf = factor * exp(  x[0] * x[0] ); 56 dy = Erf.Forward(1, dx); 57 ok &= NearEqual(derf, dy[0], 0., 2e3); 60 // check second derivative 61 CPPAD_TESTVECTOR(double) x2(1), y2(1); 62 x2[0] = 0.0; 63 y2 = f.Forward(2, x2); 64 check = df.Forward(1, x1) / 2.0; 65 ok &= NearEqual(check[0], y2[0], 0., 2e3); 66 # if CPPAD_COMPILER_HAS_ERF 67 ok &= NearEqual(check[0], y2[0], eps, eps); 68 # endif 58 69 59 // test using erf with AD< AD<double> > 60 AD< AD<double> > X0 = AD<double>( x[0] ); 61 AD< AD<double> > Y0 = erf(X0); 70 // check third derivative 71 CPPAD_TESTVECTOR(double) x3(1), y3(1); 72 x3[0] = 0.0; 73 y3 = f.Forward(3, x3); 74 check = df.Forward(2, x2) / 3.0; 75 ok &= NearEqual(check[0], y3[0], 0., 2e3); 76 # if CPPAD_COMPILER_HAS_ERF 77 ok &= NearEqual(check[0], y3[0], eps, eps); 78 # endif 62 79 63 ok &= ( y[0] == Value( Value(Y0) ) ); 64 } 80 // check 4th order of reverse mode 81 CPPAD_TESTVECTOR(double) w(1), dy(4), x4(1), y4(1); 82 x4[0] = 0.0; 83 w[0] = 1.0; 84 dy = f.Reverse(4, w); 85 y4 = f.Forward(4, x4); 86 // 87 ok &= NearEqual(dy[0], y1[0], 0., 2e3); 88 # if CPPAD_COMPILER_HAS_ERF 89 ok &= NearEqual(dy[0], y1[0], eps, eps); 90 # endif 91 // 92 ok &= NearEqual(dy[1], 2.0 * y2[0], 0., 2e3); 93 # if CPPAD_COMPILER_HAS_ERF 94 ok &= NearEqual(dy[1], 2.0 * y2[0], eps, eps); 95 # endif 96 // 97 ok &= NearEqual(dy[2], 3.0 * y3[0], 0., 2e3); 98 # if CPPAD_COMPILER_HAS_ERF 99 ok &= NearEqual(dy[2], 3.0 * y3[0], eps, eps); 100 # endif 101 // 102 ok &= NearEqual(dy[3], 4.0 * y4[0], 0., 2e3); 103 # if CPPAD_COMPILER_HAS_ERF 104 ok &= NearEqual(dy[3], 4.0 * y4[0], eps, eps); 105 # endif 106 65 107 return ok; 66 108 }
Note: See TracChangeset
for help on using the changeset viewer.