Transform a boost::array into NumericVector in Rcpp

前端 未结 2 1112
清歌不尽
清歌不尽 2021-01-25 20:23

In my C++ script (run in R using Rcpp), I defined :

typedef boost::array< double ,3 > state_type;

Now, I want to create a function to tra

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-25 20:50

    If you want to have seamless integration you will have to extend Rcpp::as(obj) and Rcpp::wrap(obj). How to do this is covered in the vignette Extending Rcpp. However, there is also an excellent entry in the Rcpp gallery that already covers a very similar case: Converting to and from boost::numeric::ublas::vector.

    If you are content with only using state_type and not the general template, then you can use something like this:

    #include 
    // [[Rcpp::depends(BH)]]
    #include 
    typedef boost::array< double ,3 > state_type;
    
    namespace Rcpp {
    // non-intrusive extension via template specialisation
    template <> state_type as(SEXP s);
    // non-intrusive extension via template specialisation
    template <> SEXP wrap(const state_type &s);
    }
    
    #include 
    
    // define template specialisations for as and wrap
    namespace Rcpp {
    template <> state_type as(SEXP stsexp) {
      Rcpp::NumericVector st(stsexp);
      state_type result;
      if (st.size() != result.size()) Rcpp::stop("Incompatible length!");
      std::copy(st.begin(), st.end(), result.begin());
      return result;
    }
    
    template <> SEXP wrap(const state_type &st) {
      Rcpp::NumericVector result(st.size());
      std::copy(st.begin(), st.end(), result.begin());
      return Rcpp::wrap(result);
    }
    }
    
    // make use of the new possibilities
    // [[Rcpp::export]]
    state_type alterStateType(state_type &st) {
      for (size_t i = 0; i < st.size(); ++i) {
        st[i] = i * st[i];
      }
      return st;
    }
    
    /*** R
    alterStateType(1:3)
    */
    

提交回复
热议问题