<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>AD Absolute Value Function: Example and Test</title>
<meta name="description" id="description" content="AD Absolute Value Function: Example and Test"/>
<meta name="keywords" id="keywords" content=" abs example fabs 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='_abs.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="abs.xml" target="_top">Prev</a>
</td><td><a href="sign.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>ADValued</option>
<option>MathOther</option>
<option>abs</option>
<option>abs.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(this)'>
<option>ADValued-&gt;</option>
<option>Arithmetic</option>
<option>std_math_ad</option>
<option>MathOther</option>
<option>CondExp</option>
<option>Discrete</option>
<option>atomic</option>
</select>
</td>
<td>
<select onchange='choose_down2(this)'>
<option>MathOther-&gt;</option>
<option>abs</option>
<option>sign</option>
<option>atan2</option>
<option>erf</option>
<option>pow</option>
<option>limits</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>abs-&gt;</option>
<option>abs.cpp</option>
</select>
</td>
<td>abs.cpp</td>
<td>Headings</td>
</tr></table><br/>
<center><b><big><big>AD Absolute Value Function: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 

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

bool abs(void)
{	bool ok = true;

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

	// domain space vector
	size_t n = 1;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) x(n);
	x[0]     = 0.;

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

	// range space vector
	size_t m = 6;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) y(m);
	y[0]     = abs(x[0] - 1.);
	y[1]     = abs(x[0]);
	y[2]     = abs(x[0] + 1.);
	//
	y[3]     = fabs(x[0] - 1.);
	y[4]     = fabs(x[0]);
	y[5]     = fabs(x[0] + 1.);

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

	// check values
	ok &amp;= (y[0] == 1.);
	ok &amp;= (y[1] == 0.);
	ok &amp;= (y[2] == 1.);
	//
	ok &amp;= (y[3] == 1.);
	ok &amp;= (y[4] == 0.);
	ok &amp;= (y[5] == 1.);

	// forward computation of partials w.r.t. a positive x[0] direction
	size_t p = 1;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) dx(n), dy(m);
	dx[0] = 1.;
	dy    = f.<a href="forward.xml" target="_top">Forward</a>(p, dx);
	ok  &amp;= (dy[0] == - dx[0]);
	ok  &amp;= (dy[1] ==   0.   ); // used to be (dy[1] == + dx[0]);
	ok  &amp;= (dy[2] == + dx[0]);
	//
	ok  &amp;= (dy[3] == - dx[0]);
	ok  &amp;= (dy[4] ==   0.   ); // used to be (dy[1] == + dx[0]);
	ok  &amp;= (dy[5] == + dx[0]);

	// forward computation of partials w.r.t. a negative x[0] direction
	dx[0] = -1.;
	dy    = f.<a href="forward.xml" target="_top">Forward</a>(p, dx);
	ok  &amp;= (dy[0] == - dx[0]);
	ok  &amp;= (dy[1] ==   0.   ); // used to be (dy[1] == - dx[0]);
	ok  &amp;= (dy[2] == + dx[0]);
	//
	ok  &amp;= (dy[3] == - dx[0]);
	ok  &amp;= (dy[4] ==   0.   ); // used to be (dy[1] == - dx[0]);
	ok  &amp;= (dy[5] == + dx[0]);

	// reverse computation of derivative of y[0] 
	p    = 1;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double)  w(m), dw(n);
	w[0] = 1.; w[1] = 0.; w[2] = 0.; w[3] = 0.; w[4] = 0.; w[5] = 0.;
	dw   = f.<a href="reverse.xml" target="_top">Reverse</a>(p, w);
	ok  &amp;= (dw[0] == -1.);

	// reverse computation of derivative of y[1] 
	w[0] = 0.; w[1] = 1.;
	dw   = f.<a href="reverse.xml" target="_top">Reverse</a>(p, w);
	ok  &amp;= (dw[0] == 0.);

	// reverse computation of derivative of y[5] 
	w[1] = 0.; w[5] = 1.;
	dw   = f.<a href="reverse.xml" target="_top">Reverse</a>(p, w);
	ok  &amp;= (dw[0] == 1.);

	// use a VecAD&lt;Base&gt;::reference object with abs and fabs
	CppAD::VecAD&lt;double&gt; v(1);
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; zero(0);
	v[zero]           = -1;
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; result = abs(v[zero]);
	ok    &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(result, 1., 1e-10, 1e-10);
	result = fabs(v[zero]);
	ok    &amp;= <a href="nearequal.xml" target="_top">NearEqual</a>(result, 1., 1e-10, 1e-10);

	return ok;
}

</pre>

</font></code>


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

</body>
</html>
