Prev Next a_double_cond_assign_xam.cpp Headings

@(@\newcommand{\B}[1]{ {\bf #1} } \newcommand{\R}[1]{ {\rm #1} }@)@
C++: a_double Conditional Assignment: Example and Test
# include <cstdio>
# include <string>
# include <cppad/swig/cppad_swig.hpp>

bool a_double_cond_assign_xam(void) {
     using cppad_swig::a_double;
     using cppad_swig::vec_bool;
     using cppad_swig::vec_int;
     using cppad_swig::vec_double;
     using cppad_swig::vec_a_double;
     using cppad_swig::a_fun;
     using cppad_swig::sparse_rc;
     using cppad_swig::sparse_rcv;
     using cppad_swig::sparse_jac_work;
     using cppad_swig::sparse_hes_work;
     using std::string;
     //
     // initialize return variable
     bool ok = true;
     //------------------------------------------------------------------------
     int n_ind = 4;
     int n_dep = 1;
     //
     // create ax (value of independent variables does not matter)
     vec_double x = cppad_swig::vec_double(n_ind);
     x[0] = 0.0;
     x[1] = 1.0;
     x[2] = 2.0;
     x[3] = 3.0;
     vec_a_double ax = cppad_swig::independent(x);
     //
     // arguments to conditional assignment
     a_double left = ax[0];
     a_double right = ax[1];
     a_double if_true = ax[2];
     a_double if_false = ax[3];
     //
     // assignment
     a_double target = cppad_swig::a_double();
     target.cond_assign(
          "<",
          left,
          right,
          if_true,
          if_false
     );
     //
     // f(x) = taget
     vec_a_double ay = cppad_swig::vec_a_double(n_dep);
     ay[0] = target;
     a_fun af = cppad_swig::a_fun(ax, ay);
     //
     // assignment with different independent variable values
     x[0] = 9.0; // left
     x[1] = 8.0; // right
     x[2] = 7.0; // if_true
     x[3] = 6.0; // if_false
     int p = 0;
     vec_double y = af.forward(p, x);
     ok = ok && y[0] == 6.0;
     //
     return( ok  );
}

Input File: build/lib/example/cplusplus/a_double_cond_assign_xam.cpp