<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Forward Mode: Example and Test of Multiple Orders</title>
<meta name="description" id="description" content="Forward Mode: Example and Test of Multiple Orders"/>
<meta name="keywords" id="keywords" content=" forward multiple orders order "/>
<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='_forward_order.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="forward.cpp.xml" target="_top">Prev</a>
</td><td><a href="size_order.xml" target="_top">Next</a>
</td><td>
<select onchange='choose_across0(this)'>
<option>Index-&gt;</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-&gt;</option>
<option>CppAD</option>
<option>ADFun</option>
<option>FunEval</option>
<option>Forward</option>
<option>forward_order</option>
<option>forward_order.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>FunEval-&gt;</option>
<option>Forward</option>
<option>Reverse</option>
<option>Sparse</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>Forward-&gt;</option>
<option>forward_zero</option>
<option>forward_one</option>
<option>forward_two</option>
<option>forward_order</option>
<option>size_order</option>
<option>CompareChange</option>
<option>capacity_order</option>
<option>number_skip</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>forward_order-&gt;</option>
<option>forward.cpp</option>
<option>forward_order.cpp</option>
</select>
</td>
<td>forward_order.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Forward Mode: Example and Test of Multiple Orders</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 
# include &lt;limits&gt;
# include &lt;cppad/cppad.hpp&gt;
bool forward_order(void)
{	bool ok = true;
	using CppAD::AD;
	using CppAD::NearEqual;
	double eps = 10. * std::numeric_limits&lt;double&gt;::epsilon();

	// domain space vector
	size_t n = 2;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) ax(n);
	ax[0] = 0.; 
	ax[1] = 1.;

	// declare independent variables and starting recording
	CppAD::<a href="independent.xml" target="_top">Independent</a>(ax);

	// range space vector
	size_t m = 1;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) ay(m);
	ay[0] = ax[0] * ax[0] * ax[1];

	// create f: x -&gt; y and stop tape recording
	CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; f(ax, ay);

	// initially, the variable values during taping are stored in f
	ok &amp;= f.size_order() == 1;

	// Compute three forward orders at one
	size_t q = 2, q1 = q+1;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) xq(n*q1), yq;
	xq[q1*0 + 0] = 3.;    xq[q1*1 + 0] = 4.; // x^0 (order zero)
	xq[q1*0 + 1] = 1.;    xq[q1*1 + 1] = 0.; // x^1 (order one)
	xq[q1*0 + 2] = 0.;    xq[q1*1 + 2] = 0.; // x^2 (order two)
	// X(t) =   x^0 + x^1 * t + x^2 * t^2
	//      = [ 3 + t, 4 ]   
	yq  = f.<a href="forward.xml" target="_top">Forward</a>(q, xq);
	ok &amp;= yq.size() == m*q1;
	// Y(t) = F[X(t)] 
	//      = (3 + t) * (3 + t) * 4
	//      = y^0 + y^1 * t + y^2 * t^2 + o(t^3)
	//
	// check y^0 (order zero)
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) x0(n);
	x0[0] = xq[q1*0 + 0];
	x0[1] = xq[q1*1 + 0];
	ok  &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(yq[q1*0 + 0] , x0[0]*x0[0]*x0[1], eps, eps);
	//
	// check y^1 (order one)
	ok  &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(yq[q1*0 + 1] , 2.*x0[0]*x0[1], eps, eps);
	//
	// check y^2 (order two)
	double F_00 = 2. * yq[q1*0 + 2]; // second partial F w.r.t. x_0, x_0
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(F_00, 2.*x0[1], eps, eps);

	// check number of orders per variable
	ok   &amp;= f.size_order() == 3;

	return ok;
}
</pre>

</font></code>


<hr/>Input File: example/forward_order.cpp

</body>
</html>
