<?xml version='1.0'?> <?xml-stylesheet type='text/xsl' href='pmathml.xsl'?> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <title>Second Partials Reverse Driver: Example and Test</title> <meta name="description" id="description" content="Second Partials Reverse Driver: Example and Test"/> <meta name="keywords" id="keywords" content=" second partial example test "/> <style type='text/css'> body { color : black } body { background-color : white } A:link { color : blue } A:visited { color : purple } A:active { color : purple } </style> <script type='text/javascript' language='JavaScript' src='_revtwo.cpp_xml.js'> </script> </head> <body> <table><tr> <td> <a href="http://www.coin-or.org/CppAD/" target="_top"><img border="0" src="_image.gif"/></a> </td> <td><a href="revtwo.xml" target="_top">Prev</a> </td><td><a href="sparse_jacobian.xml" target="_top">Next</a> </td><td> <select onchange='choose_across0(this)'> <option>Index-></option> <option>contents</option> <option>reference</option> <option>index</option> <option>search</option> <option>external</option> </select> </td> <td> <select onchange='choose_up0(this)'> <option>Up-></option> <option>CppAD</option> <option>ADFun</option> <option>Drivers</option> <option>RevTwo</option> <option>RevTwo.cpp</option> </select> </td> <td> <select onchange='choose_down3(this)'> <option>ADFun-></option> <option>Independent</option> <option>FunConstruct</option> <option>Dependent</option> <option>abort_recording</option> <option>seq_property</option> <option>FunEval</option> <option>Drivers</option> <option>FunCheck</option> <option>omp_max_thread</option> <option>optimize</option> <option>FunDeprecated</option> </select> </td> <td> <select onchange='choose_down2(this)'> <option>Drivers-></option> <option>Jacobian</option> <option>ForOne</option> <option>RevOne</option> <option>Hessian</option> <option>ForTwo</option> <option>RevTwo</option> <option>sparse_jacobian</option> <option>sparse_hessian</option> </select> </td> <td> <select onchange='choose_down1(this)'> <option>RevTwo-></option> <option>RevTwo.cpp</option> </select> </td> <td>RevTwo.cpp</td> <td>Headings</td> </tr></table><br/> <center><b><big><big>Second Partials Reverse Driver: Example and Test</big></big></b></center> <code><font color="blue"><pre style='display:inline'> # include <cppad/cppad.hpp> namespace { // ----------------------------------------------------- // define the template function in empty namespace // bool RevTwoCases<VectorBase, VectorSize_t>(void) template <class VectorBase, class VectorSize_t> bool RevTwoCases() { bool ok = true; using CppAD::AD; using CppAD::NearEqual; using CppAD::exp; using CppAD::sin; using CppAD::cos; // domain space vector size_t n = 2; <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>< <a href="ad.xml" target="_top">AD</a><double> > X(n); X[0] = 1.; X[1] = 2.; // declare independent variables and starting recording CppAD::<a href="independent.xml" target="_top">Independent</a>(X); // a calculation between the domain and range values <a href="ad.xml" target="_top">AD</a><double> Square = X[0] * X[0]; // range space vector size_t m = 3; <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>< <a href="ad.xml" target="_top">AD</a><double> > Y(m); Y[0] = Square * exp( X[1] ); Y[1] = Square * sin( X[1] ); Y[2] = Square * cos( X[1] ); // create f: X -> Y and stop tape recording CppAD::<a href="funconstruct.xml" target="_top">ADFun</a><double> f(X, Y); // new value for the independent variable vector VectorBase x(n); x[0] = 2.; x[1] = 1.; // set i and j to compute specific second partials of y size_t p = 2; VectorSize_t i(p); VectorSize_t j(p); i[0] = 0; j[0] = 0; // for partials y[0] w.r.t x[0] and x[k] i[1] = 1; j[1] = 1; // for partials y[1] w.r.t x[1] and x[k] // compute the second partials VectorBase ddw(n * p); ddw = f.RevTwo(x, i, j); // partials of y[0] w.r.t x[0] is 2 * x[0] * exp(x[1]) // check partials of y[0] w.r.t x[0] and x[k] for k = 0, 1 ok &= <a href="nearequal.xml" target="_top">NearEqual</a>( 2.*exp(x[1]), ddw[0*p+0], 1e-10, 1e-10 ); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>( 2.*x[0]*exp(x[1]), ddw[1*p+0], 1e-10, 1e-10 ); // partials of y[1] w.r.t x[1] is x[0] * x[0] * cos(x[1]) // check partials of F_1 w.r.t x[1] and x[k] for k = 0, 1 ok &= <a href="nearequal.xml" target="_top">NearEqual</a>( 2.*x[0]*cos(x[1]), ddw[0*p+1], 1e-10, 1e-10 ); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>( -x[0]*x[0]*sin(x[1]), ddw[1*p+1], 1e-10, 1e-10 ); return ok; } } // End empty namespace # include <vector> # include <valarray> bool RevTwo(void) { bool ok = true; // Run with VectorBase equal to three different cases // all of which are Simple Vectors with elements of type double. ok &= RevTwoCases< CppAD::vector <double>, std::vector<size_t> >(); ok &= RevTwoCases< std::vector <double>, std::vector<size_t> >(); ok &= RevTwoCases< std::valarray <double>, std::vector<size_t> >(); // Run with VectorSize_t equal to two other cases // which are Simple Vectors with elements of type size_t. ok &= RevTwoCases< std::vector <double>, CppAD::vector<size_t> >(); ok &= RevTwoCases< std::vector <double>, std::valarray<size_t> >(); return ok; }</pre> </font></code> <hr/>Input File: example/rev_two.cpp </body> </html>