<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Number of Variables That Can be Skipped: Example and Test</title>
<meta name="description" id="description" content="Number of Variables That Can be Skipped: Example and Test"/>
<meta name="keywords" id="keywords" content=" number_skip example optimize conditional expression condition "/>
<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='_number_skip.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="number_skip.xml" target="_top">Prev</a>
</td><td><a href="reverse.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>Forward</option>
<option>number_skip</option>
<option>number_skip.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>Forward-&gt;</option>
<option>forward_zero</option>
<option>forward_one</option>
<option>forward_two</option>
<option>forward_order</option>
<option>size_order</option>
<option>CompareChange</option>
<option>capacity_order</option>
<option>number_skip</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>number_skip-&gt;</option>
<option>number_skip.cpp</option>
</select>
</td>
<td>number_skip.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Number of Variables That Can be Skipped: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 
# include &lt;cppad/cppad.hpp&gt;
bool number_skip(void)
{	bool ok = true;
	using CppAD::AD;

	// independent variable vector 
	CppAD::vector&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; ax(2);
	ax[0] = 0.; 
	ax[1] = 1.;
	<a href="independent.xml" target="_top">Independent</a>(ax);
	
	// Use a conditional expression
	CppAD::vector&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; ay(1);

	// variable that gets optimized out
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; az = ax[0] * ax[0];

	
	// conditional expression
	ay[0] = CondExpLt(ax[0], ax[1], ax[0] + ax[1], ax[0] - ax[1]);
	
	// create function object F : x -&gt; ay
	CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; f;
	f.Dependent(ax, ay);

	// use zero order to evaluate F[ (3, 4) ] 
	CppAD::vector&lt;double&gt;  x( f.Domain() );
	CppAD::vector&lt;double&gt;  y( f.Range() );
	x[0]    = 3.;
	x[1]    = 4.;
	y   = f.<a href="forward.xml" target="_top">Forward</a>(0, x);
	ok &amp;= (y[0] == x[0] + x[1]); 

	// before call to optimize
	ok &amp;= f.number_skip() == 0;
	size_t n_var = f.size_var();
	
	// now optimize the operation sequence
	f.optimize();

	// after optimize, check forward mode result
	x[0]    = 4.;
	x[1]    = 3.;
	y   = f.<a href="forward.xml" target="_top">Forward</a>(0, x);
	ok &amp;= (y[0] == x[0] - x[1]); 

	// after optimize, check amount of optimization
	ok &amp;= f.size_var() == n_var - 1;
	ok &amp;= f.number_skip() == 1;
	
	return ok;
}

</pre>

</font></code>


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

</body>
</html>
