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

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

rev_sparse_jac.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>Reverse Mode Jacobian Sparsity: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Reverse Mode Jacobian Sparsity: Example and Test"/>
<meta name="keywords" id="keywords" content=" reverse mode jacobian sparsity: example and test Revsparsejac sparsity "/>
<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='_rev_sparse_jac.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="revsparsejac.xml" target="_top">Prev</a>
</td><td><a href="revsparsehes.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>FunEval</option>
<option>Sparse</option>
<option>RevSparseJac</option>
<option>rev_sparse_jac.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>FunEval-&gt;</option>
<option>Forward</option>
<option>Reverse</option>
<option>Sparse</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>Sparse-&gt;</option>
<option>ForSparseJac</option>
<option>RevSparseJac</option>
<option>RevSparseHes</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>RevSparseJac-&gt;</option>
<option>rev_sparse_jac.cpp</option>
</select>
</td>
<td>rev_sparse_jac.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Reverse Mode Jacobian Sparsity: 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 BoolCases&lt;Vector&gt; 
template &lt;typename Vector&gt;  // vector class, elements of type bool
bool BoolCases(void)
{	bool ok = true;
	using CppAD::AD;

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

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

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

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

	// sparsity pattern for the identity matrix
	Vector r(m * m);
	size_t i, j;
	for(i = 0; i &lt; m; i++)
	{	for(j = 0; j &lt; m; j++)
			r[ i * m + j ] = (i == j);
	}

	// sparsity pattern for F'(x)
	Vector s(m * n);
	s = f.RevSparseJac(m, r);

	// check values
	ok &amp;= (s[ 0 * n + 0 ] == true);  // y[0] does     depend on x[0]
	ok &amp;= (s[ 0 * n + 1 ] == false); // y[0] does not depend on x[1]
	ok &amp;= (s[ 1 * n + 0 ] == true);  // y[1] does     depend on x[0]
	ok &amp;= (s[ 1 * n + 1 ] == true);  // y[1] does     depend on x[1]
	ok &amp;= (s[ 2 * n + 0 ] == false); // y[2] does not depend on x[0]
	ok &amp;= (s[ 2 * n + 1 ] == true);  // y[2] does     depend on x[1]

	// sparsity pattern for F'(x)^T, note R is the identity, so R^T = R
	bool transpose = true;
	Vector st(n * m);
	st = f.RevSparseJac(m, r, transpose);

	// check values
	ok &amp;= (st[ 0 * m + 0 ] == true);  // y[0] does     depend on x[0]
	ok &amp;= (st[ 1 * m + 0 ] == false); // y[0] does not depend on x[1]
	ok &amp;= (st[ 0 * m + 1 ] == true);  // y[1] does     depend on x[0]
	ok &amp;= (st[ 1 * m + 1 ] == true);  // y[1] does     depend on x[1]
	ok &amp;= (st[ 0 * m + 2 ] == false); // y[2] does not depend on x[0]
	ok &amp;= (st[ 1 * m + 2 ] == true);  // y[2] does     depend on x[1]

	return ok;
}
// define the template function SetCases&lt;Vector&gt; 
template &lt;typename Vector&gt;  // vector class, elements of type std::set&lt;size_t&gt;
bool SetCases(void)
{	bool ok = true;
	using CppAD::AD;

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

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

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

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

	// sparsity pattern for the identity matrix
	Vector r(m);
	size_t i;
	for(i = 0; i &lt; m; i++)
	{	assert( r[i].empty() );
		r[i].insert(i);
	}

	// sparsity pattern for F'(x)
	Vector s(m);
	s = f.RevSparseJac(m, r);

	// check values
	bool found;

	// y[0] does     depend on x[0]
	found = s[0].find(0) != s[0].end();  ok &amp;= (found == true);  
	// y[0] does not depend on x[1]
	found = s[0].find(1) != s[0].end();  ok &amp;= (found == false); 
	// y[1] does     depend on x[0]
	found = s[1].find(0) != s[1].end();  ok &amp;= (found == true);  
	// y[1] does     depend on x[1]
	found = s[1].find(1) != s[1].end();  ok &amp;= (found == true);  
	// y[2] does not depend on x[0]
	found = s[2].find(0) != s[2].end();  ok &amp;= (found == false); 
	// y[2] does     depend on x[1]
	found = s[2].find(1) != s[2].end();  ok &amp;= (found == true);  

	// sparsity pattern for F'(x)^T
	bool transpose = true;
	Vector st(n);
	st = f.RevSparseJac(m, r, transpose);

	// y[0] does     depend on x[0]
	found = st[0].find(0) != st[0].end();  ok &amp;= (found == true);  
	// y[0] does not depend on x[1]
	found = st[1].find(0) != st[1].end();  ok &amp;= (found == false); 
	// y[1] does     depend on x[0]
	found = st[0].find(1) != st[0].end();  ok &amp;= (found == true);  
	// y[1] does     depend on x[1]
	found = st[1].find(1) != st[1].end();  ok &amp;= (found == true);  
	// y[2] does not depend on x[0]
	found = st[0].find(2) != st[0].end();  ok &amp;= (found == false); 
	// y[2] does     depend on x[1]
	found = st[1].find(2) != st[1].end();  ok &amp;= (found == true);  

	return ok;
}
} // End empty namespace
# include &lt;vector&gt;
# include &lt;valarray&gt;
bool RevSparseJac(void)
{	bool ok = true;
	// Run with Vector equal to four different cases
	// all of which are Simple Vectors with elements of type bool.
	ok &amp;= BoolCases&lt; CppAD::vectorBool     &gt;();
	ok &amp;= BoolCases&lt; CppAD::vector  &lt;bool&gt; &gt;();
	ok &amp;= BoolCases&lt; std::vector    &lt;bool&gt; &gt;(); 
	ok &amp;= BoolCases&lt; std::valarray  &lt;bool&gt; &gt;(); 


	// Run with Vector equal to two different cases both of which are 
	// Simple Vectors with elements of type std::set&lt;size_t&gt;
	typedef std::set&lt;size_t&gt; set;
	ok &amp;= SetCases&lt; CppAD::vector  &lt;set&gt; &gt;();
	ok &amp;= SetCases&lt; std::vector    &lt;set&gt; &gt;(); 

	// Do not use valarray because its element access in the const case
	// returns a copy instead of a reference
	// ok &amp;= SetCases&lt; std::valarray  &lt;set&gt; &gt;(); 

	return ok;
}

</pre>

</font></code>


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

</body>
</html>