bool PowInt(void) { bool ok = true; using CppAD::pow; using CppAD::exp; using CppAD::log; using namespace CppAD; // independent variable vector, indices, values, and declaration CPPAD_TESTVECTOR(AD<double>) U(1); U[0] = 2.; Independent(U); // dependent variable vector and indices CPPAD_TESTVECTOR(AD<double>) Z(2); // dependent variable values Z[0] = pow(U[0], 5); // x = u^5 Z[1] = pow(U[0], -5); // y = u^{-5} // create f: U -> Z and vectors used for derivative calculations ADFun<double> f(U, Z); CPPAD_TESTVECTOR(double) v( f.Domain() ); CPPAD_TESTVECTOR(double) w( f.Range() ); /* x_u = 5 * u^4 y_u = - 5 * u^{-6} */ // check function values values double u = Value(U[0]); ok &= NearEqual(Z[0] , exp( log(u) * 5.), 1e-10 , 1e-10); ok &= NearEqual(Z[1] , exp( - log(u) * 5.), 1e-10 , 1e-10); // forward computation of partials v[0] = 1.; w = f.Forward(1, v); ok &= NearEqual(w[0] , 5. * exp( log(u) * 4.), 1e-10 , 1e-10); ok &= NearEqual(w[1] , - 5. * exp( - log(u) * 6.), 1e-10 , 1e-10); return ok; }
bool Erf(void) { bool ok = true; using namespace CppAD; using CppAD::atan; using CppAD::exp; using CppAD::sqrt; // Construct function object corresponding to erf CPPAD_TEST_VECTOR< AD<double> > X(1); CPPAD_TEST_VECTOR< AD<double> > Y(1); X[0] = 0.; Independent(X); Y[0] = erf(X[0]); ADFun<double> Erf(X, Y); // vectors to use with function object CPPAD_TEST_VECTOR<double> x(1); CPPAD_TEST_VECTOR<double> y(1); CPPAD_TEST_VECTOR<double> dx(1); CPPAD_TEST_VECTOR<double> dy(1); // check value at zero x[0] = 0.; y = Erf.Forward(0, x); ok &= NearEqual(0., y[0], 4e-4, 0.); // check the derivative of error function dx[0] = 1.; double pi = 4. * atan(1.); double factor = 2. / sqrt( pi ); int i; for(i = -10; i <= 10; i++) { x[0] = i / 4.; y = Erf.Forward(0, x); // check derivative double derf = factor * exp( - x[0] * x[0] ); dy = Erf.Forward(1, dx); ok &= NearEqual(derf, dy[0], 0., 2e-3); // test using erf with AD< AD<double> > AD< AD<double> > X0 = x[0]; AD< AD<double> > Y0 = erf(X0); ok &= ( y[0] == Value( Value(Y0) ) ); } return ok; }
bool VecUnary(void) { using namespace CppAD; using CppAD::abs; using CppAD::sin; using CppAD::atan; using CppAD::cos; using CppAD::exp; using CppAD::log; using CppAD::sqrt; bool ok = true; size_t n = 8; size_t i; CPPAD_TEST_VECTOR< AD<double> > X(n); VecAD<double> Y(n); CPPAD_TEST_VECTOR< AD<double> > Z(n); for(i = 0; i < n; i++) X[i] = int(i); // some compilers require the int here Independent(X); AD<double> j; j = 0.; Y[j] = X[0]; Z[0] = -Y[j]; j = 1.; Y[j] = X[1]; Z[1] = sin( Y[j] ); j = 2.; Y[j] = X[2]; Z[2] = abs( Y[j] ); j = 3.; Y[j] = X[3]; Z[3] = atan( Y[j] ); j = 4.; Y[j] = X[4]; Z[4] = cos( Y[j] ); j = 5.; Y[j] = X[5]; Z[5] = exp( Y[j] ); j = 6.; Y[j] = X[6]; Z[6] = log( Y[j] ); j = 7.; Y[j] = X[7]; Z[7] = sqrt( Y[j] ); ADFun<double> f(X, Z); CPPAD_TEST_VECTOR<double> x(n); CPPAD_TEST_VECTOR<double> z(n); for(i = 0; i < n; i++) x[i] = 2. / double(i + 1); x[7] = abs( x[7] ); z = f.Forward(0, x); ok &= NearEqual(z[0], - x[0], 1e-10, 1e-10); ok &= NearEqual(z[1], sin( x[1] ), 1e-10, 1e-10); ok &= NearEqual(z[2], abs( x[2] ), 1e-10, 1e-10); ok &= NearEqual(z[3], atan(x[3] ), 1e-10, 1e-10); ok &= NearEqual(z[4], cos( x[4] ), 1e-10, 1e-10); ok &= NearEqual(z[5], exp( x[5] ), 1e-10, 1e-10); ok &= NearEqual(z[6], log( x[6] ), 1e-10, 1e-10); ok &= NearEqual(z[7], sqrt(x[7] ), 1e-10, 1e-10); return ok; }
bool ForHess(void) { bool ok = true; using namespace CppAD; using CppAD::exp; using CppAD::sin; using CppAD::cos; using CppAD::NearEqual; double eps99 = 99.0 * std::numeric_limits<double>::epsilon(); size_t i; // create independent variable vector with assigned values CPPAD_TESTVECTOR(double) u0(3); CPPAD_TESTVECTOR(AD<double>) U(3); for(i = 0; i < 3; i++) U[i] = u0[i] = double(i+1); Independent( U ); // define the function CPPAD_TESTVECTOR(AD<double>) Y(2); Y[0] = U[0] * exp( U[1] ); Y[1] = U[1] * sin( U[2] ); // create the function y = F(u) ADFun<double> F(U, Y); // formulas for the upper triangle of Hessian of F_0 CPPAD_TESTVECTOR(double) H0(9); H0[0] = 0.; // d^2 y[0] / d_u[0] d_u[0] H0[1] = exp( u0[1] ); // d^2 y[0] / d_u[0] d_u[1] H0[2] = 0.; // d^2 y[0] / d_u[0] d_u[2] H0[4] = u0[0] * exp( u0[1] ); // d^2 y[0] / d_u[1] d_u[1] H0[5] = 0.; // d^2 y[0] / d_u[1] d_u[2] H0[8] = 0.; // d^2 y[0] / d_u[2] d_u[2] // formulas for the upper triangle of Hessian of F_1 CPPAD_TESTVECTOR(double) H1(9); H1[0] = 0.; // d^2 Y[1] / d_U[0] d_U[0] H1[1] = 0.; // d^2 Y[1] / d_U[0] d_U[1] H1[2] = 0.; // d^2 Y[1] / d_U[0] d_U[2] H1[4] = 0.; // d^2 Y[1] / d_U[1] d_U[1] H1[5] = cos( u0[2] ); // d^2 Y[1] / d_U[1] d_U[2] H1[8] = - u0[1] * sin( u0[2] );// d^2 Y[1] / d_U[2] d_U[2] // Define U(t) = u0 + u1 t + u2 t^2 / 2 CPPAD_TESTVECTOR(double) u1(3); CPPAD_TESTVECTOR(double) u2(3); for(i = 0; i < 3; i++) u1[i] = u2[i] = 0.; size_t j; for(i = 0; i < 3; i++) { // diagonal of Hessians in i-th coordiante direction u1[i] = 1.; F.Forward(1, u1); CPPAD_TESTVECTOR(double) Di = F.Forward(2, u2); ok &= NearEqual( 2. * Di[0] , H0[ i + 3 * i], eps99, eps99); ok &= NearEqual( 2. * Di[1] , H1[ i + 3 * i], eps99, eps99); // for(j = i+1; j < 3; j++) { // cross term in i and j direction u1[j] = 1.; F.Forward(1, u1); CPPAD_TESTVECTOR(double) Cij = F.Forward(2, u2); // diagonal of Hessian in j-th coordinate direction u1[i] = 0.; F.Forward(1, u1); CPPAD_TESTVECTOR(double) Dj = F.Forward(2, u2); // (i, j) elements of the Hessians double H0ij = Cij[0] - Di[0] - Dj[0]; ok &= NearEqual( H0ij, H0[j + 3 * i], eps99, eps99); double H1ij = Cij[1] - Di[1] - Dj[1]; ok &= NearEqual( H1ij, H1[j + 3 * i], eps99, eps99); // reset all components of u1 to zero u1[j] = 0.; } } return ok; }