Prev Next number_skip.cpp Headings

@(@\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}} }@)@
Number of Variables That Can be Skipped: Example and Test
# include <cppad/cppad.hpp>
bool number_skip(void)
{     bool ok = true;
     using CppAD::AD;

     // independent variable vector
     CppAD::vector< AD<double> > ax(2);
     ax[0] = 0.;
     ax[1] = 1.;
     Independent(ax);

     // Use a conditional expression
     CppAD::vector< AD<double> > ay(1);

     // variable that gets optimized out
     AD<double> az = ax[0] * ax[0];


     // conditional expression
     ay[0] = CondExpLt(ax[0], ax[1], ax[0] + ax[1], ax[0] - ax[1]);

     // create function object F : x -> ay
     CppAD::ADFun<double> f;
     f.Dependent(ax, ay);

     // use zero order to evaluate F[ (3, 4) ]
     CppAD::vector<double>  x( f.Domain() );
     CppAD::vector<double>  y( f.Range() );
     x[0]    = 3.;
     x[1]    = 4.;
     y   = f.Forward(0, x);
     ok &= (y[0] == x[0] + x[1]);

     // before call to optimize
     ok &= f.number_skip() == 0;
     size_t n_var = f.size_var();

     // now optimize the operation sequence
     f.optimize();

     // after optimize, check forward mode result
     x[0]    = 4.;
     x[1]    = 3.;
     y   = f.Forward(0, x);
     ok &= (y[0] == x[0] - x[1]);

     // after optimize, check amount of optimization
     ok &= f.size_var() == n_var - 1;
     ok &= f.number_skip() == 1;

     return ok;
}

Input File: example/general/number_skip.cpp