Codebase list cppad / upstream/2015.00.00.7 doc / reverse_two.cpp.xml
upstream/2015.00.00.7

Tree @upstream/2015.00.00.7 (Download .tar.gz)

reverse_two.cpp.xml @upstream/2015.00.00.7raw · history · blame

<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Second Order Reverse ModeExample and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Second Order Reverse ModeExample and Test"/>
<meta name="keywords" id="keywords" content=" second order reverse modeexample and test 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='_reverse_two.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="reverse_two.xml" target="_top">Prev</a>
</td><td><a href="hes_times_dir.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>Reverse</option>
<option>reverse_two</option>
<option>reverse_two.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>Reverse-&gt;</option>
<option>reverse_one</option>
<option>reverse_two</option>
<option>reverse_any</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>reverse_two-&gt;</option>
<option>reverse_two.cpp</option>
<option>hes_times_dir.cpp</option>
</select>
</td>
<td>reverse_two.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Second Order Reverse ModeExample and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 
# include &lt;cppad/cppad.hpp&gt;
namespace { // ----------------------------------------------------------
// define the template function reverse_two_cases&lt;Vector&gt; in empty namespace
template &lt;typename Vector&gt; 
bool reverse_two_cases(void)
{	bool ok = true;
	using CppAD::AD;
	using CppAD::NearEqual;

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

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

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

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

	// use zero order forward mode to evaluate y at x = (3, 4)
	// use the template parameter Vector for the vector type
	Vector x(n), y(m);
	x[0]  = 3.;
	x[1]  = 4.;
	y     = f.<a href="forward.xml" target="_top">Forward</a>(0, x);
	ok    &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(y[0] , x[0]*x[0]*x[1], 1e-10, 1e-10);

	// use first order forward mode in x[0] direction
	// (all second order partials below involve x[0])
	Vector dx(n), dy(m);
	dx[0] = 1.;
	dx[1] = 1.;
	dy    = f.<a href="forward.xml" target="_top">Forward</a>(1, dx);
	double check = 2.*x[0]*x[1]*dx[0] + x[0]*x[0]*dx[1];
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[0], check, 1e-10, 1e-10);

	// use second order reverse mode to evalaute second partials of y[0]
	// with respect to (x[0], x[0]) and with respect to (x[0], x[1])
	Vector w(m), dw( n * 2 );
	w[0]  = 1.;
	dw    = f.<a href="reverse.xml" target="_top">Reverse</a>(2, w);

	// check derivative of f
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[0*2+0] , 2.*x[0]*x[1], 1e-10, 1e-10);
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[1*2+0] ,    x[0]*x[0], 1e-10, 1e-10);

	// check derivative of f^{(1)} (x) * dx
	check = 2.*x[1]*dx[1] + 2.*x[0]*dx[1];
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[0*2+1] , check, 1e-10, 1e-10); 
	check = 2.*x[0]*dx[1];
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[1*2+1] , check, 1e-10, 1e-10); 

	return ok;
}
} // End empty namespace 
# include &lt;vector&gt;
# include &lt;valarray&gt;
bool reverse_two(void)
{	bool ok = true;
	ok &amp;= reverse_two_cases&lt; CppAD::vector  &lt;double&gt; &gt;();
	ok &amp;= reverse_two_cases&lt; std::vector    &lt;double&gt; &gt;();
	ok &amp;= reverse_two_cases&lt; std::valarray  &lt;double&gt; &gt;();
	return ok;
}
</pre>

</font></code>


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

</body>
</html>