<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Adolc Speed: Sparse Hessian</title>
<meta name="description" id="description" content="Adolc Speed: Sparse Hessian"/>
<meta name="keywords" id="keywords" content=" link_sparse_hessian adolc speed sparse hessian "/>
<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='_adolc_sparse_hessian.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="adolc_poly.cpp.xml" target="_top">Prev</a>
</td><td><a href="adolc_sparse_jacobian.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>speed</option>
<option>speed_adolc</option>
<option>adolc_sparse_hessian.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>CppAD-&gt;</option>
<option>Install</option>
<option>Introduction</option>
<option>AD</option>
<option>ADFun</option>
<option>preprocessor</option>
<option>multi_thread</option>
<option>library</option>
<option>ipopt_solve</option>
<option>Example</option>
<option>speed</option>
<option>Appendix</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>speed-&gt;</option>
<option>speed_main</option>
<option>speed_utility</option>
<option>speed_double</option>
<option>speed_adolc</option>
<option>speed_cppad</option>
<option>speed_fadbad</option>
<option>speed_sacado</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>speed_adolc-&gt;</option>
<option>adolc_det_minor.cpp</option>
<option>adolc_det_lu.cpp</option>
<option>adolc_mat_mul.cpp</option>
<option>adolc_ode.cpp</option>
<option>adolc_poly.cpp</option>
<option>adolc_sparse_hessian.cpp</option>
<option>adolc_sparse_jacobian.cpp</option>
<option>adolc_alloc_mat</option>
</select>
</td>
<td>adolc_sparse_hessian.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Specifications</option>
<option>Implementation</option>
</select>
</td>
</tr></table><br/>



<center><b><big><big>Adolc Speed: Sparse Hessian</big></big></b></center>
<br/>
<b><big><a name="Specifications" id="Specifications">Specifications</a></big></b>
<br/>
See <a href="link_sparse_hessian.xml" target="_top"><span style='white-space: nowrap'>link_sparse_hessian</span></a>
.

<br/>
<br/>
<b><big><a name="Implementation" id="Implementation">Implementation</a></big></b>


<code><font color='blue'><pre style='display:inline'> 
# include &lt;adolc/adolc.h&gt;
# include &lt;adolc/adolc_sparse.h&gt;
# include &lt;cppad/vector.hpp&gt;
# include &lt;cppad/speed/uniform_01.hpp&gt;
# include &lt;cppad/thread_alloc.hpp&gt;
# include &lt;cppad/speed/sparse_hes_fun.hpp&gt;

// list of possible options
extern bool global_memory, global_onetape, global_atomic, global_optimize;
extern bool global_colpack, global_boolsparsity;

