Re: [Rcpp-devel] Struggling with Rcpp sugar

2012-11-18 Thread Hadley Wickham
>> recycle(x, y, z); >> >> Hadley > > > In a way, we can already use sugar rep_len. > > rep_len( x, 30 ) > > will recycle x into a vector of length 30, lazily. Agreed, but a helper function would automate this process: int length = max(x.length(), y.length(), z.length()) # check integer multiples

Re: [Rcpp-devel] Struggling with Rcpp sugar

2012-11-18 Thread Romain Francois
Le 18/11/12 16:12, Hadley Wickham a écrit : microbenchmark( pdist1(0.5, ys), pdist2(0.5, ys), pdist3(0.5, ys) ) # 10-fold slower?? Maybe it's because I'm using a double instead of # a numeric vector? That's weird. Not sure where the ineffiency is. Some hunting. My guess is that sug

Re: [Rcpp-devel] Struggling with Rcpp sugar

2012-11-18 Thread Hadley Wickham
>> microbenchmark( >>pdist1(0.5, ys), >>pdist2(0.5, ys), >>pdist3(0.5, ys) >> ) >> # 10-fold slower?? Maybe it's because I'm using a double instead of >> # a numeric vector? > > That's weird. Not sure where the ineffiency is. Some hunting. > My guess is that sugar pow needs a refresh.

Re: [Rcpp-devel] Struggling with Rcpp sugar

2012-11-17 Thread Romain Francois
Another, with mapply. But there we have to use rep since our mapply only works on vector expressions. inline double distance(double y, double x){ return pow( (y-x), 2.0 ) ; } // [[Rcpp::export]] NumericVector pdist6(double x, NumericVector ys) { return mapply( ys, rep(x,ys.size()), distance

Re: [Rcpp-devel] Struggling with Rcpp sugar

2012-11-17 Thread Romain Francois
Hi, While there, consider this version based on sapply: class Distance { public: typedef double result_type ; Distance( double x_ ) : x(x_){} inline double operator()(double y) const { return pow( (y-x), 2.0 ) ; } private: double x; } ; // [[Rcpp::export]] NumericVector pdist

Re: [Rcpp-devel] Struggling with Rcpp sugar

2012-11-17 Thread Romain Francois
Le 17/11/12 14:42, Hadley Wickham a écrit : Hi all, I've included what seems to be a simple application of Rcpp sugar below, but I'm getting some very strange results. Any help would be much appreciate! Thanks, Hadley library(Rcpp) library(microbenchmark) # Compute distance between single p

[Rcpp-devel] Struggling with Rcpp sugar

2012-11-17 Thread Hadley Wickham
Hi all, I've included what seems to be a simple application of Rcpp sugar below, but I'm getting some very strange results. Any help would be much appreciate! Thanks, Hadley library(Rcpp) library(microbenchmark) # Compute distance between single point and vector of points pdist1 <- function(x