<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Multiple Tapes: Example and Test</title>
<meta name="description" id="description" content="Multiple Tapes: Example and Test"/>
<meta name="keywords" id="keywords" content=" multiple Ad level "/>
<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_level.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="mul_level.xml" target="_top">Prev</a>
</td><td><a href="change_const.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>Example</option>
<option>General</option>
<option>mul_level</option>
<option>mul_level.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>Example-&gt;</option>
<option>General</option>
<option>ExampleUtility</option>
<option>ListAllExamples</option>
<option>testvector</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>General-&gt;</option>
<option>ad_fun.cpp</option>
<option>ad_in_c.cpp</option>
<option>conj_grad.cpp</option>
<option>cppad_eigen.hpp</option>
<option>hes_minor_det.cpp</option>
<option>hes_lu_det.cpp</option>
<option>interface2c.cpp</option>
<option>jac_minor_det.cpp</option>
<option>jac_lu_det.cpp</option>
<option>mul_level</option>
<option>ode_stiff.cpp</option>
<option>mul_level_ode.cpp</option>
<option>mul_level_adolc_ode.cpp</option>
<option>ode_taylor.cpp</option>
<option>stack_machine.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>mul_level-&gt;</option>
<option>mul_level.cpp</option>
<option>change_const.cpp</option>
</select>
</td>
<td>mul_level.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Purpose</option>
</select>
</td>
</tr></table><br/>



<center><b><big><big>Multiple Tapes: Example and Test</big></big></b></center>
<br/>
<b><big><a name="Purpose" id="Purpose">Purpose</a></big></b>
<br/>
In this example, we use <code><font color="blue">AD&lt; AD&lt;double&gt; &gt;</font></code> (level two taping),
the compute values of the function 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>f</mi>
<mo stretchy="false">:</mo>
<msup><mrow><mstyle mathvariant='bold'><mi mathvariant='bold'>R</mi>
</mstyle></mrow>
<mi mathvariant='italic'>n</mi>
</msup>
<mo stretchy="false">&#x02192;</mo>
<mrow><mstyle mathvariant='bold'><mi mathvariant='bold'>R</mi>
</mstyle></mrow>
</mrow></math>

 where 

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mi mathvariant='italic'>f</mi>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">=</mo>
<mfrac><mrow><mn>1</mn>
</mrow>
<mrow><mn>2</mn>
</mrow>
</mfrac>
<mrow><mo stretchy="true">(</mo><mrow><msubsup><mi mathvariant='italic'>x</mi>
<mn>0</mn>
<mn>2</mn>
</msubsup>
<mo stretchy="false">+</mo>
<mo stretchy="false">&#x022EF;</mo>
<mo stretchy="false">+</mo>
<msubsup><mi mathvariant='italic'>x</mi>
<mrow><mi mathvariant='italic'>n</mi>
<mn>-1</mn>
</mrow>
<mn>2</mn>
</msubsup>
</mrow><mo stretchy="true">)</mo></mrow>
</mrow></math>

