[Rcpp-commits] r2359 - in pkg/Rcpp: . inst/include/Rcpp/sugar/functions inst/unitTests
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Sun Oct 24 03:35:38 CEST 2010
Author: romain
Date: 2010-10-24 03:35:34 +0200 (Sun, 24 Oct 2010)
New Revision: 2359
Modified:
pkg/Rcpp/ChangeLog
pkg/Rcpp/TODO
pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h
pkg/Rcpp/inst/unitTests/runit.sugar.R
Log:
sugar::sum now takes care of missing values
Modified: pkg/Rcpp/ChangeLog
===================================================================
--- pkg/Rcpp/ChangeLog 2010-10-24 00:52:37 UTC (rev 2358)
+++ pkg/Rcpp/ChangeLog 2010-10-24 01:35:34 UTC (rev 2359)
@@ -8,6 +8,9 @@
* inst/unitTests/runit.Matrix.R: unit tests for the above<
+ * inst/include/Rcpp/sugar/functions/sum.h: the sugar version of sum
+ now takes care of missing values correctly
+
2010-10-21 Romain Francois <romain at r-enthusiasts.com>
* inst/include/Rcpp/vector/MatrixColumn.h: Fixed indexing bug (mismatch
Modified: pkg/Rcpp/TODO
===================================================================
--- pkg/Rcpp/TODO 2010-10-24 00:52:37 UTC (rev 2358)
+++ pkg/Rcpp/TODO 2010-10-24 01:35:34 UTC (rev 2359)
@@ -84,8 +84,6 @@
tukey : only has p and q, no r or d
- o sum : deal with missing values
-
o other random generators:
rmultinom : tricky because it generates a matrix
Modified: pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h
===================================================================
--- pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h 2010-10-24 00:52:37 UTC (rev 2358)
+++ pkg/Rcpp/inst/include/Rcpp/sugar/functions/sum.h 2010-10-24 01:35:34 UTC (rev 2359)
@@ -40,13 +40,42 @@
Sum( const VEC_TYPE& object_ ) : object(object_){}
- inline STORAGE get() const {
- return std::accumulate( object.begin(), object.end(), STORAGE() , std::plus<STORAGE>() ) ;
+ STORAGE get() const {
+ STORAGE result = 0 ;
+ int n = object.size() ;
+ STORAGE current ;
+ for( int i=0; i<n; i++){
+ current = object[i] ;
+ if( Rcpp::traits::is_na<RTYPE>(current) )
+ return Rcpp::traits::get_na<RTYPE>() ;
+ result += current ;
+ }
+ return result ;
}
private:
const VEC_TYPE& object ;
} ;
+
+template <int RTYPE, typename T>
+class Sum<RTYPE,false,T> : public Lazy< typename Rcpp::traits::storage_type<RTYPE>::type , Sum<RTYPE,false,T> > {
+public:
+ typedef typename Rcpp::VectorBase<RTYPE,false,T> VEC_TYPE ;
+ typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
+ Sum( const VEC_TYPE& object_ ) : object(object_){}
+
+ STORAGE get() const {
+ STORAGE result = 0 ;
+ int n = object.size() ;
+ for( int i=0; i<n; i++){
+ result += object[i] ;
+ }
+ return result ;
+ }
+private:
+ const VEC_TYPE& object ;
+} ;
+
} // sugar
template <bool NA, typename T>
Modified: pkg/Rcpp/inst/unitTests/runit.sugar.R
===================================================================
--- pkg/Rcpp/inst/unitTests/runit.sugar.R 2010-10-24 00:52:37 UTC (rev 2358)
+++ pkg/Rcpp/inst/unitTests/runit.sugar.R 2010-10-24 01:35:34 UTC (rev 2359)
@@ -644,7 +644,15 @@
_["expm1"] = expm1(xx)
) ;
'
- )
+ ),
+ "runit_sum" = list(
+ signature( x = "numeric" ),
+ '
+ NumericVector xx(x) ;
+ double res = sum( xx ) ;
+ return wrap( res ) ;
+ '
+ )
)
signatures <- lapply( sugar.functions, "[[", 1L )
@@ -1257,3 +1265,11 @@
) )
}
+test.sugar.sum <- function(){
+ fx <- .rcpp.sugar$runit_sum
+ x <- rnorm( 10 )
+ checkEquals( fx(x), sum(x) )
+ x[4] <- NA
+ checkEquals( fx(x), sum(x) )
+}
+
More information about the Rcpp-commits
mailing list