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