<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>LuRatio: Example and Test</title>
<meta name="description" id="description" content="LuRatio: Example and Test"/>
<meta name="keywords" id="keywords" content=" example Luratio test "/>
<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='_lu_ratio.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="luratio.xml" target="_top">Prev</a>
</td><td><a href="ipopt_solve.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>library</option>
<option>LuRatio</option>
<option>lu_ratio.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>library-&gt;</option>
<option>ErrorHandler</option>
<option>NearEqual</option>
<option>speed_test</option>
<option>SpeedTest</option>
<option>time_test</option>
<option>NumericType</option>
<option>CheckNumericType</option>
<option>SimpleVector</option>
<option>CheckSimpleVector</option>
<option>nan</option>
<option>pow_int</option>
<option>Poly</option>
<option>LuDetAndSolve</option>
<option>RombergOne</option>
<option>RombergMul</option>
<option>Runge45</option>
<option>Rosen34</option>
<option>OdeErrControl</option>
<option>OdeGear</option>
<option>OdeGearControl</option>
<option>CppAD_vector</option>
<option>thread_alloc</option>
<option>index_sort</option>
<option>BenderQuad</option>
<option>opt_val_hes</option>
<option>LuRatio</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>LuRatio-&gt;</option>
<option>lu_ratio.cpp</option>
</select>
</td>
<td>lu_ratio.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>LuRatio: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 
# include &lt;cstdlib&gt;               // for rand function
# include &lt;cassert&gt;
# include &lt;cppad/cppad.hpp&gt;

namespace { // Begin empty namespace

CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; *NewFactor(
	size_t                           n ,
	const <a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) &amp;x , 
	bool                           &amp;ok ,
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(size_t)      &amp;ip , 
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(size_t)      &amp;jp )
{	using CppAD::AD;
	using CppAD::ADFun;
	size_t i, j, k;

	// values for independent and dependent variables
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) Y(n*n+1), X(n*n);

	// values for the LU factor
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) LU(n*n);

	// record the LU factorization corresponding to this value of x
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; Ratio;
	for(k = 0; k &lt; n*n; k++)
		X[k] = x[k];
	<a href="independent.xml" target="_top">Independent</a>(X);
	for(k = 0; k &lt; n*n; k++)
		LU[k] = X[k];
	CppAD::LuRatio(ip, jp, LU, Ratio);
	for(k = 0; k &lt; n*n; k++)
		Y[k] = LU[k];
	Y[n*n] = Ratio;

	// use a function pointer so can return ADFun object
	<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; *FunPtr = new <a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt;(X, Y);

	// check value of ratio during recording
	ok &amp;= (Ratio == 1.);

	// check that ip and jp are permutations of the indices 0, ... , n-1
	for(i = 0; i &lt; n; i++)
	{	ok &amp;= (ip[i] &lt; n);
		ok &amp;= (jp[i] &lt; n);
		for(j = 0; j &lt; n; j++)
		{	if( i != j )
			{	ok &amp;= (ip[i] != ip[j]);
				ok &amp;= (jp[i] != jp[j]);
			}
		}
	}
	return FunPtr; 
}
bool CheckLuFactor(
	size_t                           n  ,
	const <a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) &amp;x  ,
	const <a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) &amp;y  ,
	const <a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(size_t) &amp;ip ,
	const <a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(size_t) &amp;jp )
{	bool     ok = true;	

	double  sum;                          // element of L * U
	double  pij;                          // element of permuted x
	size_t  i, j, k;                      // temporary indices

	// L and U factors
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double)  L(n*n), U(n*n);

	// Extract L from LU factorization
	for(i = 0; i &lt; n; i++)
	{	// elements along and below the diagonal
		for(j = 0; j &lt;= i; j++)
			L[i * n + j] = y[ ip[i] * n + jp[j] ];
		// elements above the diagonal
		for(j = i+1; j &lt; n; j++)
			L[i * n + j] = 0.;
	}

	// Extract U from LU factorization
	for(i = 0; i &lt; n; i++)
	{	// elements below the diagonal
		for(j = 0; j &lt; i; j++)
			U[i * n + j] = 0.;
		// elements along the diagonal
		U[i * n + i] = 1.;
		// elements above the diagonal
		for(j = i+1; j &lt; n; j++)
			U[i * n + j] = y[ ip[i] * n + jp[j] ];
	}

	// Compute L * U 
	for(i = 0; i &lt; n; i++)
	{	for(j = 0; j &lt; n; j++)
		{	// compute element (i,j) entry in L * U
			sum = 0.;
			for(k = 0; k &lt; n; k++)
				sum += L[i * n + k] * U[k * n + j];
			// element (i,j) in permuted version of A
			pij  = x[ ip[i] * n + jp[j] ];
			// compare
			ok  &amp;= CppAD::<a href="nearequal.xml" target="_top">NearEqual</a>(pij, sum, 1e-10, 1e-10);
		}
	}
	return ok;
}

} // end Empty namespace

bool LuRatio(void)
{	bool  ok = true;

	size_t  n = 2; // number rows in A 
	double  ratio;

	// values for independent and dependent variables
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double)  x(n*n), y(n*n+1);

	// pivot vectors
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(size_t) ip(n), jp(n);

	// set x equal to the identity matrix
	x[0] = 1.; x[1] = 0;
	x[2] = 0.; x[3] = 1.;

	// create a fnction object corresponding to this value of x
	CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; *FunPtr = NewFactor(n, x, ok, ip, jp);

	// use function object to factor matrix
	y     = FunPtr-&gt;Forward(0, x);
	ratio = y[n*n];
	ok   &amp;= (ratio == 1.);
	ok   &amp;= CheckLuFactor(n, x, y, ip, jp);

	// set x so that the pivot ratio will be infinite
	x[0] = 0. ; x[1] = 1.;
	x[2] = 1. ; x[3] = 0.;

	// try to use old function pointer to factor matrix
	y     = FunPtr-&gt;Forward(0, x);
	ratio = y[n*n];

	// check to see if we need to refactor matrix
	ok &amp;= (ratio &gt; 10.);
	if( ratio &gt; 10. )
	{	delete FunPtr; // to avoid a memory leak	
		FunPtr = NewFactor(n, x, ok, ip, jp);
	}

	//  now we can use the function object to factor matrix
	y     = FunPtr-&gt;Forward(0, x);
	ratio = y[n*n];
	ok    &amp;= (ratio == 1.);
	ok    &amp;= CheckLuFactor(n, x, y, ip, jp);

	delete FunPtr;  // avoid memory leak
	return ok;
}
</pre>

</font></code>


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

</body>
</html>
