Skip to content

Commit bcc1c45

Browse files
committed
mean needs to return double, not the storage_type
1 parent 8abb181 commit bcc1c45

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
2015-01-01 Dirk Eddelbuettel <[email protected]>
2+
3+
* inst/include/Rcpp/sugar/functions/mean.h: Return type is double, not
4+
storage type; also added logical vector case [in progress]
5+
16
2014-12-31 Dirk Eddelbuettel <[email protected]>
27

38
* inst/include/Rcpp/sugar/functions/mean.h: Support int and complex

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ namespace Rcpp{
2626
namespace sugar{
2727

2828
template <int RTYPE, bool NA, typename T>
29-
class Mean : public Lazy<typename Rcpp::traits::storage_type<RTYPE>::type, Mean<RTYPE,NA,T> > {
29+
class Mean : public Lazy<double, Mean<RTYPE,NA,T> > {
3030
public:
3131
typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE;
32-
typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE;
3332
typedef Rcpp::Vector<RTYPE> VECTOR;
3433

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

37-
STORAGE get() const {
36+
double get() const {
3837
VECTOR input = object;
3938
int n = input.size(); // double pass (as in summary.c)
4039
long double s = std::accumulate(input.begin(), input.end(), 0.0L);
@@ -89,6 +88,28 @@ class Mean<CPLXSXP,NA,T> : public Lazy<Rcomplex, Mean<CPLXSXP,NA,T> > {
8988
const VEC_TYPE& object ;
9089
};
9190

91+
template <bool NA, typename T>
92+
class Mean<LGLSXP,NA,T> : public Lazy<double, Mean<LGLSXP,NA,T> > {
93+
public:
94+
typedef typename Rcpp::VectorBase<LGLSXP,NA,T> VEC_TYPE;
95+
96+
Mean(const VEC_TYPE& object_) : object(object_) {}
97+
98+
double get() const {
99+
LogicalVector input = object;
100+
int n = input.size(); // double pass (as in summary.c)
101+
long double s = 0.0;
102+
for (int i=0; i<n; i++) {
103+
if (input[i] == NA) return NA;
104+
s += (input[i] == TRUE);
105+
}
106+
s /= n; // no overflow correction needed for logical vectors
107+
return (double)s;
108+
}
109+
private:
110+
const VEC_TYPE& object ;
111+
};
112+
92113
} // sugar
93114

94115
template <bool NA, typename T>
@@ -106,6 +127,11 @@ inline sugar::Mean<CPLXSXP,NA,T> mean(const VectorBase<CPLXSXP,NA,T>& t) {
106127
return sugar::Mean<CPLXSXP,NA,T>(t);
107128
}
108129

130+
template <bool NA, typename T>
131+
inline sugar::Mean<LGLSXP,NA,T> mean(const VectorBase<LGLSXP,NA,T>& t) {
132+
return sugar::Mean<LGLSXP,NA,T>(t);
133+
}
134+
109135
} // Rcpp
110136
#endif
111137

0 commit comments

Comments
 (0)