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

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

reverse_three.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>Third Order Reverse Mode: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Third Order Reverse Mode: Example and Test"/>
<meta name="keywords" id="keywords" content=" third order reverse mode: example and test taylor coefficients "/>
<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_three.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_any.xml" target="_top">Prev</a>
</td><td><a href="reverse_any.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_any</option>
<option>reverse_three.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_any-&gt;</option>
<option>reverse_three.cpp</option>
<option>reverse_any.cpp</option>
</select>
</td>
<td>reverse_three.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Taylor Coefficients</option>
</select>
</td>
</tr></table><br/>



<center><b><big><big>Third Order Reverse Mode: Example and Test</big></big></b></center>
<br/>
<b><big><a name="Taylor Coefficients" id="Taylor Coefficients">Taylor Coefficients</a></big></b>


<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mtable rowalign="center" ><mtr><mtd columnalign="right" >
<mi mathvariant='italic'>X</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">)</mo>
</mtd><mtd columnalign="center" >
<mo stretchy="false">=</mo>
</mtd><mtd columnalign="left" >
<msup><mi mathvariant='italic'>x</mi>
<mrow><mo stretchy="false">(</mo>
<mn>0</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
<mo stretchy="false">+</mo>
<msup><mi mathvariant='italic'>x</mi>
<mrow><mo stretchy="false">(</mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">+</mo>
<msup><mi mathvariant='italic'>x</mi>
<mrow><mo stretchy="false">(</mo>
<mn>2</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
<msup><mi mathvariant='italic'>t</mi>
<mn>2</mn>
</msup>
</mtd></mtr><mtr><mtd columnalign="right" >
<msup><mi mathvariant='italic'>X</mi>
<mrow><mo stretchy="false">(</mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">)</mo>
</mtd><mtd columnalign="center" >
<mo stretchy="false">=</mo>
</mtd><mtd columnalign="left" >
<msup><mi mathvariant='italic'>x</mi>
<mrow><mo stretchy="false">(</mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
<mo stretchy="false">+</mo>
<mn>2</mn>
<msup><mi mathvariant='italic'>x</mi>
<mrow><mo stretchy="false">(</mo>
<mn>2</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
<mi mathvariant='italic'>t</mi>
</mtd></mtr><mtr><mtd columnalign="right" >
<msup><mi mathvariant='italic'>X</mi>
<mrow><mo stretchy="false">(</mo>
<mn>2</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>t</mi>
<mo stretchy="false">)</mo>
</mtd><mtd columnalign="center" >
<mo stretchy="false">=</mo>
</mtd><mtd columnalign="left" >
<mn>2</mn>
<msup><mi mathvariant='italic'>x</mi>
<mrow><mo stretchy="false">(</mo>
<mn>2</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
</mtd></mtr></mtable>
</mrow></math>

Thus, we need to be careful to properly account for the fact that

<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<msup><mi mathvariant='italic'>X</mi>
<mrow><mo stretchy="false">(</mo>
<mn>2</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
<mo stretchy="false">(</mo>
<mn>0</mn>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<mn>2</mn>
<msup><mi mathvariant='italic'>x</mi>
<mrow><mo stretchy="false">(</mo>
<mn>2</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
</mrow></math>

 
(and similarly 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<msup><mi mathvariant='italic'>Y</mi>
<mrow><mo stretchy="false">(</mo>
<mn>2</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
<mo stretchy="false">(</mo>
<mn>0</mn>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<mn>2</mn>
<msup><mi mathvariant='italic'>y</mi>
<mrow><mo stretchy="false">(</mo>
<mn>2</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
</mrow></math>

).

<code><font color="blue">
<pre style='display:inline'> 
# include &lt;cppad/cppad.hpp&gt;
namespace { // ----------------------------------------------------------
// define the template function cases&lt;Vector&gt; in empty namespace
template &lt;typename Vector&gt; 
bool cases(void)
{	bool ok    = true;
	double eps = 10. * CppAD::numeric_limits&lt;double&gt;::epsilon();
	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[1];

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

	// define x^0 and compute y^0 using user zero order forward 
	Vector x0(n), y0(m);
	x0[0]    = 2.;
	x0[1]    = 3.;
	y0       = f.<a href="forward.xml" target="_top">Forward</a>(0, x0);

	// y^0 = F(x^0) 
	double check;
	check    =  x0[0] * x0[1];
	ok      &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(y0[0] , check, eps, eps);

	// define x^1 and compute y^1 using first order forward mode 
	Vector x1(n), y1(m);
	x1[0] = 4.;
	x1[1] = 5.;
	y1    = f.<a href="forward.xml" target="_top">Forward</a>(1, x1);

	// Y^1 (x) = partial_t F( x^0 + x^1 * t )
	// y^1     = Y^1 (0)
	check = x1[0] * x0[1] + x0[0] * x1[1];
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(y1[0], check, eps, eps);

	// define x^2 and compute y^2 using second order forward mode
	Vector x2(n), y2(m);
	x2[0] = 6.;
	x2[1] = 7.;
	y2    = f.<a href="forward.xml" target="_top">Forward</a>(2, x2);

	// Y^2 (x) = partial_tt F( x^0 + x^1 * t + x^2 * t^2 )
	// y^2     = (1/2) *  Y^2 (0)
	check  = x2[0] * x0[1] + x1[0] * x1[1] + x0[0] * x2[1];
	ok    &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(y2[0], check, eps, eps);

	// W(x)  = Y^0 (x) + 2 * Y^1 (x) + 3 * (1/2) * Y^2 (x)
	size_t p = 3;
	Vector dw(n*p), w(m*p);
	w[0] = 1.;
	w[1] = 2.;
	w[2] = 3.;
	dw   = f.<a href="reverse.xml" target="_top">Reverse</a>(p, w);

	// check partial w.r.t x^0_0 of W(x)
	check = x0[1] + 2. * x1[1] + 3. * x2[1];
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[0*p+0], check, eps, eps);

	// check partial w.r.t x^0_1 of W(x)
	check = x0[0] + 2. * x1[0] + 3. * x2[0];
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[1*p+0], check, eps, eps);

	// check partial w.r.t x^1_0 of W(x)
	check = 2. * x0[1] + 3. * x1[1];
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[0*p+1], check, eps, eps);

	// check partial w.r.t x^1_1 of W(x)
	check = 2. * x0[0] + 3. * x1[0];
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[1*p+1], check, eps, eps);

	// check partial w.r.t x^2_0 of W(x)
	check = 3. * x0[1]; 
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[0*p+2], check, eps, eps);

	// check partial w.r.t x^2_1 of W(x)
	check = 3. * x0[0]; 
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[1*p+2], check, eps, eps);

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

</font></code>


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

</body>
</html>