Rcpp Version 1.0.9
var.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // var.h: Rcpp R/C++ interface class library -- var
4 //
5 // Copyright (C) 2011 Dirk Eddelbuettel and Romain Francois
6 // Copyright (C) 2015 Wush Wu
7 //
8 // This file is part of Rcpp.
9 //
10 // Rcpp is free software: you can redistribute it and/or modify it
11 // under the terms of the GNU General Public License as published by
12 // the Free Software Foundation, either version 2 of the License, or
13 // (at your option) any later version.
14 //
15 // Rcpp is distributed in the hope that it will be useful, but
16 // WITHOUT ANY WARRANTY; without even the implied warranty of
17 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 // GNU General Public License for more details.
19 //
20 // You should have received a copy of the GNU General Public License
21 // along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
22 
23 #ifndef Rcpp__sugar__var_h
24 #define Rcpp__sugar__var_h
25 
26 namespace Rcpp{
27 namespace sugar{
28 
29 template <int RTYPE, bool NA, typename T>
30 class Var : public Lazy< double , Var<RTYPE,NA,T> > {
31 public:
33 
34  Var( const VEC_TYPE& object_ ) : object(object_){}
35 
36  double get() const{
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);
43  }
44 
45 private:
46  const VEC_TYPE& object ;
47 } ;
48 
49 template <bool NA, typename T>
50 class Var<CPLXSXP,NA,T> : public Lazy< double , Var<CPLXSXP,NA,T> > {
51 public:
53 
54  Var( const VEC_TYPE& object_ ) : object(object_){}
55 
56  double get() const{
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;
63  }
64  return sum_squared_deviations_magnitudes / (sample_size - 1);
65  }
66 
67 private:
68  const VEC_TYPE& object ;
69 } ;
70 
71 } // sugar
72 
73 template <bool NA, typename T>
75  return sugar::Var<REALSXP,NA,T>( t ) ;
76 }
77 
78 template <bool NA, typename T>
80  return sugar::Var<INTSXP,NA,T>( t ) ;
81 }
82 
83 template <bool NA, typename T>
85  return sugar::Var<LGLSXP,NA,T>( t ) ;
86 }
87 
88 template <bool NA, typename T>
90  return sugar::Var<CPLXSXP,NA,T>( t ) ;
91 }
92 
93 } // Rcpp
94 #endif
95 
const VEC_TYPE & object
Definition: var.h:68
Rcpp::VectorBase< CPLXSXP, NA, T > VEC_TYPE
Definition: var.h:52
Var(const VEC_TYPE &object_)
Definition: var.h:54
Var(const VEC_TYPE &object_)
Definition: var.h:34
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition: var.h:32
const VEC_TYPE & object
Definition: var.h:46
double get() const
Definition: var.h:36
Rcpp API.
Definition: algo.h:28
sugar::Var< REALSXP, NA, T > var(const VectorBase< REALSXP, NA, T > &t)
Definition: var.h:74
sugar::Mean< REALSXP, NA, T > mean(const VectorBase< REALSXP, NA, T > &t)
Definition: mean.h:140
sugar::Pow< RTYPE, NA, T, EXPONENT_TYPE > pow(const VectorBase< RTYPE, NA, T > &t, EXPONENT_TYPE exponent)
Definition: pow.h:79
static Na_Proxy NA
Definition: Na_Proxy.h:52