[Rcpp-commits] r1695 - in pkg/Rcpp/inst: examples/ConvolveBenchmarks include/Rcpp/vector
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jun 23 22:57:24 CEST 2010
Author: romain
Date: 2010-06-23 22:57:24 +0200 (Wed, 23 Jun 2010)
New Revision: 1695
Added:
pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve5_cpp.cpp
Modified:
pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh
pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
Log:
using sugar to convolve
Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh 2010-06-23 20:48:10 UTC (rev 1694)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/buildAndRun.sh 2010-06-23 20:57:24 UTC (rev 1695)
@@ -14,6 +14,7 @@
R CMD SHLIB convolve2_cpp.cpp
R CMD SHLIB convolve3_cpp.cpp
R CMD SHLIB convolve4_cpp.cpp
+R CMD SHLIB convolve5_cpp.cpp
# call R so that we get an interactive session
Rscript exampleRCode.r
Added: pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve5_cpp.cpp
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve5_cpp.cpp (rev 0)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/convolve5_cpp.cpp 2010-06-23 20:57:24 UTC (rev 1695)
@@ -0,0 +1,19 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
+
+// This is a rewrite of the 'Writing R Extensions' section 5.10.1 example
+
+#include <Rcpp.h>
+using namespace Rcpp ;
+
+RcppExport SEXP convolve5cpp(SEXP a, SEXP b) {
+ NumericVector xa(a); int n_xa = xa.size() ;
+ NumericVector xb(b); int n_xb = xb.size() ;
+ int nab = n_xa + n_xb - 1;
+
+ NumericVector xab(nab,0.0);
+
+ for(int i=0; i<n_xa; i++){
+ xab[ Range(i, i+n_xb-1) ] += xa[i] * xb ;
+ }
+ return xab ;
+}
Modified: pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r
===================================================================
--- pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r 2010-06-23 20:48:10 UTC (rev 1694)
+++ pkg/Rcpp/inst/examples/ConvolveBenchmarks/exampleRCode.r 2010-06-23 20:57:24 UTC (rev 1695)
@@ -36,6 +36,12 @@
t4 <- system.time(replicate(1000, .Call("convolve4cpp", a, b)))
+## load the sugar based version
+dyn.load( "convolve5_cpp.so" )
+v5 <- .Call( "convolve5cpp", a, b )
+t5 <- system.time(replicate(1000, .Call("convolve5cpp", a, b)))
+
+
## load shared library with wrapper code and callback class
dyn.load("convolve7_c.so")
@@ -44,15 +50,16 @@
t7 <- system.time(replicate(1000, .Call("convolve7", a, b)))
-res <- data.frame(rbind(t1, t7, t2, t3, t4))
+res <- data.frame(rbind(t1, t7, t2, t3, t4, t5))
rownames(res) <- c("Writing R extensions",
"Less careful use of R API",
"RcppVector<double>::operator()",
"Rcpp::NumericVector::operator[]",
- "Rcpp::NumericVector::begin()")
+ "Rcpp::NumericVector::begin()",
+ "sugar" )
print(res)
-results <- list( v1, v2, v3, v4, v7)
+results <- list( v1, v2, v3, v4, v7, v5)
for (i in seq_along(results) ){
stopifnot( all.equal(results[[1L]], results[[i]] ) )
}
Modified: pkg/Rcpp/inst/include/Rcpp/vector/Vector.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/vector/Vector.h 2010-06-23 20:48:10 UTC (rev 1694)
+++ pkg/Rcpp/inst/include/Rcpp/vector/Vector.h 2010-06-23 20:57:24 UTC (rev 1695)
@@ -682,7 +682,16 @@
}
return *this ;
}
-
+
+ template <bool NA, typename T>
+ RangeIndexer& operator+=( const VectorBase<RTYPE,NA,T>& x){
+ int n = size() ;
+ for( int i=0; i<n; i++){
+ vec[ range[i] ] += x[i] ;
+ }
+ return *this ;
+ }
+
inline Proxy operator[]( int i ){
return vec[ range[i] ] ;
}
More information about the Rcpp-commits
mailing list