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

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

mul.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>AD Binary Multiplication: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="AD Binary Multiplication: Example and Test"/>
<meta name="keywords" id="keywords" content=" ad binary multiplication: example and test * Ad multiply times "/>
<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='_mul.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="sub.cpp.xml" target="_top">Prev</a>
</td><td><a href="div.cpp.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>ADValued</option>
<option>Arithmetic</option>
<option>ad_binary</option>
<option>mul.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>ADValued-&gt;</option>
<option>Arithmetic</option>
<option>std_math_ad</option>
<option>MathOther</option>
<option>CondExp</option>
<option>Discrete</option>
<option>atomic</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>Arithmetic-&gt;</option>
<option>UnaryPlus</option>
<option>UnaryMinus</option>
<option>ad_binary</option>
<option>compute_assign</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>ad_binary-&gt;</option>
<option>add.cpp</option>
<option>sub.cpp</option>
<option>mul.cpp</option>
<option>div.cpp</option>
</select>
</td>
<td>mul.cpp</td>
<td>Headings</td>
</tr></table><br/>


<center><b><big><big>AD Binary Multiplication: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 
# include &lt;cppad/cppad.hpp&gt;

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

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

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

	// some binary multiplication operations
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; a = x[0] * 1.; // <a href="ad.xml" target="_top">AD</a>&lt;double&gt; * double
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; b = a    * 2;  // <a href="ad.xml" target="_top">AD</a>&lt;double&gt; * int
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; c = 3.   * b;  // double     * <a href="ad.xml" target="_top">AD</a>&lt;double&gt; 
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; d = 4    * c;  // int        * <a href="ad.xml" target="_top">AD</a>&lt;double&gt; 

	// 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[0] * d;          // <a href="ad.xml" target="_top">AD</a>&lt;double&gt; * <a href="ad.xml" target="_top">AD</a>&lt;double&gt;

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

	// check value 
	ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(y[0] , x0*(4.*3.*2.*1.)*x0,  1e-10 , 1e-10);

	// forward computation of partials w.r.t. x[0]
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dx(n);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dy(m);
	dx[0] = 1.;
	dy    = f.<a href="forward.xml" target="_top">Forward</a>(1, dx);
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[0], (4.*3.*2.*1.)*2.*x0, 1e-10 , 1e-10); 

	// reverse computation of derivative of y[0]
	<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;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[0], (4.*3.*2.*1.)*2.*x0, 1e-10 , 1e-10); 

	// use a VecAD&lt;Base&gt;::reference object with multiplication
	CppAD::VecAD&lt;double&gt; v(1);
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; zero(0);
	v[zero] = c;
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; result = 4 * v[zero];
	ok     &amp;= (result == d);

	return ok;
}

</pre>

</font></code>


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

</body>
</html>