<?xml version='1.0'?> <?xml-stylesheet type='text/xsl' href='pmathml.xsl'?> <html xmlns='http://www.w3.org/1999/xhtml'> <head> <title>AD Computed Assignment Subtraction: Example and Test</title> <meta name="description" id="description" content="AD Computed Assignment Subtraction: Example and Test"/> <meta name="keywords" id="keywords" content=" -= Ad example computed assignment subtract test assign plus add "/> <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='_subeq.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="addeq.cpp.xml" target="_top">Prev</a> </td><td><a href="muleq.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>Arithmetic</option> <option>compute_assign</option> <option>SubEq.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>Arithmetic-></option> <option>UnaryPlus</option> <option>UnaryMinus</option> <option>ad_binary</option> <option>compute_assign</option> </select> </td> <td> <select onchange='choose_down1(this)'> <option>compute_assign-></option> <option>AddEq.cpp</option> <option>SubEq.cpp</option> <option>MulEq.cpp</option> <option>DivEq.cpp</option> </select> </td> <td>SubEq.cpp</td> <td>Headings</td> </tr></table><br/> <center><b><big><big>AD Computed Assignment Subtraction: Example and Test</big></big></b></center> <code><font color="blue"><pre style='display:inline'> # include <cppad/cppad.hpp> bool SubEq(void) { bool ok = true; using CppAD::AD; using CppAD::NearEqual; // domain space vector size_t n = 1; double x0 = .5; <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>< <a href="ad.xml" target="_top">AD</a><double> > x(n); x[0] = x0; // declare independent variables and start tape recording CppAD::<a href="independent.xml" target="_top">Independent</a>(x); // range space vector size_t m = 2; <a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>< <a href="ad.xml" target="_top">AD</a><double> > y(m); y[0] = 3. * x[0]; // initial value y[0] -= 2; // <a href="ad.xml" target="_top">AD</a><double> -= int y[0] -= 4.; // <a href="ad.xml" target="_top">AD</a><double> -= double y[1] = y[0] -= x[0]; // use the result of a computed assignment // create f: x -> y and stop tape recording 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] , 3.*x0-(2.+4.+x0), 1e-10 , 1e-10); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(y[1] , y[0], 1e-10 , 1e-10); // forward computation of partials w.r.t. x[0] <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.; dy = f.<a href="forward.xml" target="_top">Forward</a>(1, dx); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[0], 2., 1e-10, 1e-10); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[1], 2., 1e-10, 1e-10); // reverse computation of derivative of y[0] <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] = 0.; dw = f.<a href="reverse.xml" target="_top">Reverse</a>(1, w); ok &= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[0], 2., 1e-10, 1e-10); // use a VecAD<Base>::reference object with computed subtraction CppAD::VecAD<double> v(1); <a href="ad.xml" target="_top">AD</a><double> zero(0); <a href="ad.xml" target="_top">AD</a><double> result = 1; v[zero] = 2; result -= v[zero]; ok &= (result == -1); return ok; } </pre> </font></code> <hr/>Input File: example/sub_eq.cpp </body> </html>