Antonio Coppola edited C++ Functions.tex  over 9 years ago

Commit id: 5d5a076d08851a1622c5fab1a1303be56df4ef5a

deletions | additions      

       

optim(c(-1.2, 1), objective.R, ..., method = "L-BFGS-B") 366.45 382.2585 415.9205 485.3085 20537.39 100  \end{lstlisting}  \subsection{Extra parameters Parameters  in C++ implementations} Implementations}  Much like in the R case, the passing of extra parameters with C++ implementations of the objective and gradient is achieved through the use of R environments. The following is an example replicating the Poisson regression using data from Golub et al. \cite{Golub_1999} with C++ function implementations. As before, we set up the objective and gradient as character strings. We include the extra environment argument and obtain data by evaluating it usign the Rcpp::Environment class. In order to perfom matrix operations, we use the RcppArmadillo library \cite{Eddelbuettel_2014}: 

\end{lstlisting}  Then we compile the functions and their pointer-assigners, taking care to map the functions' signatures correctly:  \begin{lstlisting}  likelihood.body <- '  typedef Rcpp::NumericVector (*funcPtr)(SEXP, SEXP);  return(XPtr(new funcPtr(&lhood)));  '  likelihood.body <- '  typedef Rcpp::NumericVector (*funcPtr)(SEXP, SEXP);  return(XPtr(new funcPtr(&grad)));  '  likelihood.CPP <- cxxfunction(signature(), body=likelihood.body,   inc=likelihood.inc, plugin="RcppArmadillo")  gradient.CPP <- cxxfunction(signature(), body=gradient.body,   inc=gradient.inc, plugin="RcppArmadillo")  \end{lstlisting}