<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>ADFun Assignment: Example and Test</title>
<meta name="description" id="description" content="ADFun Assignment: Example and Test"/>
<meta name="keywords" id="keywords" content=" Adfun assignment example "/>
<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='_fun_assign.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="funconstruct.xml" target="_top">Prev</a>
</td><td><a href="dependent.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>FunConstruct</option>
<option>fun_assign.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>CppAD-&gt;</option>
<option>Install</option>
<option>Introduction</option>
<option>AD</option>
<option>ADFun</option>
<option>preprocessor</option>
<option>multi_thread</option>
<option>library</option>
<option>ipopt_solve</option>
<option>Example</option>
<option>speed</option>
<option>Appendix</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>ADFun-&gt;</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>optimize</option>
<option>check_for_nan</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>FunConstruct-&gt;</option>
<option>fun_assign.cpp</option>
</select>
</td>
<td>fun_assign.cpp</td>
<td>Headings</td>
</tr></table><br/>


<center><b><big><big>ADFun Assignment: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 
# include &lt;cppad/cppad.hpp&gt;
# include &lt;limits&gt;

bool fun_assign(void)
{	bool ok = true;
	using CppAD::AD;
	using CppAD::NearEqual;
	size_t i, j;

	// ten times machine percision
	double eps = 10. * CppAD::numeric_limits&lt;double&gt;::epsilon();

	// two <a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; objects
	CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; g;

	// domain space vector
	size_t n  = 3;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) x(n);
	for(j = 0; j &lt; n; j++)
		x[j] = <a href="ad.xml" target="_top">AD</a>&lt;double&gt;(j + 2);

	// 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="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) y(m);
	y[0] = x[0] + x[0] * x[1];
	y[1] = x[1] * x[2] + x[2];

	// Store operation sequence, and order zero forward results, in f.
	CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; f(x, y);

	// sparsity pattern for the identity matrix
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(std::set&lt;size_t&gt;) r(n);
	for(j = 0; j &lt; n; j++)
		r[j].insert(j);

	// Store forward mode sparsity pattern in f
	f.ForSparseJac(n, r); 

	// make a copy in g
	g = f;

	// check values that should be equal
	ok &amp;= ( g.size_order()       == f.size_order() );
	ok &amp;= ( g.size_forward_bool() == f.size_forward_bool() );
	ok &amp;= ( g.size_forward_set()  == f.size_forward_set() );

	// Use zero order Taylor coefficient from f for first order
	// calculation using g.
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dx(n), dy(m);
	for(i = 0; i &lt; n; i++)
		dx[i] = 0.;
	dx[1] = 1;
	dy    = g.<a href="forward.xml" target="_top">Forward</a>(1, dx);
	ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[0], x[0], eps, eps); // partial y[0] w.r.t x[1] 
	ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[1], x[2], eps, eps); // partial y[1] w.r.t x[1] 

	// Use forward Jacobian sparsity pattern from f to calculate
	// Hessian sparsity pattern using g.
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(std::set&lt;size_t&gt;) s(1), h(n);
	s[0].insert(0); // Compute sparsity pattern for Hessian of y[0]
	h =  f.RevSparseHes(n, s);

	// check sparsity pattern for Hessian of y[0] = x[0] + x[0] * x[1] 
	ok  &amp;= ( h[0].find(0) == h[0].end() ); // zero     w.r.t x[0], x[0] 
	ok  &amp;= ( h[0].find(1) != h[0].end() ); // non-zero w.r.t x[0], x[1] 
	ok  &amp;= ( h[0].find(2) == h[0].end() ); // zero     w.r.t x[0], x[2] 

	ok  &amp;= ( h[1].find(0) != h[1].end() ); // non-zero w.r.t x[1], x[0] 
	ok  &amp;= ( h[1].find(1) == h[1].end() ); // zero     w.r.t x[1], x[1] 
	ok  &amp;= ( h[1].find(2) == h[1].end() ); // zero     w.r.t x[1], x[2] 

	ok  &amp;= ( h[2].find(0) == h[2].end() ); // zero     w.r.t x[2], x[0] 
	ok  &amp;= ( h[2].find(1) == h[2].end() ); // zero     w.r.t x[2], x[1] 
	ok  &amp;= ( h[2].find(2) == h[2].end() ); // zero     w.r.t x[2], x[2] 

	return ok;
}

</pre>

</font></code>


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

</body>
</html>
