<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>ADFun Sequence Properties: Example and Test</title>
<meta name="description" id="description" content="ADFun Sequence Properties: Example and Test"/>
<meta name="keywords" id="keywords" content=" Domain Adfun Range Parameter size_var size_par size_op size_op_arg size_vecad 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='_seq_property.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="seq_property.xml" target="_top">Prev</a>
</td><td><a href="funeval.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>seq_property</option>
<option>seq_property.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>seq_property-&gt;</option>
<option>seq_property.cpp</option>
</select>
</td>
<td>seq_property.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>ADFun Sequence Properties: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 

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

bool seq_property(void)
{	bool ok = true;
	using CppAD::AD;

	// Use nvar to track the number of variables in the operation sequence.
	// Start with one for the phantom variable at tape address zero.
	size_t nvar = 1;

	// Use npar to track the number of parameters in the operation sequence.
	size_t npar = 0;

	// Start with one for operator corresponding to phantom variable
	size_t nop  = 1;

	// Start with one for operator corresponding to phantom argument
	size_t narg = 1;

	// Use ntext to track the number of characters used to label
	// output generated using PrintFor commands.
	size_t ntext = 0;

	// Use nvecad to track the number of VecAD vectors, plus the number
	// of VecAD vector elements, in the operation sequence.
	size_t nvecad = 0;

	// a VecAD vector
	CppAD::VecAD&lt;double&gt; v(2);
	v[0]     = 0; // requires the parameter 0, when becomes a variable
	v[1]     = 1; // requires the parameter 1, when becomes a variable

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

	// declare independent variables and start tape recording
	CppAD::<a href="independent.xml" target="_top">Independent</a>(x); 
	nvar    += n;
	nop     += n;

	// a computation that adds to the operation sequence
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; I = 0;
	v[I]         = x[0]; 
	nvecad      +=   3;  // one for vector, two for its elements 
	npar        +=   2;  // need parameters 0 and 1 for initial v
	nop         +=   1;  // operator for storing in a VecAD object
	narg        +=   3;  // the three arguments are v, I, and x[0]

	// some operations that do not add to the operation sequence
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; u = x[0];  // use same variable as x[0]
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; w = x[1];  // use same variable as x[1]

	// a computation that adds to the operation sequence
	w      = w * (u + w); 
	nop   += 2;   // requires two new operators, an add and a multiply
	nvar  += 2;   // each operator results in its own variable
	narg  += 4;   // each operator has two arguments

	// range space vector
	size_t m = 3;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) y(m);

	// operations that do not add to the operation sequence
	y[0]   = 1.;  // re-use the parameter 1   
	y[1]   = u;   // use same variable as u

	// a computation that adds to the operation sequence
	y[2]   = w + 2.;
	nop   += 1;   // requires a new add operator
	npar  += 1;   // new parameter 2 is new, so it must be included
	nvar  += 1;   // variable corresponding to the result
	narg  += 2;   // operator has two arguments

	// create f: x -&gt; y and stop tape recording
	CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; f(x, y); 
	nop   += 1;   // special operator for y[0] becasue it is a parameter
	nvar  += 1;   // special variable for y[0] because it is a parameter
	narg  += 1;   // identifies which parameter corresponds to y[0]
	nop   += 1;   // special operator at the end of operation sequence

	// check the sequence property functions
	ok &amp;= f.Domain()      == n;
	ok &amp;= f.Range()       == m;
	ok &amp;= f.Parameter(0)  == true;
	ok &amp;= f.Parameter(1)  == false;
	ok &amp;= f.Parameter(2)  == false;
	ok &amp;= f.size_var()    == nvar;
	ok &amp;= f.size_op()     == nop;
	ok &amp;= f.size_op_arg() == narg;
	ok &amp;= f.size_par()    == npar;
	ok &amp;= f.size_text()   == ntext;
	ok &amp;= f.size_VecAD()  == nvecad;
	size_t sum = 0;
	sum += nop    * sizeof(CppAD::OpCode);
	sum += narg   * sizeof(CPPAD_TAPE_ADDR_TYPE);
	sum += npar   * sizeof(double);
	sum += ntext  * sizeof(char);
	sum += nvecad * sizeof(CPPAD_TAPE_ADDR_TYPE);
	ok &amp;= f.size_op_seq() == sum;

	return ok;
}

</pre>

</font></code>


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

</body>
</html>
