<?xml version='1.0'?>
<html xmlns='http://www.w3.org/1999/xhtml'
      xmlns:math='http://www.w3.org/1998/Math/MathML'
>
<head>
<title>Print During Zero Order Forward Mode: Example and Test</title>
<meta name="description" id="description" content="Print During Zero Order Forward Mode: Example and Test"/>
<meta name="keywords" id="keywords" content=" print zero order forward "/>
<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='_print_for_string.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="print_for_cout.cpp.xml" target="_top">Prev</a>
</td><td><a href="var2par.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>Convert</option>
<option>PrintFor</option>
<option>print_for_string.cpp</option>
</select>
</td>
<td>
<select onchange='choose_down3(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_down2(this)'>
<option>Convert-&gt;</option>
<option>Value</option>
<option>Integer</option>
<option>ad_input</option>
<option>ad_output</option>
<option>PrintFor</option>
<option>Var2Par</option>
</select>
</td>
<td>
<select onchange='choose_down1(this)'>
<option>PrintFor-&gt;</option>
<option>print_for_cout.cpp</option>
<option>print_for_string.cpp</option>
</select>
</td>
<td>print_for_string.cpp</td>
<td>Headings</td>
</tr></table><br/>



<center><b><big><big>Print During Zero Order Forward Mode: Example and Test</big></big></b></center>
<code><font color="blue"><pre style='display:inline'> 
# include &lt;cppad/cppad.hpp&gt;

namespace { 
	using std::endl;
	using CppAD::AD;

	// use of PrintFor to check for invalid function arguments
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; check_log(const <a href="ad.xml" target="_top">AD</a>&lt;double&gt;&amp; y, std::ostream&amp; s_out)
	{	// check <a href="ad.xml" target="_top">AD</a>&lt;double&gt; value during recording
		if( y &lt;= 0 )
			s_out &lt;&lt; &quot;check_log: y == &quot; &lt;&lt; y &lt;&lt; &quot; which is &lt;= 0\n&quot;;

		// check double value during zero order forward calculation
		PrintFor(y, &quot;check_log: y == &quot;, y , &quot; which is &lt;= 0\n&quot;);

		return log(y);
	} 
}

bool print_for(void)
{	bool ok = true;
	using CppAD::PrintFor;
	std::stringstream stream_out;

	// independent variable vector
	size_t n = 1;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) ax(n);
	ax[0] = 1.;         // value of the independent variable during recording
	<a href="independent.xml" target="_top">Independent</a>(ax);

	// A log evaluations that is OK when x[0] = 1 but not when x[0] = 2.
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; var     = 2. - ax[0];
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; log_var = check_log(var, stream_out);
	ok &amp;= stream_out.str() == &quot;&quot;;

	// dependent variable vector 
	size_t m = 1;
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD&lt;double&gt;) ay(m);
	ay[0]    = log_var;

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

	// zero order forward with x[0] = 2 
	<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(double) x(n);
	x[0] = 2.;
	f.<a href="forward.xml" target="_top">Forward</a>(0, x, stream_out);	

	std::string string_out = stream_out.str();
	ok &amp;= stream_out.str() == &quot;check_log: y == 0 which is &lt;= 0\n&quot;;

	return ok;
}
</pre>

</font></code>


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

</body>
</html>
