[Rcpp-devel] RcppArmadillo and sugar

Romain Francois romain at r-enthusiasts.com
Wed Aug 18 21:59:46 CEST 2010


After some discussion this morning with Conrad (who develops Armadillo), 
we've come up with a way that is not too intrusive on armadillo itself 
(it adds about 6 lines of code), yet opens a door for RcppArmadillo to 
adds its own constructors to the Mat template class.

I've added this one now :

template <int RTYPE, bool NA, typename VECTOR>
inline Mat( const Rcpp::VectorBase<RTYPE,NA,VECTOR>& X ) ;

Those of you who are familiar with how sugar is implemented (e.g. who 
have read the sugar vignette) will recognize the VectorBase class which 
is the magic template that makes all of sugar possible. Anyway, with 
this (rev. 2039), one can directly write code that makes an arma::mat 
directly out of a sugar expression.

Here is how it is tested in RcppArmadillo unit tests


test.armadillo.sugar.ctor <- function(){

	fx <- cxxfunction( signature(x= "numeric") , '
	NumericVector xx(x) ;
	arma::mat m = xx + xx ;
     return wrap( m + m ) ;

	', plugin = "RcppArmadillo" )
	checkEquals( fx(1:10),
		matrix( 4*(1:10), nrow = 10 ) ,
		msg = "Mat( sugar expression )" )
	
}

I need to take care of the special case of complex vectors though. Will 
do. But the key here is these two lines :


NumericVector xx(x) ;    // create a NumericVector from a SEXP
arma::mat m = xx + xx ;  // create a arma::mat from a sugar expression

Romain

Le 17/08/10 12:26, Romain Francois a écrit :
> Hello,
>
> After the last thread, I looked again at armadillo and came up with a
> way to make it aware of Rcpp sugar, so that we can create an armadillo
> matrix from an Rcpp sugar expression, without having to assign the
> expression into an Rcpp vector.
>
> For example :
>
> NumericVector xx(x) ;
> arma::mat m = forward( xx + xx ) ;
>
> But also for complex expressions : (expressions that make complex
> vectors ) :
>
> ComplexVector xx(x) ;
> arma::cx_mat m = forward( exp( xx ) ) ;
>
>
> It needs the svn version of both Rcpp and RcppArmadillo to work.
> It does not deal with dimensions at the moment, it just makes a arma
> matrix with one column, I'll deal with that later.
>
>
> The name forward might not be the right verb here, I was also thinking
> about "melt" so that we melt sugar into caramel, but I'd like to hear
> what others do with sugar.
>
>
>
>
> BTW, one thing this gives is the construct :
>
> arma::mat m = forward( NumericVector(x) ) ;
>
> which I believe will be more efficient than
>
> arma::mat m = Rcpp::as< arma::mat >( x ) ;
>
> because it makes less copies. However I have not yet found a way to make
> it "borrow" the memory as in the advanced constructor in armadillo.
>
>
> Romain
>


-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/bzoWrs : Rcpp svn revision 2000
|- http://bit.ly/b8VNE2 : Rcpp at LondonR, oct 5th
`- http://bit.ly/aAyra4 : highlight 0.2-2



More information about the Rcpp-devel mailing list