<?xml version='1.0'?> <?xml-stylesheet type='text/xsl' href='pmathml.xsl'?> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <title>The AD Power Function: Example and Test</title> <meta name="description" id="description" content="The AD Power Function: Example and Test"/> <meta name="keywords" id="keywords" content=" pow Ad 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='_pow.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="pow.xml" target="_top">Prev</a> </td><td><a href="pow_int.cpp.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>AD</option> <option>ADValued</option> <option>MathOther</option> <option>pow</option> <option>Pow.cpp</option> </select> </td> <td> <select onchange='choose_down3(this)'> <option>ADValued-></option> <option>Arithmetic</option> <option>std_math_ad</option> <option>MathOther</option> <option>CondExp</option> <option>Discrete</option> </select> </td> <td> <select onchange='choose_down2(this)'> <option>MathOther-></option> <option>abs</option> <option>atan2</option> <option>erf</option> <option>pow</option> </select> </td> <td> <select onchange='choose_down1(this)'> <option>pow-></option> <option>Pow.cpp</option> <option>pow_int.cpp</option> </select> </td> <td>Pow.cpp</td> <td>Headings</td> </tr></table><br/> <center><b><big><big>The AD Power Function: Example and Test</big></big></b></center> <code><font color="blue"><pre style='display:inline'> # include <cppad/cppad.hpp> # include <cmath> bool Pow(void) { bool ok = true; using CppAD::AD; using CppAD::NearEqual; // domain space vector size_t n = 2; double x = 0.5; double y = 2.; <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>< <a href="ad.xml" target="_top">AD</a><double> > XY(n); XY[0] = x; XY[1] = y; // declare independent variables and start tape recording CppAD::<a href="independent.xml" target="_top">Independent</a>(XY); // 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> > Z(m); Z[0] = CppAD::pow(XY[0], XY[1]); // pow(variable, variable) Z[1] = CppAD::pow(XY[0], y); // pow(variable, parameter) Z[2] = CppAD::pow(x, XY[1]); // pow(parameter, variable) // create f: XY -> Z and stop tape recording CppAD::<a href="funconstruct.xml" target="_top">ADFun</a><double> f(XY, Z); // check value double check = std::pow(x, y); size_t i; for(i = 0; i < m; i++) ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(Z[i] , check, 1e-10 , 1e-10); // forward computation of first partial w.r.t. x <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><double> dxy(n); <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><double> dz(m); dxy[0] = 1.; dxy[1] = 0.; dz = f.<a href="forward.xml" target="_top">Forward</a>(1, dxy); check = y * std::pow(x, y-1.); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dz[0], check, 1e-10, 1e-10); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dz[1], check, 1e-10, 1e-10); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dz[2], 0., 1e-10, 1e-10); // forward computation of first partial w.r.t. y dxy[0] = 0.; dxy[1] = 1.; dz = f.<a href="forward.xml" target="_top">Forward</a>(1, dxy); check = std::log(x) * std::pow(x, y); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dz[0], check, 1e-10, 1e-10); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dz[1], 0., 1e-10, 1e-10); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dz[2], check, 1e-10, 1e-10); // reverse computation of derivative of z[0] + z[1] + z[2] <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><double> w(m); <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a><double> dw(n); w[0] = 1.; w[1] = 1.; w[2] = 1.; dw = f.<a href="reverse.xml" target="_top">Reverse</a>(1, w); check = y * std::pow(x, y-1.); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[0], 2. * check, 1e-10, 1e-10); check = std::log(x) * std::pow(x, y); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[1], 2. * check, 1e-10, 1e-10); // use a VecAD<Base>::reference object with pow CppAD::VecAD<double> v(2); <a href="ad.xml" target="_top">AD</a><double> zero(0); <a href="ad.xml" target="_top">AD</a><double> one(1); v[zero] = XY[0]; v[one] = XY[1]; <a href="ad.xml" target="_top">AD</a><double> result = CppAD::pow(v[zero], v[one]); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(result, Z[0], 1e-10, 1e-10); return ok; } </pre> </font></code> <hr/>Input File: example/pow.cpp </body> </html>