Skip to content

Commit e6882c0

Browse files
committed
Merge pull request #220 from RcppCore/feature/two-pass-mean
Feature/two pass mean
2 parents 1285268 + b5764cf commit e6882c0

File tree

4 files changed

+29
-11
lines changed

4 files changed

+29
-11
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2014-12-30 Dirk Eddelbuettel <[email protected]>
2+
3+
* inst/include/Rcpp/sugar/functions/mean.h: Use two-pass method
4+
15
2014-12-29 Kevin Ushey <[email protected]>
26

37
* inst/include/Rcpp/macros/macros.h: reformat for legibility

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: Rcpp
22
Title: Seamless R and C++ Integration
3-
Version: 0.11.3.3
4-
Date: 2014-11-30
3+
Version: 0.11.3.5
4+
Date: 2014-12-30
55
Author: Dirk Eddelbuettel, Romain Francois, JJ Allaire, Kevin Ushey,
66
Douglas Bates, and John Chambers
77
Maintainer: Dirk Eddelbuettel <[email protected]>

inst/NEWS.Rd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
\code{List}, \code{RawVector} and \code{ExpressionVector}.
2121
\item Vectors now have a \code{Vector::const\_iterator} that is 'const correct'
2222
thanks to fix by Romain following bug report in rcpp-devel by Martyn Plummer
23+
\item The \code{mean()} sugar function now uses a more robust two-pass method.
2324
}
2425
\item Changes in Rcpp Attributes:
2526
\itemize{

inst/include/Rcpp/sugar/functions/mean.h

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// mean.h: Rcpp R/C++ interface class library -- mean
44
//
5-
// Copyright (C) 2011 Dirk Eddelbuettel and Romain Francois
5+
// Copyright (C) 2011 - 2014 Dirk Eddelbuettel and Romain Francois
66
//
77
// This file is part of Rcpp.
88
//
@@ -28,23 +28,36 @@ namespace sugar{
2828
template <int RTYPE, bool NA, typename T>
2929
class Mean : public Lazy< typename Rcpp::traits::storage_type<RTYPE>::type , Mean<RTYPE,NA,T> > {
3030
public:
31-
typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
32-
typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
31+
typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
32+
typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
3333

34-
Mean( const VEC_TYPE& object_ ) : object(object_){}
34+
Mean( const VEC_TYPE& object_ ) : object(object_){}
3535

36-
STORAGE get() const {
37-
return sum(object).get() / object.size() ;
38-
}
36+
STORAGE get() const {
37+
//return sum(object).get() / object.size() ;
38+
NumericVector input = object;
39+
40+
int n = input.size(); // double pass (as in summary.c)
41+
long double s = std::accumulate(input.begin(), input.end(), 0.0L);
42+
s /= n;
43+
if (R_FINITE((double)s)) {
44+
long double t = 0.0;
45+
for (int i = 0; i < n; i++) {
46+
t += input[i] - s;
47+
}
48+
s += t/n;
49+
}
50+
return (double)s ;
51+
}
3952
private:
40-
const VEC_TYPE& object ;
53+
const VEC_TYPE& object ;
4154
} ;
4255

4356
} // sugar
4457

4558
template <bool NA, typename T>
4659
inline sugar::Mean<REALSXP,NA,T> mean( const VectorBase<REALSXP,NA,T>& t){
47-
return sugar::Mean<REALSXP,NA,T>( t ) ;
60+
return sugar::Mean<REALSXP,NA,T>( t ) ;
4861
}
4962

5063

0 commit comments

Comments
 (0)