Rcpp Version 0.9.10
convolve8_cpp.cpp
Go to the documentation of this file.
00001 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
00002 
00003 // this version is between the Rcpp_New_ptr and the Rcpp_New_std version
00004 //                          test elapsed  relative user.self sys.self
00005 // 5    Rcpp_New_ptr(REPS, a, b)   0.214  1.000000     0.213    0.001
00006 // 7  Rcpp_New_std_2(REPS, a, b)   0.223  1.042056     0.216    0.006
00007 // 4    Rcpp_New_std(REPS, a, b)   0.524  2.448598     0.523    0.001
00008 //
00009 // so there is some overhead due to creating Vec objects and indexing them
00010 // but much less than when we index the NumericVector
00011 
00012 #include <Rcpp.h>
00013 
00014 class Vec {
00015 public:
00016     Vec( double* data_ ) : data(data_){}
00017     inline double& operator[]( int i){ return data[i] ; }
00018     
00019 private:
00020     double* data ;
00021 } ;
00022 
00023 
00024 RcppExport SEXP convolve8cpp(SEXP a, SEXP b){
00025     Rcpp::NumericVector xa(a);
00026     Rcpp::NumericVector xb(b);
00027     int n_xa = xa.size() ;
00028     int n_xb = xb.size() ;
00029     int nab = n_xa + n_xb - 1;
00030     Rcpp::NumericVector xab(nab);
00031     
00032     Vec vab(xab.begin()), va(xa.begin()), vb(xb.begin()) ;
00033     
00034     for (int i = 0; i < n_xa; i++)
00035         for (int j = 0; j < n_xb; j++) 
00036             vab[i + j] += va[i] * vb[j];
00037 
00038     return xab ;
00039 }
00040 
00041 #include "loopmacro.h"
00042 LOOPMACRO_CPP(convolve8cpp)
00043 
 All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Defines