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

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

eigen_array.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>Using Eigen Arrays: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Using Eigen Arrays: Example and Test"/>
<meta name="keywords" id="keywords" content=" using eigen arrays: example and test array "/>
<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='_eigen_array.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="eigen_plugin.hpp.xml" target="_top">Prev</a>
</td><td><a href="eigen_det.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>cppad_eigen.hpp</option>
<option>eigen_array.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>Example-&gt;</option>
<option>General</option>
<option>ExampleUtility</option>
<option>ListAllExamples</option>
<option>testvector</option>
</select>
</td>
<td>
<select onchange='choose_down2(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>
<select onchange='choose_down1(this)'>
<option>cppad_eigen.hpp-&gt;</option>
<option>eigen_plugin.hpp</option>
<option>eigen_array.cpp</option>
<option>eigen_det.cpp</option>
</select>
</td>
<td>eigen_array.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Using Eigen Arrays: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 
# include &lt;cppad/example/cppad_eigen.hpp&gt;
# include &lt;cppad/speed/det_by_minor.hpp&gt;
# include &lt;Eigen/Dense&gt;

bool eigen_array(void)
{	bool ok = true;
	using CppAD::AD;
	using CppAD::NearEqual;
	using Eigen::Matrix;
	using Eigen::Dynamic;
	//
	typedef Matrix&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; , Dynamic, 1 &gt; a_vector;
	//
	// some temporary indices
	size_t i, j;

	// domain and range space vectors
	size_t n  = 10, m = n;
	a_vector a_x(n), a_y(m);

	// set and declare independent variables and start tape recording
	for(j = 0; j &lt; n; j++)
		a_x[j] = double(1 + j);
	CppAD::<a href="independent.xml" target="_top">Independent</a>(a_x);

	// evaluate a component wise function
	a_y = a_x.array() + sin(a_x.array()); 
	
	// create f: x -&gt; y and stop tape recording
	CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; f(a_x, a_y); 

	// compute the derivative of y w.r.t x using CppAD
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) x(n);
	for(j = 0; j &lt; n; j++)
		x[j] = double(j) + 1.0 / double(j+1);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) jac = f.<a href="jacobian.xml" target="_top">Jacobian</a>(x);

	// check Jacobian
	double eps = 100. * CppAD::numeric_limits&lt;double&gt;::epsilon();
	for(i = 0; i &lt; m; i++)
	{	for(j = 0; j &lt; n; j++)
		{	double check = 1.0 + cos(x[i]);
			if( i != j )
				check = 0.0;
			ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(jac[i * n + j], check, eps, eps);
		}
	}

	return ok;
}

</pre>

</font></code>


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

</body>
</html>