<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Hessian Times Direction: Example and Test</title>
<meta name="description" id="description" content="Hessian Times Direction: Example and Test"/>
<meta name="keywords" id="keywords" content=" Hessian times direction "/>
<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='_hes_times_dir.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="reverse_two.cpp.xml" target="_top">Prev</a>
</td><td><a href="reverse_any.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>ADFun</option>
<option>FunEval</option>
<option>Reverse</option>
<option>reverse_two</option>
<option>hes_times_dir.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>FunEval-&gt;</option>
<option>Forward</option>
<option>Reverse</option>
<option>Sparse</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>Reverse-&gt;</option>
<option>reverse_one</option>
<option>reverse_two</option>
<option>reverse_any</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>reverse_two-&gt;</option>
<option>reverse_two.cpp</option>
<option>hes_times_dir.cpp</option>
</select>
</td>
<td>hes_times_dir.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Hessian Times Direction: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 
// Example and test of computing the Hessian times a direction; i.e.,
// given F : R^n -&gt; R and a direction dx in R^n, we compute F''(x) * dx

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

namespace { // put this function in the empty namespace
	// F(x) = |x|^2 = x[0]^2 + ... + x[n-1]^2
	template &lt;class Type&gt;
	Type F(CPPAD_TESTVECTOR(Type) &amp;x)
	{	Type sum = 0;
		size_t i = x.size();
		while(i--)
			sum += x[i] * x[i];
		return sum;
	} 
}

bool HesTimesDir(void)
{	bool ok = true;                   // initialize test result
	size_t j;                         // a domain variable variable

	using CppAD::AD;
	using CppAD::NearEqual;

	// domain space vector
	size_t n = 5; 
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;)  X(n);
	for(j = 0; j &lt; n; j++)
		X[j] = <a href="ad.xml" target="_top">AD</a>&lt;double&gt;(j); 

	// declare independent variables and start 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] = F(X);

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

	// choose a direction dx and compute dy(x) = F'(x) * dx
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dx(n);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dy(m);
	for(j = 0; j &lt; n; j++)
		dx[j] = double(n - j);
	dy = f.<a href="forward.xml" target="_top">Forward</a>(1, dx);

	// compute ddw = F''(x) * dx
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) w(m);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) ddw(2 * n);
	w[0] = 1.;
	ddw  = f.<a href="reverse.xml" target="_top">Reverse</a>(2, w);
	
	// F(x)        = x[0]^2 + x[1]^2 + ... + x[n-1]^2
	// F''(x)      = 2 * Identity_Matrix
	// F''(x) * dx = 2 * dx
	for(j = 0; j &lt; n; j++)
		ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(ddw[j * 2 + 1], 2.*dx[j], 1e-10, 1e-10);

	return ok;
}
</pre>

</font></code>


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

</body>
</html>
