[Rcpp-devel] Wrapping uBlas vectors into Rcpp Vectors.

Romain Francois romain at r-enthusiasts.com
Wed Jun 1 12:44:10 CEST 2011


I've not used uBlas, but what you are trying to do is quite similar to 
what we do in RcppArmadillo.

You can probably manage to guess the output type from the input type, so 
you only have to parameterise your template on the input type. something 
like (untested) :

template <typename T>
Rcpp::Vector< Rcpp::traits::r_sexptype_traits<T>::rtype >
ublas2rcpp( const vector<T>& x ){
	return Rcpp::Vector< r_sexptype_traits<T>::rtype >(
		x.begin(), x.end()
	)  ;

This way you don't have to specify template parameter when you call 
ublas2rcpp because the compiler is smart enough.

Nicer than this would be to implement wrap and as for ublas vectors, the 
way to go is documented in the Rcpp-extended vignettes, with examples 
implementations in RcppArmadillo and RcppGSL.

As a side note, you don't want to use push_back on Rcpp types, because 
it creates a new vector each time, so this is HUGE memory waste.

Now, this could get much smarter as ublas has vector expressions, 
similar to armadillo, so I suppose someone could write something like 
RcppUBlas with nice goodies. This is not me, at least not now ;-)


Le 01/06/11 12:24, Cedric Ginestet a écrit :
> Dear Rcpp experts,
> I have started to use the uBlas library, and I am trying to ensure that
> I can pass from uBlas vectors to Rcpp vectors relatively easily. So far,
> I have tried the following templated function:
> ///////////////////////////////////////////////////////////////////////
> using namespace Rcpp;
> using namespace boost::numeric::ublas;
> template <class T1, class T2>
> T1 ublas2rcpp(T2& uVec){
> T1 rcppVec;
> for(int i=0; i<uVec.size(); ++i) rcppVec.push_back(uVec(i));
> return rcppVec;
> }//ublas2rcpp.
> ////////
> SEXP foo(vector<int> &y){
> ...
> IntegerVector rcppY=ublas2rcpp<IntegerVector,vector<int> >(y);
> ....
> return rcppY;
> }
> ///////////////////////////////////////////////////////////////////////
> I have got two questions:
> a. This templated function doesn't work. It compiles fine, but hangs
> when I try to run it in R. What do you think is faulty in the codes above?
> b. Is there a better way to wrap uBlas vectors into Rcpp ones? Is that
> something that you are planning to implement (or have already
> implemented) within the Rcpp suite?
> Thank you very much for your help,
> --
> Cedric Ginestet, PhD
> Centre for Neuroimaging Sciences (L3.04)
> NIHR Biomedical Research Centre
> Department of Neuroimaging
> Institute of Psychiatry, Box P089
> King's College London
> De Crespigny Park
> London
> SE5 8AF
> http://arxiv.org/find/q-bio/1/au:+Ginestet_C/0/1/0/all/0/1

Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
|- http://bit.ly/hdKhCy : Rcpp article in JSS
|- http://bit.ly/elZJRJ : Montpellier Comedie Club - Avril 2011
`- http://bit.ly/fhqbRC : Rcpp workshop in Chicago on April 28th

More information about the Rcpp-devel mailing list