Hello,

Initially motivated by Doug's question: http://article.gmane.org/gmane.comp.lang.r.rcpp/412

and drafted quickly in : http://article.gmane.org/gmane.comp.lang.r.rcpp/422

I've added some code in Rcpp that I call "Rcpp sugar" (another name I had in mind was "Rcpp plasma". I like sugar because what this does is to add some syntactic sugar so that Rcpp code looks more like R code.

So now we can use "any" and "all" with binary operators <,>,<=,>=,!=,== on Rcpp vectors.

Here is one random example from the unit test suite:

test.sugar.all.less.or.equal <- function( ){

        fx <- cxxfunction( signature( x = "numeric", y = "numeric" ), '
        
                NumericVector xx(x) ;
                NumericVector yy(y) ;
                
                return all( xx <= yy ) ;
        
        ', plugin = "Rcpp" )
        
        checkTrue( fx( 1, 1 ) )
        checkTrue( ! fx( 1:2, c(1,1) ) )
        checkTrue( fx( 0, 1 ) )
        checkTrue( ! fx( 1, 0 ) )
        checkTrue( is.na( fx( NA, 1 ) ) )
        
}


The difference between the Rcpp code:

all( xx <= yy )

and the R code

all( xx <= yy )


is that R would first allocate the logical vector resulting from the expression "xx <= yy" and then call all.

Rcpp::all does not need to allocate the logical vector, so will be more efficient.

This is the tip of the iceberg of what Rcpp::sugar could be, and uses a version of expression templates (e.g. see armadillo or Blitz).

Much more could be done, but this will get complicated to code at some point. So I'm not sure yet if I will or if I will wait for contributions or sponsorship, etc ...

See other examples in the test files that contain the word "sugar" :

> list.files( system.file( "unitTests", package = "Rcpp" ), full = T, pattern = "sugar" )

Romain


--
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/98Uf7u : Rcpp 0.8.1
|- http://bit.ly/c6YnCi : graph gallery collage
`- http://bit.ly/bZ7ltC : inline 0.3.5

_______________________________________________
Rcpp-devel mailing list
[email protected]
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

Reply via email to