I'm working with R and I would like to use ublas from boost. Using some code I found from the below link, with corresponding code to follow, I'm able to return the ublas vector wrapped as an R-type. However I'd like to go the other way converting the R type to ublas. Right now I'm doing this via a loop. I create a ublas vector of the corresponding length and just assign everything with a loop. Or is a loop the best I can do, at least performance wise? My code is posted at the end.I'm fairly new to C++, I'm using it through the R C++ extension RCpp
http://lists.r-forge.r-project.org/pipermail/rcpp-devel/2011-June/002402.html
template <typename T>
Rcpp::Vector< Rcpp::traits::r_sexptype_traits<T>::rtype >
ublas2rcpp( const boost::numeric::ublas::vector<T>& x ){
return Rcpp::Vector< Rcpp::traits::r_sexptype_traits<T>::rtype >(
x.begin(), x.end()
) ;
}
/// here is my code
// so R can find the libraries
//[[Rcpp::depends(BH)]]
//[[Rcpp::plugins("cpp11")]]
#include <Rcpp.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <boost/numeric/odeint.hpp>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
using namespace std;
using namespace Rcpp;
using namespace boost::numeric::ublas;
//typedef std::vector< double > vector_type;
typedef boost::numeric::ublas::vector< double > vector_type;
typedef boost::numeric::ublas::matrix< double > matrix_type;
template <typename T> /// this need to be fixed up, hopefully working for now though
Rcpp::Vector< Rcpp::traits::r_sexptype_traits<T>::rtype >
ublas2rcpp( const boost::numeric::ublas::vector<T>& x ){
return Rcpp::Vector< Rcpp::traits::r_sexptype_traits<T>::rtype >(
x.begin(), x.end()
) ;
}
// [[Rcpp::export]]
NumericVector main(NumericVector x1)
{
int L =x1.length();
vector_type x(L , 0 ); // initialize the vector to all zero
for(int i=0;i<L;i++)
{
x(i) = x1(i);
}
return(ublas2rcpp(x));
}
Aucun commentaire:
Enregistrer un commentaire