<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>AD Vectors that Record Index Operations: Example and Test</title>
<meta name="description" id="description" content="AD Vectors that Record Index Operations: Example and Test"/>
<meta name="keywords" id="keywords" content=" Vecad example test vec_ad.cpp "/>
<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='_vec_ad.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="vecad.xml" target="_top">Prev</a>
</td><td><a href="base_require.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>VecAD</option>
<option>vec_ad.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>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_down1(this)'>
<option>VecAD-&gt;</option>
<option>vec_ad.cpp</option>
</select>
</td>
<td>vec_ad.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>AD Vectors that Record Index Operations: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 

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

namespace {
	// return the vector x that solves the following linear system 
	//	a[0] * x[0] + a[1] * x[1] = b[0]
	//	a[2] * x[0] + a[3] * x[1] = b[1]
	// in a way that will record pivot operations on the <a href="ad.xml" target="_top">AD</a>&lt;double&gt; tape
	typedef <a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(CppAD::<a href="ad.xml" target="_top">AD</a>&lt;double&gt;) Vector;
	Vector Solve(const Vector &amp;a , const Vector &amp;b)
	{	using namespace CppAD;
		assert(a.size() == 4 &amp;&amp; b.size() == 2);	

		// copy the vector b into the VecAD object B
		VecAD&lt;double&gt; B(2); 
		<a href="ad.xml" target="_top">AD</a>&lt;double&gt;    u;
		for(u = 0; u &lt; 2; u += 1.)
			B[u] = b[ Integer(u) ];

		// copy the matrix a into the VecAD object A
		VecAD&lt;double&gt; A(4); 
		for(u = 0; u &lt; 4; u += 1.)
			A[u] = a [ Integer(u) ];

		// tape AD operation sequence that determines the row of A
		// with maximum absolute element in column zero
		<a href="ad.xml" target="_top">AD</a>&lt;double&gt; zero(0), one(1);
		<a href="ad.xml" target="_top">AD</a>&lt;double&gt; rmax = CondExpGt(abs(a[0]), abs(a[2]), zero, one);

		// divide row rmax by A(rmax, 0)
		A[rmax * 2 + 1]  = A[rmax * 2 + 1] / A[rmax * 2 + 0];
		B[rmax]          = B[rmax]         / A[rmax * 2 + 0];
		A[rmax * 2 + 0]  = one;

		// subtract A(other,0) times row A(rmax, *) from row A(other,*)
		<a href="ad.xml" target="_top">AD</a>&lt;double&gt; other   = one - rmax;
		A[other * 2 + 1]   = A[other * 2 + 1]
		                   - A[other * 2 + 0] * A[rmax * 2 + 1];
		B[other]           = B[other]
		                   - A[other * 2 + 0] * B[rmax];
		A[other * 2 + 0] = zero;

		// back substitute to compute the solution vector x.
		// Note that the columns of A correspond to rows of x.
		// Also note that A[rmax * 2 + 0] is equal to one.
		<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) x(2);
		x[1] = B[other] / A[other * 2 + 1];
		x[0] = B[rmax] - A[rmax * 2 + 1] * x[1];

		return x;
	}
}

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

	// domain space vector
	size_t n = 4;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double)       x(n);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) X(n);
	// 2 * identity matrix (rmax in Solve will be 0)
	X[0] = x[0] = 2.; X[1] = x[1] = 0.;  
	X[2] = x[2] = 0.; X[3] = x[3] = 2.; 

	// declare independent variables and start tape recording
	CppAD::<a href="independent.xml" target="_top">Independent</a>(X);

	// define the vector b
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double)       b(2);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) B(2);
	B[0] = b[0] = 0.;
	B[1] = b[1] = 1.;

	// range space vector solves X * Y = b
	size_t m = 2;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) Y(m);
	Y = Solve(X, B);

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

	// By Cramer's rule:
	// y[0] = [ b[0] * x[3] - x[1] * b[1] ] / [ x[0] * x[3] - x[1] * x[2] ]
	// y[1] = [ x[0] * b[1] - b[0] * x[2] ] / [ x[0] * x[3] - x[1] * x[2] ]
	
	double den   = x[0] * x[3] - x[1] * x[2];
	double dsq   = den * den;
	double num0  = b[0] * x[3] - x[1] * b[1];
	double num1  = x[0] * b[1] - b[0] * x[2];

	// check value 
	ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(Y[0] , num0 / den,  1e-10 , 1e-10);
	ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(Y[1] , num1 / den,  1e-10 , 1e-10);

	// forward computation of partials w.r.t. x[0]
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dx(n);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dy(m);
	dx[0] = 1.; dx[1] = 0.;
	dx[2] = 0.; dx[3] = 0.;
	dy    = f.<a href="forward.xml" target="_top">Forward</a>(1, dx);
	ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[0], 0.         - num0 * x[3] / dsq, 1e-10, 1e-10);
	ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[1], b[1] / den - num1 * x[3] / dsq, 1e-10, 1e-10);

	// compute the solution for a new x matrix such that pivioting
	// on the original rmax row would divide by zero
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) y(m);
	x[0] = 0.; x[1] = 2.;
	x[2] = 2.; x[3] = 0.;

	// new values for Cramer's rule
	den   = x[0] * x[3] - x[1] * x[2];
	dsq   = den * den;
	num0  = b[0] * x[3] - x[1] * b[1];
	num1  = x[0] * b[1] - b[0] * x[2];

	// check values
	y    = f.<a href="forward.xml" target="_top">Forward</a>(0, x);
	ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(y[0] , num0 / den,  1e-10 , 1e-10);
	ok &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(y[1] , num1 / den,  1e-10 , 1e-10);
	
	// forward computation of partials w.r.t. x[1]
	dx[0] = 0.; dx[1] = 1.;
	dx[2] = 0.; dx[3] = 0.;
	dy    = f.<a href="forward.xml" target="_top">Forward</a>(1, dx);
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[0],-b[1] / den + num0 * x[2] / dsq, 1e-10, 1e-10);
	ok   &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dy[1], 0.         + num1 * x[2] / dsq, 1e-10, 1e-10);

	// reverse computation of derivative of y[0] w.r.t x
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) w(m);
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dw(n);
	w[0] = 1.; w[1] = 0.;
	dw   = f.<a href="reverse.xml" target="_top">Reverse</a>(1, w);
	ok  &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[0], 0.         - num0 * x[3] / dsq, 1e-10, 1e-10);
	ok  &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[1],-b[1] / den + num0 * x[2] / dsq, 1e-10, 1e-10);
	ok  &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[2], 0.         + num0 * x[1] / dsq, 1e-10, 1e-10);
	ok  &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(dw[3], b[0] / den - num0 * x[0] / dsq, 1e-10, 1e-10);

	return ok;
}

</pre>

</font></code>


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

</body>
</html>
