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

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

print_for_cout.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>Printing During Forward Mode: Example and Test</title>
<meta http-equiv='Content-Type' content='text/html' charset='utf-8'/>
<meta name="description" id="description" content="Printing During Forward Mode: Example and Test"/>
<meta name="keywords" id="keywords" content=" printing during forward mode: example and test mode print running source code output "/>
<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_cout.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="printfor.xml" target="_top">Prev</a>
</td><td><a href="print_for_string.cpp.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_cout.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_cout.cpp</td>
<td>
<select onchange='choose_current0(this)'>
<option>Headings-&gt;</option>
<option>Running</option>
<option>Source Code</option>
<option>Output</option>
</select>
</td>
</tr></table><br/>



<center><b><big><big>Printing During Forward Mode: Example and Test</big></big></b></center>
<br/>
<b><big><a name="Running" id="Running">Running</a></big></b>
<br/>
To build this program and run its correctness test see <a href="cmake_check.xml" target="_top"><span style='white-space: nowrap'>cmake_check</span></a>
.

<br/>
<br/>
<b><big><a name="Source Code" id="Source Code">Source Code</a></big></b>

<code><font color='blue'><pre style='display:inline'> 
# include &lt;cppad/cppad.hpp&gt;

namespace { 
	using std::cout;
	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)
	{	// check during recording
		if( y &lt;= 0. )
			cout &lt;&lt; &quot;check_log: y = &quot; &lt;&lt; y &lt;&lt; &quot; is &lt;= 0&quot; &lt;&lt; endl;

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

		return log(y);
	} 
}

void print_for(void)
{	using CppAD::PrintFor;

	// 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.;
	<a href="independent.xml" target="_top">Independent</a>(ax);

	// print a VecAD&lt;double&gt;::reference object that is a parameter
	CppAD::VecAD&lt;double&gt; av(1);
	<a href="ad.xml" target="_top">AD</a>&lt;double&gt; Zero(0);
	av[Zero] = 0.;
	PrintFor(&quot;v[0] = &quot;, av[Zero]); 

	// Print a newline to separate this from previous output,
	// then print an <a href="ad.xml" target="_top">AD</a>&lt;double&gt; object that is a variable.
	PrintFor(&quot;\nv[0] + x[0] = &quot;, av[0] + ax[0]); 

	// A conditional print that will not generate output when x[0] = 2.
	PrintFor(ax[0], &quot;\n  2. + x[0] = &quot;,   2. + ax[0], &quot;\n&quot;);

	// A conditional print that will generate output when x[0] = 2.
	PrintFor(ax[0] - 2., &quot;\n  3. + x[0] = &quot;,   3. + ax[0], &quot;\n&quot;);

	// A log evaluations that will result in an error message 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);

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

	// 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.;

	cout &lt;&lt; &quot;v[0] = 0&quot; &lt;&lt; endl; 
	cout &lt;&lt; &quot;v[0] + x[0] = 2&quot; &lt;&lt; endl; 
	cout &lt;&lt; &quot;  3. + x[0] = 5&quot; &lt;&lt; endl; 
	cout &lt;&lt; &quot;check_log: y == 0 which is &lt;= 0&quot; &lt;&lt; endl;
	// ./makefile.am expects &quot;Test passes&quot; at beginning of next output line
	cout &lt;&lt; &quot;Test passes if four lines above repeat below:&quot; &lt;&lt; endl;
	f.<a href="forward.xml" target="_top">Forward</a>(0, x);	

	return;
}
int main(void)
{	bool ok = true;
	print_for();

	size_t thread;
	for(thread = 0; thread &lt; 2; thread++)
	{	ok &amp;= CppAD::omp_alloc::inuse(thread) == 0;
		ok &amp;= CppAD::omp_alloc::available(thread) == 0;
	}
	if( ! ok )
		return 1;
	return 0;
}
</pre></font></code>


<br/>
<br/>
<b><big><a name="Output" id="Output">Output</a></big></b>
<br/>
Executing the program above generates the following output:
<code><font color='blue'><pre style='display:inline'> 
	v[0] = 0
	v[0] + x[0] = 2
	Test passes if two lines above repeat below:
	v[0] = 0
	v[0] + x[0] = 2
</pre></font></code>


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

</body>
</html>