celestialgod

Rcpp external pointer

Mar 20th, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. // [[Rcpp::depends(RcppArmadillo)]]
  2. #include <RcppArmadillo.h>
  3.  
  4. arma::vec f(const arma::vec& x){
  5.   return sin(x);
  6. }
  7.  
  8. typedef arma::vec (*funcPtr)(const arma::vec& x);
  9.  
  10. // [[Rcpp::export]]
  11. Rcpp::XPtr<funcPtr> ptr2f(){
  12.   return Rcpp::XPtr<funcPtr>(new funcPtr(&f));
  13. }
  14.  
  15. // [[Rcpp::export]]
  16. arma::vec callXPtr(const arma::vec& x, Rcpp::XPtr<funcPtr> xpfun) {
  17.   funcPtr func = *xpfun;
  18.   arma::vec y = func(x);
  19.   return (y);
  20. }
  21.  
  22. /*** R
  23.   f <- ptr2f()
  24.   callXPtr(1:10, f)
  25.  */
Advertisement
Add Comment
Please, Sign In to add comment