Prev Next more_cpp

@(@\newcommand{\B}[1]{ {\bf #1} } \newcommand{\R}[1]{ {\rm #1} }@)@
Steps To Add More C++ Functions

Purpose
This section outlines the steps for adding more CppAD functionality to cppad_py. This is done by example showing how the cpp_fun_new_dynamic was added. This example case was chosen because it required both changing one C++ function, cpp_independent , and adding a new C++ function, cpp_fun_new_dynamic .

Include File

independent
The include file include/cppad/py/fun.hpp was edited to add the following prototype:
     CPPAD_PY_LIB_PUBLIC
     std::vector<a_double> independent(
          const std::vector<double>& x, const std::vector<double>& dynamic
     );
The independent function is not part of the d_fun or a_fun class, but calling it is the first step in creating these objects. This is why its prototype is in the fun.hpp file.

new_dynamic
The include file include/cppad/py/fun.hpp was edited to add the following member function to d_fun:

    void new_dynamic(const std::vector<double>& dynamic);
The following member function was added to a_fun:

    void new_dynamic(const std::vector<a_double>& adynamic);

Documentation

independent
The C++ file lib/cplusplus/fun.cpp was edited to add the following syntax documentation:
     
a_both = cppad_py::independent(xdynamic)
and the corresponding return value was documented; see a_both .

new_dynamic
The cpp_fun_new_dynamic documentation was added.

Example
The corresponding example file was added to the documentation, below the cpp_independent section, using the OMhelp commands: $children% lib/example/cplusplus/fun_dynamic_xam.cpp %$$ In addition, a reference to this example was added under the example heading in the independent documentation.

Implementation

independent
The following function was added to the lib/cplusplus/fun.cpp file:
std::vector<a_double> independent(
     const std::vector<double>& x       ,
     const std::vector<double>& dynamic )
{    using CppAD::AD;
     size_t nx = x.size();
     size_t nd = dynamic.size();
     CppAD::vector< AD<double> > ax(nx), adynamic(nd);
     for(size_t j = 0; j < nx; j++)
          ax[j] = x[j];
     for(size_t j = 0; j < nd; j++)
          adynamic[j] = dynamic[j];
     size_t abort_op_index = 0;
     size_t record_compare = false;
     CppAD::Independent(ax, abort_op_index, record_compare, adynamic);
     std::vector<a_double> a_both(nx + nd);
     // use a_double( *AD<double> ) constructor in these assignment loops
     for(size_t j = 0; j < nx; j++)
          a_both[j] =  &ax[j] ;
     for(size_t j = 0; j < nd; j++)
          a_both[nx + j] =  &adynamic[j] ;
     return a_both;
}

new_dynamic
The following function was added to the lib/cplusplus/fun.cpp file:
void d_fun::new_dynamic(const std::vector<double>& dynamic)
{    if( dynamic.size() != ptr_->size_dyn_ind() )
          error_message("cppad_py::d_fun::new_dynamic dynamic.size() error");
     ptr_->new_dynamic(dynamic);
     return;
}
void a_fun::new_dynamic(const std::vector<a_double>& adynamic)
{    if( adynamic.size() != a_ptr_->size_dyn_ind() )
          error_message("cppad_py::a_fun::jacobian adynamic.size() error");
     std::vector< CppAD::AD<double> > au = vec2cppad_double(adynamic);
     a_ptr_->new_dynamic(au);
     return;
}
A similar function was added to the a_fun class.

Example
The file lib/example/cplusplus/fun_dynamic_xam.cpp was added with the following contents: fun_dynamic_xam.cpp . In addition, in the file lib/example/cplusplus/check_all.cpp, % extern bool fun_optimize_xam(void); % was added to the external declarations and % ok &= Run( fun_optimize_xam, "fun_optimize_xam" ); % was added to the main program.

Testing
You must do a git add for all of the new files before running bin/check_all.sh. After all the C++ changes above were implemented, bin/check_all.sh was run and the changes were made until the warnings and errors were fixed. The command
 
     grep 'fun_dynamic_xam' check_all.log
was used to make sure that the new C++ example / test was run. Note that if a particular step in bin/check_all.sh is failing, you can just re-run that step to see if a particular fix works. Once the C++ tests were working, the changes where checked into using git.
Input File: lib/cplusplus/more_cpp.omh