1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
<?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-></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-></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-></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-></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-></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 <cppad/cppad.hpp>
namespace {
using std::endl;
using CppAD::AD;
// use of PrintFor to check for invalid function arguments
<a href="ad.xml" target="_top">AD</a><double> check_log(const <a href="ad.xml" target="_top">AD</a><double>& y, std::ostream& s_out)
{ // check <a href="ad.xml" target="_top">AD</a><double> value during recording
if( y <= 0 )
s_out << "check_log: y == " << y << " which is <= 0\n";
// check double value during zero order forward calculation
PrintFor(y, "check_log: y == ", y , " which is <= 0\n");
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<double>) 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><double> var = 2. - ax[0];
<a href="ad.xml" target="_top">AD</a><double> log_var = check_log(var, stream_out);
ok &= stream_out.str() == "";
// dependent variable vector
size_t m = 1;
<a href="testvector.xml" target="_top">CPPAD_TESTVECTOR</a>(AD<double>) ay(m);
ay[0] = log_var;
// define f: x -> y and stop tape recording
CppAD::<a href="funconstruct.xml" target="_top">ADFun</a><double> 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 &= stream_out.str() == "check_log: y == 0 which is <= 0\n";
return ok;
}
</pre>
</font></code>
<hr/>Input File: example/print_for.cpp
</body>
</html>
|