Codebase list cppad / upstream/2015.00.00.7 doc / sign.cpp.xml
upstream/2015.00.00.7

Tree @upstream/2015.00.00.7 (Download .tar.gz)

sign.cpp.xml @upstream/2015.00.00.7raw · history · blame

<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Sign Function: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Sign Function: Example and Test"/>
<meta name="keywords" id="keywords" content=" sign function: example and 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='_sign.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="sign.xml" target="_top">Prev</a>
</td><td><a href="atan2.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>sign</option>
<option>sign.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>sign-&gt;</option>
<option>sign.cpp</option>
</select>
</td>
<td>sign.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Sign Function: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 

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

bool sign(void)
{	bool ok = true;

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

	// create f: x -&gt; y where f(x) = sign(x)
	size_t n = 1;
	size_t m = 1;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) ax(n), ay(m);
	ax[0]     = 0.;
	CppAD::<a href="independent.xml" target="_top">Independent</a>(ax);
	ay[0]     = sign(ax[0]);
	CppAD::<a href="funconstruct.xml" target="_top">ADFun</a>&lt;double&gt; f(ax, ay);

	// check value during recording
	ok &amp;= (ay[0] == 0.);

	// use f(x) to evaluate the sign function and its derivatives
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) x(n), y(m), dx(n), dy(m), w(m), dw(n);
	dx[0] = 1.;
	w[0] = 1.; 
	//
	x[0]  = 2.;
	y     = f.<a href="forward.xml" target="_top">Forward</a>(0, x);
	ok   &amp;= (y[0] == 1.);
	dy    = f.<a href="forward.xml" target="_top">Forward</a>(1, dx);
	ok   &amp;= (dy[0] == 0.);
	dw   = f.<a href="reverse.xml" target="_top">Reverse</a>(1, w);
	ok  &amp;= (dw[0] == 0.);
	//
	x[0]  = 0.;
	y     = f.<a href="forward.xml" target="_top">Forward</a>(0, x);
	ok   &amp;= (y[0] == 0.);
	dy    = f.<a href="forward.xml" target="_top">Forward</a>(1, dx);
	ok   &amp;= (dy[0] == 0.);
	dw   = f.<a href="reverse.xml" target="_top">Reverse</a>(1, w);
	ok  &amp;= (dw[0] == 0.);
	//
	x[0]  = -2.;
	y     = f.<a href="forward.xml" target="_top">Forward</a>(0, x);
	ok   &amp;= (y[0] == -1.);
	dy    = f.<a href="forward.xml" target="_top">Forward</a>(1, dx);
	ok   &amp;= (dy[0] == 0.);
	dw   = f.<a href="reverse.xml" target="_top">Reverse</a>(1, w);
	ok  &amp;= (dw[0] == 0.);

	// use a VecAD&lt;Base&gt;::reference object with sign
	CppAD::VecAD&lt;double&gt; v(1);
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; zero(0);
	v[zero]           = 2.;
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; result = sign(v[zero]);
	ok   &amp;= (result == 1.);

	return ok;
}

</pre>

</font></code>


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

</body>
</html>