Prev Next fun_optimize_xam.cpp Headings

@(@\newcommand{\B}[1]{ {\bf #1} } \newcommand{\R}[1]{ {\rm #1} }@)@
C++: Optimize an d_fun: Example and Test
# include <cstdio>
# include <cppad/py/cppad_py.hpp>

bool fun_optimize_xam(void) {
     using cppad_py::a_double;
     using cppad_py::vec_double;
     using cppad_py::vec_a_double;
     using cppad_py::d_fun;
     //
     // initialize return variable
     bool ok = true;
     //------------------------------------------------------------------------
     int n_ind = 1; // number of independent variables
     int n_dep = 1; // number of dependent variables
     int n_var = 1; // phantom variable at address 0
     int n_op = 1;  // special operator at beginning
     //
     // dimension some vectors
     vec_double x(n_ind);
     vec_a_double ay(n_dep);
     //
     // independent variables
     x[0] = 1.0;
     vec_a_double ax = cppad_py::independent(x);
     n_var = n_var + n_ind; // one for each indpendent
     n_op = n_op + n_ind;
     //
     // accumulate summation
     a_double ax0 = ax[0];
     a_double csum(0.0);
     csum = ax0 + ax0 + ax0 + ax0;
     n_var = n_var + 3; // one per + operator
     n_op = n_op + 3;
     //
     // define f(x) = y_0 = csum
     ay[0] = csum;
     d_fun f(ax, ay);
     n_op = n_op + 1; // speical operator at end
     //
     // check number of variables and operators
     ok = ok && f.size_var() == n_var;
     ok = ok && f.size_op() == n_op;
     //
     // optimize
     f.optimize();
     //
     // number of variables and operators has decreased by two
     ok = ok && f.size_var() == n_var-2;
     ok = ok && f.size_op() == n_op-2;
     //
     return( ok  );
}

Input File: lib/example/cplusplus/fun_optimize_xam.cpp