Reputation: 669
I am using RcppEigen on R. I would like to take a double vector from R layer:
RcppExport SEXP testfunc (SEXP a) {
const Eigen::Map<Eigen::VectorXd> vecD(as<Eigen::Map<Eigen::VectorXd> >(a));
But, number is comming as integer, so I need to get it as integer like this :
RcppExport SEXP testfunc (SEXP a) {
const Eigen::Map<Eigen::VectorXi> vecD(as<Eigen::Map<Eigen::VectorXi> >(a));
So, I need to convert it to the double. Can I convert Eigen::VectorXi to Eigen::VectorXd ?
Upvotes: 0
Views: 1040
Reputation: 368509
Are you sure you are not getting confused calling with integer
from R when you meant numeric
aka double
or vice versa ? There is no reason not to have two functions, or to dispatch inside your function.
Eg consider the code here (using Rcpp 0.10.0 features)
#include <RcppEigen.h>
// [[Rcpp::depends(RcppEigen)]]
// [[Rcpp::export]]
double vecdSum(SEXP x) {
const Eigen::Map<Eigen::VectorXd>
vec(Rcpp::as<Eigen::Map<Eigen::VectorXd> >(x));
return vec.sum();
}
// [[Rcpp::export]]
int veciSum(SEXP x) {
const Eigen::Map<Eigen::VectorXi>
vec(Rcpp::as<Eigen::Map<Eigen::VectorXi> >(x));
return vec.sum();
}
which we can easily put to use via
R> sourceCpp('/tmp/vecsums.cpp') # Rcpp 0.10.0 adds this
R> veciSum(c(1L, 2L, 3L))
[1] 6
R> vecdSum(c(1, 2, 3))
[1] 6
R>
Upvotes: 2