<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Taping Array Index Operation: Example and Test</title>
<meta name="description" id="description" content="Taping Array Index Operation: Example and Test"/>
<meta name="keywords" id="keywords" content=" array tape index operation "/>
<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='_tape_index.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="discrete.xml" target="_top">Prev</a>
</td><td><a href="interp_onetape.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>Discrete</option>
<option>tape_index.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>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_down1(this)'>
<option>Discrete-&gt;</option>
<option>tape_index.cpp</option>
<option>interp_onetape.cpp</option>
<option>interp_retape.cpp</option>
</select>
</td>
<td>tape_index.cpp</td>
<td>Headings</td>
</tr></table><br/>


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

namespace {
	double Array(const double &amp;index)
	{	static double array[] = {
			5.,
			4.,
			3.,
			2.,
			1.
		};
		static size_t number = sizeof(array) / sizeof(array[0]);
		if( index &lt; 0. )
			return array[0];

		size_t i = static_cast&lt;size_t&gt;(index);
		if( i &gt;= number )
			return array[number-1];

		return array[i];
	}
	// in empty namespace and outside any other routine
	CPPAD_DISCRETE_FUNCTION(double, Array)
}

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

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

	// 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] * Array( X[0] );

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

	// vectors for arguments to the function object f
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) x(n);   // argument values
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) y(m);   // function values 
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) w(m);   // function weights 
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dw(n);  // derivative of weighted function

	// check function value
	x[0] = Value(X[0]);
	x[1] = Value(X[1]);
	y[0] = Value(Y[0]);
	ok  &amp;= y[0] == x[1] * Array(x[0]);

	// evaluate f where x has different values
	x[0] = x[0] + 1.;  // new array index value
	x[1] = x[1] + 1.;  // new multiplier value
	y    = f.<a href="forward.xml" target="_top">Forward</a>(0, x);
	ok  &amp;= y[0] == x[1] * Array(x[0]);

	// evaluate derivaitve of y[0] 
	w[0] = 1.;
	dw   = f.<a href="reverse.xml" target="_top">Reverse</a>(1, w);
	ok   &amp;= dw[0] == 0.;              // partial w.r.t array index
	ok   &amp;= dw[1] == Array(x[0]);     // partial w.r.t multiplier

	return ok;
}

</pre>

</font></code>


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

</body>
</html>
