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

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

var2par.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>Convert an AD Variable to a Parameter: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Convert an AD Variable to a Parameter: Example and Test"/>
<meta name="keywords" id="keywords" content=" convert an ad variable to a parameter: example and test Var2par Value during taping "/>
<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='_var2par.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="var2par.xml" target="_top">Prev</a>
</td><td><a href="advalued.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>Convert</option>
<option>Var2Par</option>
<option>var2par.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>AD-&gt;</option>
<option>ad_ctor</option>
<option>ad_assign</option>
<option>Convert</option>
<option>ADValued</option>
<option>BoolValued</option>
<option>VecAD</option>
<option>base_require</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>Convert-&gt;</option>
<option>Value</option>
<option>Integer</option>
<option>ad_input</option>
<option>ad_output</option>
<option>PrintFor</option>
<option>Var2Par</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>Var2Par-&gt;</option>
<option>var2par.cpp</option>
</select>
</td>
<td>var2par.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Convert an AD Variable to a Parameter: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 

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


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

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

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

	// range space vector 
	size_t m = 1;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) y(m);
	y[0] = - x[1] * Var2Par(x[0]);    // same as y[0] = -x[1] * 3.;

	// cannot call Value(x[j]) or Value(y[0]) here (currently variables)
	ok &amp;= ( Value( Var2Par(x[0]) ) == 3. );
	ok &amp;= ( Value( Var2Par(x[1]) ) == 4. ); 
	ok &amp;= ( Value( Var2Par(y[0]) ) == -12. ); 

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

	// can call Value(x[j]) or Value(y[0]) here (currently parameters)
	ok &amp;= (Value(x[0]) ==  3.);
	ok &amp;= (Value(x[1]) ==  4.);
	ok &amp;= (Value(y[0]) == -12.);

	// evaluate derivative of y w.r.t x
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) w(m);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dw(n);
	w[0] = 1.;
	dw   = f.<a href="reverse.xml" target="_top">Reverse</a>(1, w);
	ok  &amp;= (dw[0] == 0.);  // derivative of y[0] w.r.t x[0] is zero
	ok  &amp;= (dw[1] == -3.); // derivative of y[0] w.r.t x[1] is 3

	return ok;
}
</pre>

</font></code>


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

</body>
</html>