cppad
  • Table of Contents
  • user_guide
    • Install
    • Theory
    • AD
      • ad_ctor
      • ad_assign
      • Convert
        • Value
        • Integer
        • ad_to_string
        • ad_input
        • ad_output
        • PrintFor
          • print_for_cout.cpp
          • print_for_string.cpp
        • Var2Par
      • ADValued
      • bool_valued
      • VecAD
      • base_require
    • ADFun
    • preprocessor
    • multi_thread
    • utility
    • ipopt_solve
    • Example
    • speed
  • appendix
  • Index
cppad
  • user_guide
  • AD
  • Convert
  • PrintFor
  • print_for_string.cpp
  • View page source

\(\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}} }\)

print_for_string.cpp

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 recording
      if( u <= 0 )
         s_out << "check_log: u = " << u << " which is <= 0\n";

      // check double value during zero order forward calculation
      PrintFor(u, "check_log: u = ", u , " which is <= 0\n");

      return log(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 generated
   CPPAD_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;
}
Previous Next

© Copyright .

Built with Sphinx using a theme provided by Read the Docs.