bool link_sparse_hessian(
	size_t                           size     , 
	size_t                           repeat   , 
	const CppAD::vector&lt;size_t&gt;&amp;     row      ,
	const CppAD::vector&lt;size_t&gt;&amp;     col      ,
	      CppAD::vector&lt;double&gt;&amp;     x_return ,
	      CppAD::vector&lt;double&gt;&amp;     hessian  )
{
	if( global_atomic || (! global_colpack) )
		return false; 
	if( global_memory || global_optimize || global_boolsparsity )
		return false;
	// -----------------------------------------------------
	// setup
	typedef unsigned int*    SizeVector;
	typedef double*          DblVector;
	typedef adouble          ADScalar;
	typedef ADScalar*        ADVector;


	size_t i, j;         // temporary indices
	size_t order = 0;    // derivative order corresponding to function
	size_t m = 1;        // number of dependent variables
	size_t n = size;     // number of independent variables

	// setup for thread_alloc memory allocator (fast and checks for leaks)
	using CppAD::thread_alloc; // the allocator
	size_t capacity;           // capacity of an allocation

	// tape identifier
	int tag  = 0;
	// AD domain space vector
	ADVector a_x = thread_alloc::create_array&lt;ADScalar&gt;(n, capacity);
	// AD range space vector
	ADVector a_y = thread_alloc::create_array&lt;ADScalar&gt;(m, capacity);
	// double argument value 
	DblVector x = thread_alloc::create_array&lt;double&gt;(n, capacity);
	// double function value 
	double f;

	// options that control sparse_hess
	int        options[2];
	options[0] = 0; // safe mode
	options[1] = 0; // indirect recovery

	// structure that holds some of the work done by sparse_hess
	int        nnz;                   // number of non-zero values
	SizeVector rind   = CPPAD_NULL;   // row indices
	SizeVector cind   = CPPAD_NULL;   // column indices
	DblVector  values = CPPAD_NULL;   // Hessian values

	// initialize all entries as zero
	for(i = 0; i &lt; m; i++)
	{	for(j = 0; j &lt; n; j++)
			hessian[ i * n + j ] = 0.;
	}
	// ----------------------------------------------------------------------
	if( ! global_onetape ) while(repeat--)
	{	// choose a value for x
		CppAD::uniform_01(n, x);

		// declare independent variables
		int keep = 0; // keep forward mode results 
		trace_on(tag, keep);
		for(j = 0; j &lt; n; j++)
			a_x[j] &lt;&lt;= x[j];

		// AD computation of f (x) 
		CppAD::sparse_hes_fun&lt;ADScalar&gt;(n, a_x, row, col, order, a_y);

		// create function object f : x -&gt; y 
		a_y[0] &gt;&gt;= f;
		trace_off();

		// is this a repeat call with the same sparsity pattern
		int same_pattern = 0;

		// calculate the hessian at this x
		rind   = CPPAD_NULL;
		cind   = CPPAD_NULL;
		values = CPPAD_NULL;
		sparse_hess(tag, int(n), 
			same_pattern, x, &amp;nnz, &amp;rind, &amp;cind, &amp;values, options
		);
		int int_n = int(n);
		for(int k = 0; k &lt; nnz; k++)
		{	hessian[ rind[k] * int_n + cind[k] ] = values[k];
			hessian[ cind[k] * int_n + rind[k] ] = values[k];
		}

		// free raw memory allocated by sparse_hess
		free(rind);
		free(cind);
		free(values);
	}
	else
	{	// choose a value for x
		CppAD::uniform_01(n, x);

		// declare independent variables
		int keep = 0; // keep forward mode results 
		trace_on(tag, keep);
		for(j = 0; j &lt; n; j++)
			a_x[j] &lt;&lt;= x[j];

		// AD computation of f (x) 
		CppAD::sparse_hes_fun&lt;ADScalar&gt;(n, a_x, row, col, order, a_y);

		// create function object f : x -&gt; y
		a_y[0] &gt;&gt;= f;
		trace_off();

		// is this a repeat call at the same argument
		int same_pattern = 0;

		while(repeat--)
		{	// choose a value for x
			CppAD::uniform_01(n, x);

			// calculate the hessian at this x
			sparse_hess(tag, int(n), 
				same_pattern, x, &amp;nnz, &amp;rind, &amp;cind, &amp;values, options
			);
			same_pattern = 1;
		}
		int int_n = int(n);
		for(int k = 0; k &lt; nnz; k++)
		{	hessian[ rind[k] * int_n + cind[k] ] = values[k];
			hessian[ cind[k] * int_n + rind[k] ] = values[k];
		}

		// free raw memory allocated by sparse_hessian
		free(rind);
		free(cind);
		free(values);
	}
	// --------------------------------------------------------------------
	// return argument 
	for(j = 0; j &lt; n; j++)
		x_return[j] = x[j];

	// tear down
	thread_alloc::delete_array(a_x);
	thread_alloc::delete_array(a_y);
	thread_alloc::delete_array(x);
	return true;

}
</pre></font></code>


<hr/>Input File: speed/adolc/sparse_hessian.cpp

</body>
</html>
