23 #ifndef Rcpp__sugar__var_h
24 #define Rcpp__sugar__var_h
29 template <
int RTYPE,
bool NA,
typename T>
30 class Var :
public Lazy< double , Var<RTYPE,NA,T> > {
37 const double average =
mean(
object).get();
38 const R_xlen_t sample_size =
object.size();
39 double sum_squared_deviations = 0.0;
40 for (R_xlen_t i = 0; i != sample_size; ++i)
41 sum_squared_deviations +=
std::pow(
object[i] - average, 2.0);
42 return sum_squared_deviations / (sample_size - 1);
49 template <
bool NA,
typename T>
50 class Var<CPLXSXP,
NA,T> :
public Lazy< double , Var<CPLXSXP,NA,T> > {
57 const Rcomplex average =
mean(
object).get();
58 const R_xlen_t sample_size =
object.size();
59 double sum_squared_deviations_magnitudes = 0.0;
60 for (R_xlen_t i = 0; i != sample_size; ++i) {
61 const Rcomplex deviation =
object[i] - average;
62 sum_squared_deviations_magnitudes += deviation.r * deviation.r + deviation.i * deviation.i;
64 return sum_squared_deviations_magnitudes / (sample_size - 1);
73 template <
bool NA,
typename T>
78 template <
bool NA,
typename T>
83 template <
bool NA,
typename T>
88 template <
bool NA,
typename T>
Rcpp::VectorBase< CPLXSXP, NA, T > VEC_TYPE
Var(const VEC_TYPE &object_)
Var(const VEC_TYPE &object_)
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
sugar::Var< REALSXP, NA, T > var(const VectorBase< REALSXP, NA, T > &t)
sugar::Mean< REALSXP, NA, T > mean(const VectorBase< REALSXP, NA, T > &t)
sugar::Pow< RTYPE, NA, T, EXPONENT_TYPE > pow(const VectorBase< RTYPE, NA, T > &t, EXPONENT_TYPE exponent)