<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Creating Your Own Interface to an ADFun Object</title>
<meta name="description" id="description" content="Creating Your Own Interface to an ADFun Object"/>
<meta name="keywords" id="keywords" content=" Adfun example test "/>
<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='_ad_fun.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="general.xml" target="_top">Prev</a>
</td><td><a href="ad_in_c.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>Example</option>
<option>General</option>
<option>ad_fun.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>Example-&gt;</option>
<option>General</option>
<option>ExampleUtility</option>
<option>ListAllExamples</option>
<option>testvector</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>General-&gt;</option>
<option>ad_fun.cpp</option>
<option>ad_in_c.cpp</option>
<option>conj_grad.cpp</option>
<option>cppad_eigen.hpp</option>
<option>hes_minor_det.cpp</option>
<option>hes_lu_det.cpp</option>
<option>interface2c.cpp</option>
<option>jac_minor_det.cpp</option>
<option>jac_lu_det.cpp</option>
<option>mul_level</option>
<option>ode_stiff.cpp</option>
<option>mul_level_ode.cpp</option>
<option>mul_level_adolc_ode.cpp</option>
<option>ode_taylor.cpp</option>
<option>stack_machine.cpp</option>
</select>
</td>
<td>ad_fun.cpp</td>
<td>Headings</td>
</tr></table><br/>
<center><b><big><big>Creating Your Own Interface to an ADFun Object</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 

# include &lt;cppad/cppad.hpp&gt;

namespace {

	// This class is an example of a different interface to an AD function object
	template &lt;class Base&gt;
	class my_ad_fun {
	
	private:
		CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;Base&gt; f;
	
	public:
		// default constructor
		my_ad_fun(void)
		{ }
	
		// destructor
		~ my_ad_fun(void)
		{ }
	
		// Construct an my_ad_fun object with an operation sequence.
		// This is the same as for <a href="funconstruct.xml" target="_top">ADFun</a>&lt;Base&gt; except that no zero 
		// order forward sweep is done. Note Hessian and Jacobian do 
		// their own zero order forward mode sweep. 
		template &lt;class ADvector&gt;
		my_ad_fun(const ADvector&amp; x, const ADvector&amp; y)
		{	f.Dependent(x, y); }
	
		// same as <a href="funconstruct.xml" target="_top">ADFun</a>&lt;Base&gt;::Jacobian
		template &lt;class VectorBase&gt;
		VectorBase jacobian(const VectorBase&amp; x) 
		{	return f.<a href="jacobian.xml" target="_top">Jacobian</a>(x); }

		// same as <a href="funconstruct.xml" target="_top">ADFun</a>&lt;Base&gt;::Hessian
	        template &lt;typename VectorBase&gt;
		VectorBase hessian(const VectorBase &amp;x, const VectorBase &amp;w)
		{	return f.Hessian(x, w); }
	}; 

} // End empty namespace

bool ad_fun(void)
{	// This example is similar to example/jacobian.cpp, except that it
	// uses my_ad_fun instead of ADFun.

	bool ok = true;
	using CppAD::AD;
	using CppAD::NearEqual;
	using CppAD::exp;
	using CppAD::sin;
	using CppAD::cos;

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

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

	// a calculation between the domain and range values
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; Square = X[0] * X[0];

	// range space vector
	size_t m = 3;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;)  Y(m);
	Y[0] = Square * exp( X[1] );
	Y[1] = Square * sin( X[1] );
	Y[2] = Square * cos( X[1] );

	// create f: X -&gt; Y and stop tape recording
	my_ad_fun&lt;double&gt; f(X, Y);

	// new value for the independent variable vector
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) x(n);
	x[0] = 2.;
	x[1] = 1.;

	// compute the derivative at this x
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) jac( m * n );
	jac = f.jacobian(x);

	/*
	F'(x) = [ 2 * x[0] * exp(x[1]) ,  x[0] * x[0] * exp(x[1]) ]
	        [ 2 * x[0] * sin(x[1]) ,  x[0] * x[0] * cos(x[1]) ]
	        [ 2 * x[0] * cos(x[1]) , -x[0] * x[0] * sin(x[i]) ]
	*/
	ok &amp;=  <a href="nearequal.xml" target="_top">NearEqual</a>( 2.*x[0]*exp(x[1]), jac[0*n+0], 1e-10, 1e-10 );
	ok &amp;=  <a href="nearequal.xml" target="_top">NearEqual</a>( 2.*x[0]*sin(x[1]), jac[1*n+0], 1e-10, 1e-10 );
	ok &amp;=  <a href="nearequal.xml" target="_top">NearEqual</a>( 2.*x[0]*cos(x[1]), jac[2*n+0], 1e-10, 1e-10 );

	ok &amp;=  <a href="nearequal.xml" target="_top">NearEqual</a>( x[0] * x[0] *exp(x[1]), jac[0*n+1], 1e-10, 1e-10 );
	ok &amp;=  <a href="nearequal.xml" target="_top">NearEqual</a>( x[0] * x[0] *cos(x[1]), jac[1*n+1], 1e-10, 1e-10 );
	ok &amp;=  <a href="nearequal.xml" target="_top">NearEqual</a>(-x[0] * x[0] *sin(x[1]), jac[2*n+1], 1e-10, 1e-10 );

	return ok;
}


</pre>

</font></code>


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

</body>
</html>
