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

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

interp_retape.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>Interpolation With Retaping: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Interpolation With Retaping: Example and Test"/>
<meta name="keywords" id="keywords" content=" interpolation with retaping: example and test interpolate tape retape see also "/>
<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='_interp_retape.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="interp_onetape.cpp.xml" target="_top">Prev</a>
</td><td><a href="atomic.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>AD</option>
<option>ADValued</option>
<option>Discrete</option>
<option>interp_retape.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>AD-&gt;</option>
<option>ad_ctor</option>
<option>ad_assign</option>
<option>Convert</option>
<option>ADValued</option>
<option>BoolValued</option>
<option>VecAD</option>
<option>base_require</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>ADValued-&gt;</option>
<option>Arithmetic</option>
<option>std_math_ad</option>
<option>MathOther</option>
<option>CondExp</option>
<option>Discrete</option>
<option>atomic</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>Discrete-&gt;</option>
<option>tape_index.cpp</option>
<option>interp_onetape.cpp</option>
<option>interp_retape.cpp</option>
</select>
</td>
<td>interp_retape.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>See Also</option>
</select>
</td>
</tr></table><br/>



<center><b><big><big>Interpolation With Retaping: Example and Test</big></big></b></center>
<br/>
<b><big><a name="See Also" id="See Also">See Also</a></big></b>

<br/>
<a href="interp_onetape.cpp.xml" target="_top"><span style='white-space: nowrap'>interp_onetape.cpp</span></a>


<code><span style='white-space: nowrap'><br/>
<br/>
</span></code><code><font color="blue"><pre style='display:inline'> 
# include &lt;cppad/cppad.hpp&gt;
# include &lt;cassert&gt;
# include &lt;cmath&gt;

namespace {
	double ArgumentValue[] = {
		.0 ,
		.2 ,
		.4 ,
		.8 ,
		1.
	};
	double FunctionValue[] = {
		std::sin( ArgumentValue[0] ) ,
		std::sin( ArgumentValue[1] ) ,
		std::sin( ArgumentValue[2] ) ,
		std::sin( ArgumentValue[3] ) ,
		std::sin( ArgumentValue[4] )
	};
	size_t TableLength = 5;

	size_t Index(const CppAD::<a href="ad.xml" target="_top">AD</a>&lt;double&gt; &amp;x)
	{	// determine the index j such that x is between
		// ArgumentValue[j] and ArgumentValue[j+1] 
		static size_t j = 0;
		while ( x &lt; ArgumentValue[j] &amp;&amp; j &gt; 0 )
			j--;
		while ( x &gt; ArgumentValue[j+1] &amp;&amp; j &lt; TableLength - 2)
			j++;
		// assert conditions that must be true given logic above
		assert( j &gt;= 0 &amp;&amp; j &lt; TableLength - 1 );
		return j;
	}
	double Argument(const CppAD::<a href="ad.xml" target="_top">AD</a>&lt;double&gt; &amp;x)
	{	size_t j = Index(x);
		return ArgumentValue[j];
	}
	double Function(const CppAD::<a href="ad.xml" target="_top">AD</a>&lt;double&gt; &amp;x)
	{	size_t j = Index(x);
		return FunctionValue[j];
	}
	double Slope(const CppAD::<a href="ad.xml" target="_top">AD</a>&lt;double&gt; &amp;x)
	{	size_t j  = Index(x);
		double dx = ArgumentValue[j+1] - ArgumentValue[j];
		double dy = FunctionValue[j+1] - FunctionValue[j];
		return dy / dx;
	}
}

bool interp_retape(void)
{	bool ok = true;

	using CppAD::AD;
	using CppAD::NearEqual;

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

	// loop over argument values
	size_t k;
	for(k = 0; k &lt; TableLength - 1; k++)
	{
		X[0] = .4 * ArgumentValue[k] + .6 * ArgumentValue[k+1];

		// declare independent variables and start tape recording
		// (use a different tape for each argument value)
		CppAD::<a href="independent.xml" target="_top">Independent</a>(X);

		// evaluate piecewise linear interpolant at X[0]
		<a href="ad.xml" target="_top">AD</a>&lt;double&gt; A = Argument(X[0]); 
		<a href="ad.xml" target="_top">AD</a>&lt;double&gt; F = Function(X[0]);
		<a href="ad.xml" target="_top">AD</a>&lt;double&gt; S = Slope(X[0]);
		<a href="ad.xml" target="_top">AD</a>&lt;double&gt; I = F + (X[0] - A) * S;

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

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

		// vectors for arguments to the function object f
		<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) x(n);   // argument values
		<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) y(m);   // function values 
		<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dx(n);  // differentials in x space
		<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dy(m);  // differentials in y space

		// to check function value we use the fact that X[0] is between
		// ArgumentValue[k] and ArgumentValue[k+1]
		double delta, check;
		x[0]   = Value(X[0]);
		delta  = ArgumentValue[k+1] - ArgumentValue[k];
		check  = FunctionValue[k+1] * (x[0]-ArgumentValue[k]) / delta
	               + FunctionValue[k] * (ArgumentValue[k+1]-x[0]) / delta; 
		ok    &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(Y[0], check, 1e-10, 1e-10);

		// evaluate partials w.r.t. x[0] 
		dx[0] = 1.;
		dy    = f.<a href="forward.xml" target="_top">Forward</a>(1, dx);

		// check that the derivative is the slope
		check = (FunctionValue[k+1] - FunctionValue[k])
		      / (ArgumentValue[k+1] - ArgumentValue[k]);
		ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[0], check, 1e-10, 1e-10);
	}
	return ok;
}

</pre>

</font></code>


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

</body>
</html>