@(@\newcommand{\W}[1]{ \; #1 \; }
\newcommand{\R}[1]{ {\rm #1} }
\newcommand{\B}[1]{ {\bf #1} }
\newcommand{\D}[2]{ \frac{\partial #1}{\partial #2} }
\newcommand{\DD}[3]{ \frac{\partial^2 #1}{\partial #2 \partial #3} }
\newcommand{\Dpow}[2]{ \frac{\partial^{#1}}{\partial {#2}^{#1}} }
\newcommand{\dpow}[2]{ \frac{ {\rm d}^{#1}}{{\rm d}\, {#2}^{#1}} }@)@This is cppad-20221105 documentation. Here is a link to its
current documentation
.
Print During Zero Order Forward Mode: Example and Test
# include <cppad/cppad.hpp>
namespace {
using std::endl;
using CppAD::AD;
// use of PrintFor to check for invalid function arguments
AD<double> check_log(const AD<double>& u, std::ostream& s_out)
{ // check AD<double> value during recordingif( u <= 0 )
s_out << "check_log: u = " << u << " which is <= 0\n";
// check double value during zero order forward calculationPrintFor(u, "check_log: u = ", u , " which is <= 0\n");
returnlog(u);
}
}
bool print_for(void)
{ bool ok = true;
using CppAD::PrintFor;
std::stringstream stream_out;
double eps99 = 99.0 * std::numeric_limits<double>::epsilon();
// independent variable vector
size_t np = 1;
size_t nx = 1;
size_t ny = 1;
CPPAD_TESTVECTOR(AD<double>) ap(np), ax(nx), ay(ny);
ap[0] = 1.0;
ax[0] = 2.0;
Independent(ax, ap);
//// define f(x, p) = log(p) + log(x)
ay[0] = check_log(ap[0], stream_out) + check_log(ax[0], stream_out);
CppAD::ADFun<double> f(ax, ay);
//// zero order forward// both x and p are positive so no output generatedCPPAD_TESTVECTOR(double) p(np), x(nx), y(ny);
p[0] = 1.0;
x[0] = 2.0;
f.new_dynamic(p);
f.check_for_nan(false);
y = f.Forward(0, x, stream_out);
ok &= stream_out.str() == "";
ok &= CppAD::NearEqual(y[0], std::log(p[0]) + std::log(x[0]), eps99, eps99);
//// zero order forward// p is negative so output generated
p[0] = -1.0;
x[0] = 2.0;
f.new_dynamic(p);
f.check_for_nan(false);
y = f.Forward(0, x, stream_out);
ok &= stream_out.str() == "check_log: u = -1 which is <= 0\n";
ok &= std::isnan(y[0]);
//return ok;
}