<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Using Adolc with Multiple Levels of Taping: Example and Test</title>
<meta name="description" id="description" content="Using Adolc with Multiple Levels of Taping: Example and Test"/>
<meta name="keywords" id="keywords" content=" multiple Adolc 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_adolc.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="base_adolc.hpp.xml" target="_top">Prev</a>
</td><td><a href="base_float.hpp.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>base_require</option>
<option>base_example</option>
<option>base_adolc.hpp</option>
<option>mul_level_adolc.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>base_require-&gt;</option>
<option>base_member</option>
<option>base_cond_exp</option>
<option>base_identical</option>
<option>base_ordered</option>
<option>base_std_math</option>
<option>base_example</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>base_example-&gt;</option>
<option>base_alloc.hpp</option>
<option>base_require.cpp</option>
<option>base_adolc.hpp</option>
<option>base_float.hpp</option>
<option>base_double.hpp</option>
<option>base_complex.hpp</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>base_adolc.hpp-&gt;</option>
<option>mul_level_adolc.cpp</option>
</select>
</td>
<td>mul_level_adolc.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Purpose</option>
<option>Memory Management</option>
<option>Configuration Requirement</option>
</select>
</td>
</tr></table><br/>



<center><b><big><big>Using Adolc with Multiple Levels of Taping: 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; adouble&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 Adolc's <code><font color="blue">adouble</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.cpp.xml" target="_top"><span style='white-space: nowrap'>mul_level.cpp</span></a>
 computes the same values using
<code><font color="blue">AD&lt; AD&lt;double&gt; &gt;</font></code> and <code><font color="blue">AD&lt;double&gt;</font></code>.

<br/>
<br/>
<b><big><a name="Memory Management" id="Memory Management">Memory Management</a></big></b>
<br/>
Adolc uses raw memory arrays that depend on the number of 
dependent and independent variables.
The memory management utility <a href="omp_alloc.xml" target="_top"><span style='white-space: nowrap'>omp_alloc</span></a>
 
is used to manage this memory allocation.

<br/>
<br/>
<b><big><a name="Configuration Requirement" id="Configuration Requirement">Configuration Requirement</a></big></b>
<br/>
This example will be compiled and tested provided that
the value <a href="ipopt_prefix.xml" target="_top"><span style='white-space: nowrap'>ipopt_prefix</span></a>
 is specified on the 
<a href="cmake.xml" target="_top"><span style='white-space: nowrap'>cmake</span></a>
 command line.

<code><font color="blue">
<pre style='display:inline'> 
# include &lt;adolc/adouble.h&gt;
# include &lt;adolc/taping.h&gt;
# include &lt;adolc/interfaces.h&gt;

// adouble definitions not in Adolc distribution and 
// required in order to use CppAD::<a href="ad.xml" target="_top">AD</a>&lt;adouble&gt;
# include &lt;cppad/example/base_adolc.hpp&gt;

# 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_adolc(void) 
{	bool ok = true;                // initialize test result
	using CppAD::omp_alloc;        // The CppAD memory allocator

	typedef adouble             ADdouble;  // for first level of taping
	typedef CppAD::<a href="ad.xml" target="_top">AD</a>&lt;ADdouble&gt; ADDdouble; // for second level of taping
	size_t n = 5;                          // number independent variables
	size_t j;

	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double)       x(n);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(ADdouble)   a_x(n);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(ADDdouble) aa_x(n);

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

	// Use <a href="ad.xml" target="_top">AD</a>&lt;adouble&gt; to tape the evaluation of f(x)
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(ADDdouble) aa_f(1); 
	aa_f[0] = f(aa_x); 

	// Declare a_F as the corresponding <a href="funconstruct.xml" target="_top">ADFun</a>&lt;adouble&gt; function f(x)
	// (make sure we do not run zero order forward during constructor)
	CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;ADdouble&gt; a_F;
	a_F.Dependent(aa_x, aa_f);

	// Value of the independent variables whitle taping f'(x) * v
	int tag = 0; 
	int keep = 1;
	trace_on(tag, keep);
	for(j = 0; j &lt; n; j++)
		a_x[j] &lt;&lt;= double(j);

	// set the argument value x for computing f'(x) * v
	a_F.<a href="forward.xml" target="_top">Forward</a>(0, a_x);

	// compute f'(x) * v
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(ADdouble) a_v(n);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(ADdouble) a_df(1);
	for(j = 0; j &lt; n; j++)
		a_v[j] = double(n - j);
	a_df = a_F.<a href="forward.xml" target="_top">Forward</a>(1, a_v); 

	// declare Adolc function corresponding to f'(x) * v
	double df; 
	a_df[0] &gt;&gt;= df;
	trace_off();

	// compute the d/dx of f'(x) * v = f''(x) * v
	size_t m      = 1;                     // # dependent in f'(x) * v

	// w = new double[capacity] where capacity &gt;= m
	size_t capacity;
	double* w  = omp_alloc::create_array&lt;double&gt;(m, capacity);
	// w = new double[capacity] where capacity &gt;= n
	double* dw = omp_alloc::create_array&lt;double&gt;(n, capacity);

	w[0]  = 1.;
	fos_reverse(tag, int(m), int(n), w, dw);

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

	// make memory avaialble for other use by this thread
	omp_alloc::delete_array(w);
	omp_alloc::delete_array(dw);
	return ok;
}
</pre>

</font></code>


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

</body>
</html>
