Rcpp Version 1.0.9
mean.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // mean.h: Rcpp R/C++ interface class library -- mean
4 //
5 // Copyright (C) 2011 - 2015 Dirk Eddelbuettel and Romain Francois
6 //
7 // This file is part of Rcpp.
8 //
9 // Rcpp is free software: you can redistribute it and/or modify it
10 // under the terms of the GNU General Public License as published by
11 // the Free Software Foundation, either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // Rcpp is distributed in the hope that it will be useful, but
15 // WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
21 
22 #ifndef Rcpp__sugar__mean_h
23 #define Rcpp__sugar__mean_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <int RTYPE, bool NA, typename T>
29 class Mean : public Lazy<double, Mean<RTYPE,NA,T> > {
30 public:
33 
34  Mean(const VEC_TYPE& object_) : object(object_) {}
35 
36  double get() const {
37  VECTOR input = object;
38  R_xlen_t n = input.size(); // double pass (as in summary.c)
39  long double s = std::accumulate(input.begin(), input.end(), 0.0L);
40  s /= n;
41  if (R_FINITE((double)s)) {
42  long double t = 0.0;
43  for (R_xlen_t i = 0; i < n; i++) {
44  t += input[i] - s;
45  }
46  s += t/n;
47  }
48  return (double)s ;
49  }
50 private:
51  const VEC_TYPE& object ;
52 };
53 
54 template <bool NA, typename T>
55 class Mean<CPLXSXP,NA,T> : public Lazy<Rcomplex, Mean<CPLXSXP,NA,T> > {
56 public:
58 
59  Mean(const VEC_TYPE& object_) : object(object_) {}
60 
61  Rcomplex get() const {
62  ComplexVector input = object;
63  R_xlen_t n = input.size(); // double pass (as in summary.c)
64  long double s = 0.0, si = 0.0;
65  for (R_xlen_t i=0; i<n; i++) {
66  Rcomplex z = input[i];
67  s += z.r;
68  si += z.i;
69  }
70  s /= n;
71  si /= n;
72  if (R_FINITE((double)s) && R_FINITE((double)si)) {
73  long double t = 0.0, ti = 0.0;
74  for (R_xlen_t i = 0; i < n; i++) {
75  Rcomplex z = input[i];
76  t += z.r - s;
77  ti += z.i - si;
78  }
79  s += t/n;
80  si += ti/n;
81  }
82  Rcomplex z;
83  z.r = s;
84  z.i = si;
85  return z;
86  }
87 private:
88  const VEC_TYPE& object ;
89 };
90 
91 template <bool NA, typename T>
92 class Mean<LGLSXP,NA,T> : public Lazy<double, Mean<LGLSXP,NA,T> > {
93 public:
95 
96  Mean(const VEC_TYPE& object_) : object(object_) {}
97 
98  double get() const {
99  LogicalVector input = object;
100  R_xlen_t n = input.size();
101  long double s = 0.0;
102  for (R_xlen_t i=0; i<n; i++) {
103  if (input[i] == NA_INTEGER) return NA_REAL;
104  s += input[i];
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 
113 template <bool NA, typename T>
114 class Mean<INTSXP,NA,T> : public Lazy<double, Mean<INTSXP,NA,T> > {
115 public:
117 
118  Mean(const VEC_TYPE& object_) : object(object_) {}
119 
120  double get() const {
121  IntegerVector input = object;
122  R_xlen_t n = input.size(); // double pass (as in summary.c)
123  long double s = std::accumulate(input.begin(), input.end(), 0.0L);
124  s /= n;
125  long double t = 0.0;
126  for (R_xlen_t i = 0; i < n; i++) {
127  if (input[i] == NA_INTEGER) return NA_REAL;
128  t += input[i] - s;
129  }
130  s += t/n;
131  return (double)s ;
132  }
133 private:
134  const VEC_TYPE& object ;
135 };
136 
137 } // sugar
138 
139 template <bool NA, typename T>
141  return sugar::Mean<REALSXP,NA,T>(t);
142 }
143 
144 template <bool NA, typename T>
146  return sugar::Mean<INTSXP,NA,T>(t);
147 }
148 
149 template <bool NA, typename T>
151  return sugar::Mean<CPLXSXP,NA,T>(t);
152 }
153 
154 template <bool NA, typename T>
156  return sugar::Mean<LGLSXP,NA,T>(t);
157 }
158 
159 } // Rcpp
160 #endif
161 
162 
R_xlen_t size() const
Definition: Vector.h:276
iterator end()
Definition: Vector.h:335
iterator begin()
Definition: Vector.h:334
const VEC_TYPE & object
Definition: mean.h:88
Mean(const VEC_TYPE &object_)
Definition: mean.h:59
Rcpp::VectorBase< CPLXSXP, NA, T > VEC_TYPE
Definition: mean.h:57
Mean(const VEC_TYPE &object_)
Definition: mean.h:118
Rcpp::VectorBase< INTSXP, NA, T > VEC_TYPE
Definition: mean.h:116
const VEC_TYPE & object
Definition: mean.h:134
Mean(const VEC_TYPE &object_)
Definition: mean.h:96
Rcpp::VectorBase< LGLSXP, NA, T > VEC_TYPE
Definition: mean.h:94
const VEC_TYPE & object
Definition: mean.h:110
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition: mean.h:31
Rcpp::Vector< RTYPE > VECTOR
Definition: mean.h:32
double get() const
Definition: mean.h:36
const VEC_TYPE & object
Definition: mean.h:51
Mean(const VEC_TYPE &object_)
Definition: mean.h:34
Rcpp API.
Definition: algo.h:28
sugar::Mean< REALSXP, NA, T > mean(const VectorBase< REALSXP, NA, T > &t)
Definition: mean.h:140
static Na_Proxy NA
Definition: Na_Proxy.h:52