<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Lu Factor and Solve With Recorded Pivoting: Example and Test</title>
<meta name="description" id="description" content="Lu Factor and Solve With Recorded Pivoting: Example and Test"/>
<meta name="keywords" id="keywords" content=" Lu record pivot example 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_vec_ad_ok.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="lu_vec_ad.cpp.xml" target="_top">Prev</a>
</td><td><a href="listallexamples.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>Example</option>
<option>ExampleUtility</option>
<option>lu_vec_ad.cpp</option>
<option>lu_vec_ad_ok.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>Example-&gt;</option>
<option>General</option>
<option>ExampleUtility</option>
<option>ListAllExamples</option>
<option>testvector</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>ExampleUtility-&gt;</option>
<option>example.cpp</option>
<option>speed_example.cpp</option>
<option>lu_vec_ad.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>lu_vec_ad.cpp-&gt;</option>
<option>lu_vec_ad_ok.cpp</option>
</select>
</td>
<td>lu_vec_ad_ok.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Lu Factor and Solve With Recorded Pivoting: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 

# include &lt;cppad/cppad.hpp&gt;
# include &quot;lu_vec_ad.hpp&quot;
# include &lt;cppad/speed/det_by_minor.hpp&gt;

bool LuVecADOk(void)
{	bool  ok = true;

	using namespace CppAD;
	typedef <a href="ad.xml" target="_top">AD</a>&lt;double&gt; ADdouble;

	size_t              n = 3;
	size_t              m = 2;
	double a1[] = {
		3., 0., 0., // (1,1) is first  pivot
		1., 2., 1., // (2,2) is second pivot
		1., 0., .5  // (3,3) is third  pivot
	};
	double a2[] = {
		1., 2., 1., // (1,2) is second pivot
		3., 0., 0., // (2,1) is first  pivot
		1., 0., .5  // (3,3) is third  pivot
	};
	double rhs[] = {
		1., 3.,
		2., 2.,
		3., 1.
	};

	VecAD&lt;double&gt;       Copy    (n * n);
	VecAD&lt;double&gt;       Rhs     (n * m);
	VecAD&lt;double&gt;       Result  (n * m);
	ADdouble            logdet;
	ADdouble            signdet;

	// routine for checking determinants using expansion by minors
	det_by_minor&lt;ADdouble&gt; Det(n);

	// matrix we are computing the determinant of
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(ADdouble) A(n * n);

	// dependent variable values
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(ADdouble) Y(1 + n * m);

	size_t  i;
	size_t  j;
	size_t  k;

	// Original matrix
	for(i = 0; i &lt; n * n; i++)
		A[i] = a1[i];

	// right hand side
	for(j = 0; j &lt; n; j++)
		for(k = 0; k &lt; m; k++)
			Rhs[ j * m + k ] = rhs[ j * m + k ];
		
	// Declare independent variables
	<a href="independent.xml" target="_top">Independent</a>(A);

	// Copy the matrix
	ADdouble index(0);
	for(i = 0; i &lt; n*n; i++)
	{	Copy[index] = A[i];
		index += 1.;
	}

	// Solve the equation
	signdet = LuVecAD(n, m, Copy, Rhs, Result, logdet);

	// Result is the first n * m dependent variables
	index = 0.;
	for(i = 0; i &lt; n * m; i++)
	{	Y[i] = Result[index];
		index += 1.;
	}

	// Determinant is last component of the solution
	Y[ n * m ] = signdet * exp( logdet );

	// construct f: A -&gt; Y
	<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; f(A, Y);

	// check determinant using minors routine
	ADdouble determinant = Det( A );
	ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(Y[n * m], determinant, 1e-10, 1e-10);


	// Check solution of Rhs = A * Result
	double sum;
	for(k = 0; k &lt; m; k++)
	{	for(i = 0; i &lt; n; i++)
		{	sum = 0.;
			for(j = 0; j &lt; n; j++)
				sum += a1[i * n + j] * Value( Y[j * m + k] );
			ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>( rhs[i * m + k], sum, 1e-10, 1e-10 );
		}
	}
 
 	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) y2(1 + n * m);
 	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) A2(n * n);
 	for(i = 0; i &lt; n * n; i++)
 		A[i] = A2[i] = a2[i];

 
 	y2          = f.<a href="forward.xml" target="_top">Forward</a>(0, A2);
 	determinant = Det(A);
 	ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(y2[ n * m], Value(determinant), 1e-10, 1e-10);

	// Check solution of Rhs = A2 * Result
	for(k = 0; k &lt; m; k++)
	{	for(i = 0; i &lt; n; i++)
		{	sum = 0.;
			for(j = 0; j &lt; n; j++)
				sum += a2[i * n + j] * y2[j * m + k];
			ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>( rhs[i * m + k], sum, 1e-10, 1e-10 );
		}
	}

	return ok;
}

</pre>

</font></code>


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

</body>
</html>
