<?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</title>
<meta name="description" id="description" content="Forward Mode: Example and Test"/>
<meta name="keywords" id="keywords" content=" Forward 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='_forward.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_order.xml" target="_top">Prev</a>
</td><td><a href="forward_order.cpp.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.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.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Forward Mode: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 
# include &lt;limits&gt;
# include &lt;cppad/cppad.hpp&gt;
namespace { // --------------------------------------------------------
// define the template function ForwardCases&lt;Vector&gt; in empty namespace
template &lt;class Vector&gt; 
bool ForwardCases(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;

	// zero order forward mode using notation in forward_zero
	// use the template parameter Vector for the vector type
	Vector x0(n), y0(m);
	x0[0] = 3.;
	x0[1] = 4.;
	y0    = f.<a href="forward.xml" target="_top">Forward</a>(0, x0);
	ok  &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(y0[0] , x0[0]*x0[0]*x0[1], eps, eps);
	ok  &amp;= f.size_order() == 1;

	// first order forward mode using notation in forward_one
	// X(t)           = x0 + x1 * t
	// Y(t) = F[X(t)] = y0 + y1 * t + o(t)
	Vector x1(n), y1(m);
	x1[0] = 1.;
	x1[1] = 0.;
	y1    = f.<a href="forward.xml" target="_top">Forward</a>(1, x1); // partial F w.r.t. x_0
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(y1[0] , 2.*x0[0]*x0[1], eps, eps);
	ok   &amp;= f.size_order() == 2;

	// second order forward mode using notation in forward_order
	// X(t) =           x0 + x1 * t + x2 * t^2
	// Y(t) = F[X(t)] = y0 + y1 * t + y2 * t^2 + o(t^3)
	Vector x2(n), y2(m);
	x2[0]      = 0.;
	x2[1]      = 0.;
	y2         = f.<a href="forward.xml" target="_top">Forward</a>(2, x2);
	double F_00 = 2. * y2[0]; // 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);
	ok         &amp;= f.size_order() == 3;

	return ok;
}
} // End empty namespace 
# include &lt;vector&gt;
# include &lt;valarray&gt;
bool Forward(void)
{	bool ok = true;
	// Run with Vector equal to three different cases
	// all of which are Simple Vectors with elements of type double.
	ok &amp;= ForwardCases&lt; CppAD::vector  &lt;double&gt; &gt;();
	ok &amp;= ForwardCases&lt; std::vector    &lt;double&gt; &gt;();
	ok &amp;= ForwardCases&lt; std::valarray  &lt;double&gt; &gt;();
	return ok;
}
</pre>

</font></code>


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

</body>
</html>
