Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2014-12-30 Dirk Eddelbuettel <edd@debian.org>

* inst/include/Rcpp/sugar/functions/mean.h: Use two-pass method

2014-12-29 Kevin Ushey <kevinushey@gmail.com>

* inst/include/Rcpp/macros/macros.h: reformat for legibility
Expand Down
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: Rcpp
Title: Seamless R and C++ Integration
Version: 0.11.3.3
Date: 2014-11-30
Version: 0.11.3.5
Date: 2014-12-30
Author: Dirk Eddelbuettel, Romain Francois, JJ Allaire, Kevin Ushey,
Douglas Bates, and John Chambers
Maintainer: Dirk Eddelbuettel <edd@debian.org>
Expand Down
1 change: 1 addition & 0 deletions inst/NEWS.Rd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
\code{List}, \code{RawVector} and \code{ExpressionVector}.
\item Vectors now have a \code{Vector::const\_iterator} that is 'const correct'
thanks to fix by Romain following bug report in rcpp-devel by Martyn Plummer
\item The \code{mean()} sugar function now uses a more robust two-pass method.
}
\item Changes in Rcpp Attributes:
\itemize{
Expand Down
31 changes: 22 additions & 9 deletions inst/include/Rcpp/sugar/functions/mean.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// mean.h: Rcpp R/C++ interface class library -- mean
//
// Copyright (C) 2011 Dirk Eddelbuettel and Romain Francois
// Copyright (C) 2011 - 2014 Dirk Eddelbuettel and Romain Francois
//
// This file is part of Rcpp.
//
Expand All @@ -28,23 +28,36 @@ namespace sugar{
template <int RTYPE, bool NA, typename T>
class Mean : public Lazy< typename Rcpp::traits::storage_type<RTYPE>::type , Mean<RTYPE,NA,T> > {
public:
typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;

Mean( const VEC_TYPE& object_ ) : object(object_){}
Mean( const VEC_TYPE& object_ ) : object(object_){}

STORAGE get() const {
return sum(object).get() / object.size() ;
}
STORAGE get() const {
//return sum(object).get() / object.size() ;
NumericVector input = object;

int n = input.size(); // double pass (as in summary.c)
long double s = std::accumulate(input.begin(), input.end(), 0.0L);
s /= n;
if (R_FINITE((double)s)) {
long double t = 0.0;
for (int i = 0; i < n; i++) {
t += input[i] - s;
}
s += t/n;
}
return (double)s ;
}
private:
const VEC_TYPE& object ;
const VEC_TYPE& object ;
} ;

} // sugar

template <bool NA, typename T>
inline sugar::Mean<REALSXP,NA,T> mean( const VectorBase<REALSXP,NA,T>& t){
return sugar::Mean<REALSXP,NA,T>( t ) ;
return sugar::Mean<REALSXP,NA,T>( t ) ;
}


Expand Down