Codebase list cppad / upstream/2015.00.00.7 doc / exp_2_cppad.xml
upstream/2015.00.00.7

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

exp_2_cppad.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>exp_2: CppAD Forward and Reverse Sweeps</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="exp_2: CppAD Forward and Reverse Sweeps"/>
<meta name="keywords" id="keywords" content=" exp_2: cppad forward and reverse sweeps purpose exercises "/>
<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='_exp_2_cppad_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="exp_2_rev2.cpp.xml" target="_top">Prev</a>
</td><td><a href="exp_eps.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>Introduction</option>
<option>exp_2</option>
<option>exp_2_cppad</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>Introduction-&gt;</option>
<option>get_started.cpp</option>
<option>exp_2</option>
<option>exp_eps</option>
<option>exp_apx_main.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>exp_2-&gt;</option>
<option>exp_2.hpp</option>
<option>exp_2.cpp</option>
<option>exp_2_for0</option>
<option>exp_2_for1</option>
<option>exp_2_rev1</option>
<option>exp_2_for2</option>
<option>exp_2_rev2</option>
<option>exp_2_cppad</option>
</select>
</td>
<td>exp_2_cppad</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Purpose</option>
<option>Exercises</option>
</select>
</td>
</tr></table><br/>



<center><b><big><big>exp_2: CppAD Forward and Reverse Sweeps</big></big></b></center>
.

<br/>
<br/>
<b><big><a name="Purpose" id="Purpose">Purpose</a></big></b>
<br/>
Use CppAD forward and reverse modes to compute the
partial derivative with respect to 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>x</mi>
</mrow></math>

,
at the point 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">=</mo>
<mn>.5</mn>
</mrow></math>

,
of the function 

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;exp_2(</span></font><i><font color="black"><span style='white-space: nowrap'>x</span></font></i><font color="blue"><span style='white-space: nowrap'>)<br/>
</span></font></code>
as defined by the <a href="exp_2.hpp.xml" target="_top"><span style='white-space: nowrap'>exp_2.hpp</span></a>
 include file.

<br/>
<br/>
<b><big><a name="Exercises" id="Exercises">Exercises</a></big></b>

<ol type="1"><li>
Create and test a modified version of the routine below that computes
the same order derivatives with respect to 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>x</mi>
</mrow></math>

,
at the point 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">=</mo>
<mn>.1</mn>
</mrow></math>

 
of the function 

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;exp_2(</span></font><i><font color="black"><span style='white-space: nowrap'>x</span></font></i><font color="blue"><span style='white-space: nowrap'>)<br/>
</span></font></code>
</li><li>
Create a routine called

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;exp_3(</span></font><i><font color="black"><span style='white-space: nowrap'>x</span></font></i><font color="blue"><span style='white-space: nowrap'>)<br/>
</span></font></code>
that evaluates the function

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mi mathvariant='italic'>f</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<mn>1</mn>
<mo stretchy="false">+</mo>
<msup><mi mathvariant='italic'>x</mi>
<mn>2</mn>
</msup>
<mo stretchy="false">/</mo>
<mn>2</mn>
<mo stretchy="false">+</mo>
<msup><mi mathvariant='italic'>x</mi>
<mn>3</mn>
</msup>
<mo stretchy="false">/</mo>
<mn>6</mn>
</mrow></math>

Test a modified version of the routine below that computes
the derivative of 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>f</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
</mrow></math>


at the point 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">=</mo>
<mn>.5</mn>
</mrow></math>

.
</li></ol>

<code><font color='blue'><pre style='display:inline'> 

# include &lt;cppad/cppad.hpp&gt;  // http://www.coin-or.org/CppAD/ 
# include &quot;exp_2.hpp&quot;        // second order exponential approximation
bool exp_2_cppad(void)
{	bool ok = true;
	using CppAD::AD;
	using CppAD::vector;    // can use any simple vector template class
	using CppAD::NearEqual; // checks if values are nearly equal

	// domain space vector
	size_t n = 1; // dimension of the domain space
	vector&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; X(n);
	X[0] = .5;    // value of x for this operation sequence

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

	// evaluate our exponential approximation
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; x   = X[0];
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; apx = exp_2(x);  

	// range space vector
	size_t m = 1;  // dimension of the range space
	vector&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; Y(m);
	Y[0] = apx;    // variable that represents only range space component

	// Create f: X -&gt; Y corresponding to this operation sequence
	// and stop recording. This also executes a zero order forward 
	// sweep using values in X for x.
	CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; f(X, Y);

	// first order forward sweep that computes
	// partial of exp_2(x) with respect to x
	vector&lt;double&gt; dx(n);  // differential in domain space
	vector&lt;double&gt; dy(m);  // differential in range space
	dx[0] = 1.;            // direction for partial derivative
	dy    = f.<a href="forward.xml" target="_top">Forward</a>(1, dx);
	double check = 1.5;
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[0], check, 1e-10, 1e-10);

	// first order reverse sweep that computes the derivative
	vector&lt;double&gt;  w(m);   // weights for components of the range
	vector&lt;double&gt; dw(n);   // derivative of the weighted function
	w[0] = 1.;              // there is only one weight
	dw   = f.<a href="reverse.xml" target="_top">Reverse</a>(1, w); // derivative of w[0] * exp_2(x)
	check = 1.5;            // partial of exp_2(x) with respect to x
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[0], check, 1e-10, 1e-10);

	// second order forward sweep that computes
	// second partial of exp_2(x) with respect to x
	vector&lt;double&gt; x2(n);     // second order Taylor coefficients 
	vector&lt;double&gt; y2(m);  
	x2[0] = 0.;               // evaluate second partial .w.r.t. x
	y2    = f.<a href="forward.xml" target="_top">Forward</a>(2, x2);
	check = 0.5 * 1.;         // Taylor coef is 1/2 second derivative 
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(y2[0], check, 1e-10, 1e-10);

	// second order reverse sweep that computes
	// derivative of partial of exp_2(x) w.r.t. x
	dw.resize(2 * n);         // space for first and second derivatives
	dw    = f.<a href="reverse.xml" target="_top">Reverse</a>(2, w);
	check = 1.;               // result should be second derivative
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[0*2+1], check, 1e-10, 1e-10);

	return ok;
}

</pre></font></code>


<hr/>Input File: introduction/exp_apx/exp_2_cppad.cpp

</body>
</html>