<?xml version='1.0'?>
<?xml-stylesheet type='text/xsl' href='pmathml.xsl'?>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<title>AD Assignment Operator: Example and Test</title>
<meta name="description" id="description" content="AD Assignment Operator: Example and Test"/>
<meta name="keywords" id="keywords" content=" assignment Ad 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='_eq.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="copybase.cpp.xml" target="_top">Prev</a>
</td><td><a href="convert.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>AD</option>
<option>ad_copy</option>
<option>Eq.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>multi_thread</option>
<option>library</option>
<option>cppad_ipopt_nlp</option>
<option>Example</option>
<option>preprocessor</option>
<option>Appendix</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>AD-&gt;</option>
<option>Default</option>
<option>ad_copy</option>
<option>Convert</option>
<option>ADValued</option>
<option>BoolValued</option>
<option>VecAD</option>
<option>base_require</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>ad_copy-&gt;</option>
<option>CopyAD.cpp</option>
<option>CopyBase.cpp</option>
<option>Eq.cpp</option>
</select>
</td>
<td>Eq.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>AD Assignment Operator: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 

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

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

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

	// declare independent variables and start tape recording
	CppAD::<a href="independent.xml" target="_top">Independent</a>(x);
	
	// range space vector 
	size_t m = 3;
	<a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; y(m);

	// assign an <a href="ad.xml" target="_top">AD</a>&lt;Base&gt; object equal to an independent variable
	// (choose the first independent variable to check a special case)
	// use the value returned by the assignment (for another assignment)
	y[0] = y[1] = x[0];  

	// assign an <a href="ad.xml" target="_top">AD</a>&lt;Base&gt; object equal to an expression 
	y[1] = x[1] + 1.;
	y[2] = x[2] + 2.;

	// check that all the resulting components of y depend on x
	ok &amp;= Variable(y[0]);  // y[0] = x[0]
	ok &amp;= Variable(y[1]);  // y[1] = x[1] + 1
	ok &amp;= Variable(y[2]);  // y[2] = x[2] + 2

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

	// check variable values
	ok &amp;= ( y[0] == 2.);
	ok &amp;= ( y[1] == 4.);
	ok &amp;= ( y[2] == 5.);

	// compute partials w.r.t x[1]
	<a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>&lt;double&gt; dx(n);
	<a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>&lt;double&gt; dy(m);
	dx[0] = 0.;
	dx[1] = 1.;
	dx[2] = 0.;
	dy   = f.<a href="forward.xml" target="_top">Forward</a>(1, dx);
	ok  &amp;= (dy[0] == 0.);  // dy[0] / dx[1]
	ok  &amp;= (dy[1] == 1.);  // dy[1] / dx[1]
	ok  &amp;= (dy[2] == 0.);  // dy[2] / dx[1]

	// compute the derivative y[2]
	<a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>&lt;double&gt;  w(m);
	<a href="test_vector.xml" target="_top">CPPAD_TEST_VECTOR</a>&lt;double&gt; dw(n);
	w[0] = 0.;
	w[1] = 0.;
	w[2] = 1.;
	dw   = f.<a href="reverse.xml" target="_top">Reverse</a>(1, w);
	ok  &amp;= (dw[0] == 0.);  // dy[2] / dx[0]
	ok  &amp;= (dw[1] == 0.);  // dy[2] / dx[1]
	ok  &amp;= (dw[2] == 1.);  // dy[2] / dx[2]

	// assign a VecAD&lt;Base&gt;::reference
	CppAD::VecAD&lt;double&gt; v(1);
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; zero(0);
	v[zero] = 5.;
	ok     &amp;= (v[0] == 5.);

	return ok;
}

</pre>

</font></code>


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

</body>
</html>

