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

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

fun_check.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>ADFun Check and Re-Tape: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="ADFun Check and Re-Tape: Example and Test"/>
<meta name="keywords" id="keywords" content=" adfun check and re-tape: example test Funcheck Adfun Dependent "/>
<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='_fun_check.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="funcheck.xml" target="_top">Prev</a>
</td><td><a href="optimize.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>FunCheck</option>
<option>fun_check.cpp</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>ADFun-&gt;</option>
<option>Independent</option>
<option>FunConstruct</option>
<option>Dependent</option>
<option>abort_recording</option>
<option>seq_property</option>
<option>FunEval</option>
<option>Drivers</option>
<option>FunCheck</option>
<option>optimize</option>
<option>check_for_nan</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>FunCheck-&gt;</option>
<option>fun_check.cpp</option>
</select>
</td>
<td>fun_check.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>ADFun Check and Re-Tape: Example 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 object Fun&lt;Type,Vector&gt; in empty namespace
template &lt;class Type, class Vector&gt;
class Fun {
private:
	size_t n;
public:
	// function constructor
	Fun(size_t n_) : n(n_)
	{ }
	// function evaluator
	Vector operator() (const Vector &amp;x)
	{	Vector y(n);
		size_t i;
		for(i = 0; i &lt; n; i++)
		{	// This operaiton sequence depends on x
			if( x[i] &gt;= 0 ) 
				y[i] = exp(x[i]);
			else	y[i] = exp(-x[i]);
		}
		return y;
	}	
};
// template function FunCheckCases&lt;Vector, ADVector&gt; in empty namespace
template &lt;class Vector, class ADVector&gt;
bool FunCheckCases(void)
{	bool ok = true;
	using CppAD::AD;
	using CppAD::ADFun;
	using CppAD::Independent;

	// use the ADFun default constructor
	<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; f;

	// domain space vector
	size_t n = 2;
	ADVector X(n);
	X[0] = -1.;
	X[1] = 1.;

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

	// create function object to use with <a href="ad.xml" target="_top">AD</a>&lt;double&gt;
	Fun&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt;, ADVector &gt; G(n);

	// range space vector
	size_t m = n;
	ADVector Y(m);
	Y = G(X);

	// stop tape and store operation sequence in f : X -&gt; Y
	f.Dependent(X, Y);
	ok &amp;= (f.size_order() == 0);  // no implicit forward operation

	// create function object to use with double
	Fun&lt;double, Vector&gt; g(n);

	// function values should agree when the independent variable 
	// values are the same as during recording
	Vector x(n);
	size_t j;
	for(j = 0; j &lt; n; j++)
		x[j] = Value(X[j]);
	double r = 1e-10; 
	double a = 1e-10;
	ok      &amp;= FunCheck(f, g, x, a, r);

	// function values should not agree when the independent variable
	// values are the negative of values during recording
	for(j = 0; j &lt; n; j++)
		x[j] = - Value(X[j]);
	ok      &amp;= ! FunCheck(f, g, x, a, r);

	// re-tape to obtain the new AD of double operation sequence
	for(j = 0; j &lt; n; j++)
		X[j] = x[j];
	<a href="independent.xml" target="_top">Independent</a>(X);
	Y = G(X);

	// stop tape and store operation sequence in f : X -&gt; Y
	f.Dependent(X, Y);
	ok &amp;= (f.size_order() == 0);  // no implicit forward with this x

	// function values should agree now
	ok      &amp;= FunCheck(f, g, x, a, r);

	return ok;
}
} // End empty namespace 
# include &lt;vector&gt;
# include &lt;valarray&gt;
bool FunCheck(void)
{	bool ok = true;
	typedef CppAD::vector&lt;double&gt;                Vector1;
	typedef CppAD::vector&lt; CppAD::<a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; ADVector1;
	typedef   std::vector&lt;double&gt;                Vector2;
	typedef   std::vector&lt; CppAD::<a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; ADVector2;
	typedef std::valarray&lt;double&gt;                Vector3;
	typedef std::valarray&lt; CppAD::<a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; ADVector3;
	// Run with Vector and ADVector equal to three different cases
	// all of which are Simple Vectors with elements of type 
	// double and <a href="ad.xml" target="_top">AD</a>&lt;double&gt; respectively.
	ok &amp;= FunCheckCases&lt; Vector1, ADVector2 &gt;();
	ok &amp;= FunCheckCases&lt; Vector2, ADVector3 &gt;();
	ok &amp;= FunCheckCases&lt; Vector3, ADVector1 &gt;();
	return ok;
}
</pre>

</font></code>


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

</body>
</html>