<?xml version='1.0'?> <?xml-stylesheet type='text/xsl' href='pmathml.xsl'?> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <title>Independent and ADFun Constructor: Example and Test</title> <meta name="description" id="description" content="Independent and ADFun Constructor: Example and Test"/> <meta name="keywords" id="keywords" content=" Independent 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='_independent.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="independent.xml" target="_top">Prev</a> </td><td><a href="funconstruct.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>Independent</option> <option>Independent.cpp</option> </select> </td> <td> <select onchange='choose_down3(this)'> <option>CppAD-></option> <option>Install</option> <option>Introduction</option> <option>AD</option> <option>ADFun</option> <option>library</option> <option>Example</option> <option>configure</option> <option>Appendix</option> </select> </td> <td> <select onchange='choose_down2(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_down1(this)'> <option>Independent-></option> <option>Independent.cpp</option> </select> </td> <td>Independent.cpp</td> <td>Headings</td> </tr></table><br/> <center><b><big><big>Independent and ADFun Constructor: Example and Test</big></big></b></center> <code><font color="blue"><pre style='display:inline'> # include <cppad/cppad.hpp> namespace { // -------------------------------------------------------- // define the template function Test<VectorAD>(void) in empty namespace template <class VectorAD> bool Test(void) { bool ok = true; using CppAD::AD; using CppAD::NearEqual; // domain space vector size_t n = 2; VectorAD X(n); // VectorAD is the template parameter in call to Test X[0] = 0.; X[1] = 1.; // declare independent variables and start recording // use the template parameter VectorAD for the vector type CppAD::<a href="independent.xml" target="_top">Independent</a>(X); <a href="ad.xml" target="_top">AD</a><double> a = X[0] + X[1]; // first AD operation <a href="ad.xml" target="_top">AD</a><double> b = X[0] * X[1]; // second AD operation // range space vector size_t m = 2; VectorAD Y(m); // VectorAD is the template paraemter in call to Test Y[0] = a; Y[1] = b; // create f: X -> Y and stop tape recording // use the template parameter VectorAD for the vector type CppAD::<a href="funconstruct.xml" target="_top">ADFun</a><double> f(X, Y); // check value ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(Y[0] , 1., 1e-10 , 1e-10); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(Y[1] , 0., 1e-10 , 1e-10); // compute f(1, 2) <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><double> x(n); <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><double> y(m); x[0] = 1.; x[1] = 2.; y = f.<a href="forward.xml" target="_top">Forward</a>(0, x); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(y[0] , 3., 1e-10 , 1e-10); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(y[1] , 2., 1e-10 , 1e-10); // compute partial of f w.r.t x[0] at (1, 2) <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><double> dx(n); <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><double> dy(m); dx[0] = 1.; dx[1] = 0.; dy = f.<a href="forward.xml" target="_top">Forward</a>(1, dx); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[0] , 1., 1e-10 , 1e-10); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[1] , x[1], 1e-10 , 1e-10); // compute partial of f w.r.t x[1] at (1, 2) dx[0] = 0.; dx[1] = 1.; dy = f.<a href="forward.xml" target="_top">Forward</a>(1, dx); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[0] , 1., 1e-10 , 1e-10); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[1] , x[0], 1e-10 , 1e-10); return ok; } } // End of empty namespace ------------------------------------------- # include <vector> # include <valarray> bool <a href="independent.xml" target="_top">Independent</a>(void) { bool ok = true; typedef CppAD::<a href="ad.xml" target="_top">AD</a><double> ADdouble; // Run with VectorAD equal to three different cases // all of which are Simple Vectors with elements of type <a href="ad.xml" target="_top">AD</a><double>. ok &= Test< CppAD::vector <ADdouble> >(); ok &= Test< std::vector <ADdouble> >(); ok &= Test< std::valarray <ADdouble> >(); return ok; } </pre> </font></code> <hr/>Input File: example/independent.cpp </body> </html>