We then use <code><font color="blue">AD&lt;double&gt;</font></code> (level one taping) to compute
the directional derivative

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<msup><mi mathvariant='italic'>f</mi>
<mrow><mo stretchy="false">(</mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">*</mo>
<mi mathvariant='italic'>v</mi>
<mo stretchy="false">=</mo>
<msub><mi mathvariant='italic'>x</mi>
<mn>0</mn>
</msub>
<msub><mi mathvariant='italic'>v</mi>
<mn>0</mn>
</msub>
<mo stretchy="false">+</mo>
<mo stretchy="false">&#x022EF;</mo>
<mo stretchy="false">+</mo>
<msub><mi mathvariant='italic'>x</mi>
<mrow><mi mathvariant='italic'>n</mi>
<mn>-1</mn>
</mrow>
</msub>
<msub><mi mathvariant='italic'>v</mi>
<mrow><mi mathvariant='italic'>n</mi>
<mn>-1</mn>
</mrow>
</msub>
</mrow></math>

.
where 
<math xmlns="http://www.w3.org/1998/Math/MathML" display="inline"><mrow>
<mi mathvariant='italic'>v</mi>
<mo stretchy="false">&#x02208;</mo>
<msup><mrow><mstyle mathvariant='bold'><mi mathvariant='bold'>R</mi>
</mstyle></mrow>
<mi mathvariant='italic'>n</mi>
</msup>
</mrow></math>

.
We then use <code><font color="blue">double</font></code> (no taping) to compute

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mfrac><mrow><mi mathvariant='italic'>d</mi>
</mrow>
<mrow><mi mathvariant='italic'>dx</mi>
</mrow>
</mfrac>
<mrow><mo stretchy="true">[</mo><mrow><msup><mi mathvariant='italic'>f</mi>
<mrow><mo stretchy="false">(</mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">*</mo>
<mi mathvariant='italic'>v</mi>
</mrow><mo stretchy="true">]</mo></mrow>
<mo stretchy="false">=</mo>
<mi mathvariant='italic'>v</mi>
</mrow></math>

This is only meant as an example of multiple levels of taping.
The example <a href="hes_times_dir.cpp.xml" target="_top"><span style='white-space: nowrap'>hes_times_dir.cpp</span></a>
 computes the same value more
efficiently by using the identity:

<math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow>
<mfrac><mrow><mi mathvariant='italic'>d</mi>
</mrow>
<mrow><mi mathvariant='italic'>dx</mi>
</mrow>
</mfrac>
<mrow><mo stretchy="true">[</mo><mrow><msup><mi mathvariant='italic'>f</mi>
<mrow><mo stretchy="false">(</mo>
<mn>1</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">*</mo>
<mi mathvariant='italic'>v</mi>
</mrow><mo stretchy="true">]</mo></mrow>
<mo stretchy="false">=</mo>
<msup><mi mathvariant='italic'>f</mi>
<mrow><mo stretchy="false">(</mo>
<mn>2</mn>
<mo stretchy="false">)</mo>
</mrow>
</msup>
<mo stretchy="false">(</mo>
<mi mathvariant='italic'>x</mi>
<mo stretchy="false">)</mo>
<mo stretchy="false">*</mo>
<mi mathvariant='italic'>v</mi>
</mrow></math>

The example <a href="mul_level_adolc.cpp.xml" target="_top"><span style='white-space: nowrap'>mul_level_adolc.cpp</span></a>
 computes the same values using
Adolc's type <code><font color="blue">adouble</font></code> and CppAD's type <code><font color="blue">AD&lt;adouble&gt;</font></code>.

<code><font color="blue">
<pre style='display:inline'> 

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

namespace { 
	// f(x) = |x|^2 / 2 = .5 * ( x[0]^2 + ... + x[n-1]^2 )
	template &lt;class Type&gt;
	Type f(CPPAD_TESTVECTOR(Type) &amp;x)
	{	Type sum;

		sum  = 0.;
		size_t i = size_t(x.size());
		for(i = 0; i &lt; size_t(x.size()); i++)
			sum += x[i] * x[i];

		return .5 * sum;
	} 
}

bool mul_level(void) 
{	bool ok = true;                          // initialize test result

	typedef CppAD::<a href="ad.xml" target="_top">AD</a>&lt;double&gt;    A1_double;  // for one level of taping
	typedef CppAD::<a href="ad.xml" target="_top">AD</a>&lt;A1_double&gt; A2_double;  // for two levels of taping
	size_t n = 5;                            // dimension for example
	size_t j;                                // a temporary index variable

	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double)       x(n);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(A1_double)  a1_x(n), a1_v(n), a1_dy(1) ;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(A2_double)  a2_x(n), a2_y(1);

	// Values for the independent variables while taping the function f(x)
	for(j = 0; j &lt; n; j++)
		a2_x[j] = a1_x[j] = x[j] = double(j);
	// Declare the independent variable for taping f(x)
	CppAD::<a href="independent.xml" target="_top">Independent</a>(a2_x); 

	// Use <a href="ad.xml" target="_top">AD</a>&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; to tape the evaluation of f(x)
	a2_y[0] = f(a2_x); 

	// Declare a1_F as the corresponding <a href="funconstruct.xml" target="_top">ADFun</a>&lt; <a href="ad.xml" target="_top">AD</a>&lt;double&gt; &gt; value a2_y
	// (make sure we do not run zero order forward during constructor)
	CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;A1_double&gt; a1_F;
	a1_F.Dependent(a2_x, a2_y);

	// Values for the independent variables while taping f'(x) * v
	// Declare the independent variable for taping f'(x) * v
	CppAD::<a href="independent.xml" target="_top">Independent</a>(a1_x); 
	// set the argument value x for computing f'(x) * v
	a1_F.<a href="forward.xml" target="_top">Forward</a>(0, a1_x);
	// compute f'(x) * v
	for(j = 0; j &lt; n; j++)
		a1_v[j] = double(n - j);
	a1_dy = a1_F.<a href="forward.xml" target="_top">Forward</a>(1, a1_v); 

	// declare dF as <a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; function corresponding to f'(x) * v
	CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; dF;
	dF.Dependent(a1_x, a1_dy);

	// optimize out operations not necessary for function f'(x) * v
	dF.optimize();

	// Evaluate f'(x) * v
	dF.<a href="forward.xml" target="_top">Forward</a>(0, x);

	// compute the d/dx of f'(x) * v = f''(x) * v = v
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) w(1);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dw(n);
	w[0] = 1.;
	dw   = dF.<a href="reverse.xml" target="_top">Reverse</a>(1, w);

	for(j = 0; j &lt; n; j++)
		ok &amp;= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(dw[j], a1_v[j], 1e-10, 1e-10);

	return ok;
}
</pre>

</font></code>


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

</body>
</html>
