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

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

exp_eps_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_eps: CppAD Forward and Reverse Sweeps</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="exp_eps: CppAD Forward and Reverse Sweeps"/>
<meta name="keywords" id="keywords" content=" exp_eps: 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_eps_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_eps_rev2.cpp.xml" target="_top">Prev</a>
</td><td><a href="exp_apx_main.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>Introduction</option>
<option>exp_eps</option>
<option>exp_eps_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_eps-&gt;</option>
<option>exp_eps.hpp</option>
<option>exp_eps.cpp</option>
<option>exp_eps_for0</option>
<option>exp_eps_for1</option>
<option>exp_eps_rev1</option>
<option>exp_eps_for2</option>
<option>exp_eps_rev2</option>
<option>exp_eps_cppad</option>
</select>
</td>
<td>exp_eps_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_eps: 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>

 and 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='normal'>&#x003B5;</mi>
<mo stretchy="false">=</mo>
<mn>.2</mn>
</mrow></math>

,
of the function 

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;exp_eps(</span></font><i><font color="black"><span style='white-space: nowrap'>x</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>epsilon</span></font></i><font color="blue"><span style='white-space: nowrap'>)<br/>
</span></font></code>
as defined by the <a href="exp_eps.hpp.xml" target="_top"><span style='white-space: nowrap'>exp_eps.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>

 and 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='normal'>&#x003B5;</mi>
<mo stretchy="false">=</mo>
<mn>.2</mn>
</mrow></math>

,
of the function 

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;exp_eps(</span></font><i><font color="black"><span style='white-space: nowrap'>x</span></font></i><font color="blue"><span style='white-space: nowrap'>,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>epsilon</span></font></i><font color="blue"><span style='white-space: nowrap'>)<br/>
</span></font></code>
</li><li>
Create and test a modified version of the routine below that computes
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>.1</mn>
</mrow></math>

 and 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='normal'>&#x003B5;</mi>
<mo stretchy="false">=</mo>
<mn>.2</mn>
</mrow></math>

,
of the function corresponding to the operation sequence 
for 
<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>

 and 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='normal'>&#x003B5;</mi>
<mo stretchy="false">=</mo>
<mn>.2</mn>
</mrow></math>

.
Hint: you could define a vector u with two components and use

<code><font color="blue"><span style='white-space: nowrap'><br/>
&#xA0;&#xA0;&#xA0;&#xA0;&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>f</span></font></i><font color="blue"><span style='white-space: nowrap'>.Forward(0,&#xA0;</span></font><i><font color="black"><span style='white-space: nowrap'>u</span></font></i><font color="blue"><span style='white-space: nowrap'>)<br/>
</span></font></code>
to run zero order forward mode at a point different
form the point where the operation sequence corresponding to

<code><i><font color="black"><span style='white-space: nowrap'>f</span></font></i></code>
 was recorded.
</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_eps.hpp&quot;      // our example exponential function approximation
bool exp_eps_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 = 2; // dimension of the domain space
	vector&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; U(n);
	U[0] = .5;    // value of x for this operation sequence
	U[1] = .2;    // value of e for this operation sequence

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

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

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

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

	// first order reverse mode 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_eps(x, epsilon)
	check = 1.5;              // partial w.r.t. x
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[0], check, 1e-10, 1e-10);
	check = 0.;               // partial w.r.t. epsilon
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[1], check, 1e-10, 1e-10);

	// second order forward sweep that computes
	// second partial of exp_eps(x, epsilon) w.r.t. x
	vector&lt;double&gt; x2(n);     // second order Taylor coefficients
	vector&lt;double&gt; y2(m);
	x2[0] = 0.;               // evaluate partial w.r.t x
	x2[1] = 0.;
	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_eps(x, epsilon) w.r.t. x
	dw.resize(2 * n);         // space for first and second derivative
	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_eps_cppad.cpp

</body>
</html>