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; }
CppAD::ADFun<T>* AtanTestTwoFunc(const std::vector<CppAD::AD<T> >& u) { using CppAD::atan; using CppAD::sin; using CppAD::cos; using namespace CppAD; assert(u.size() == 1); // a temporary values AD<T> x = sin(u[0]) / cos(u[0]); // dependent variable vector std::vector< AD<T> > Z(1); Z[0] = atan(x); // atan( tan(u) ) // create f: U -> Z and vectors used for derivative calculations return new ADFun<T > (u, Z); }
CppAD::ADFun<T>* AtanTestOneFunc(const std::vector<CppAD::AD<T> >& u) { using CppAD::atan; using namespace CppAD; assert(u.size() == 1); size_t s = 0; // some temporary values AD<T> x = cos(u[s]); AD<T> y = sin(u[s]); AD<T> z = y / x; // tan(s) // dependent variable vector and indices std::vector< AD<T> > Z(1); size_t a = 0; // dependent variable values Z[a] = atan(z); // atan( tan(s) ) // create f: U -> Z and vectors used for derivative calculations return new ADFun<T > (u, Z); }
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; }