Skip to content

Commit d62a215

Browse files
committed
Merge pull request #233 from wush978/master
Resolve #233
2 parents 4d49373 + df64b62 commit d62a215

File tree

2 files changed

+83
-12
lines changed

2 files changed

+83
-12
lines changed

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

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// var.h: Rcpp R/C++ interface class library -- var
44
//
55
// Copyright (C) 2011 Dirk Eddelbuettel and Romain Francois
6+
// Copyright (C) 2015 Wush Wu
67
//
78
// This file is part of Rcpp.
89
//
@@ -26,31 +27,65 @@ namespace Rcpp{
2627
namespace sugar{
2728

2829
template <int RTYPE, bool NA, typename T>
29-
class Var : public Lazy< typename Rcpp::traits::storage_type<RTYPE>::type , Var<RTYPE,NA,T> > {
30+
class Var : public Lazy< double , Var<RTYPE,NA,T> > {
3031
public:
31-
typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
32-
typedef typename Rcpp::traits::storage_type<RTYPE>::type STORAGE ;
32+
typedef typename Rcpp::VectorBase<RTYPE,NA,T> VEC_TYPE ;
3333

34-
Var( const VEC_TYPE& object_ ) : object(object_){}
34+
Var( const VEC_TYPE& object_ ) : object(object_){}
3535

36-
STORAGE get() const{
37-
STORAGE m = mean(object).get() ;
38-
Minus_Vector_Primitive<RTYPE,NA,T> mm( object, m) ;
39-
STORAGE ssq = sum( pow(mm,2.0) ).get() ;
40-
return ssq / (object.size() - 1 ) ;
41-
}
36+
double get() const{
37+
double
38+
m = mean(object).get(),
39+
ssq = sum( pow(object, 2.0) ).get();
40+
return (ssq - m * m * object.size()) / (object.size() - 1);
41+
}
4242

4343
private:
44-
const VEC_TYPE& object ;
44+
const VEC_TYPE& object ;
45+
} ;
46+
47+
template <bool NA, typename T>
48+
class Var<CPLXSXP,NA,T> : public Lazy< double , Var<CPLXSXP,NA,T> > {
49+
public:
50+
typedef typename Rcpp::VectorBase<CPLXSXP,NA,T> VEC_TYPE ;
51+
52+
Var( const VEC_TYPE& object_ ) : object(object_){}
53+
54+
double get() const{
55+
double sq = 0, ssq = 0;
56+
for(int i = 0;i < object.size();i++) {
57+
Rcomplex z = object[i];
58+
sq += z.r;
59+
ssq += z.r * z.r;
60+
}
61+
return (ssq - sq * sq / object.size()) / (object.size() - 1);
62+
}
63+
64+
private:
65+
const VEC_TYPE& object ;
4566
} ;
4667

4768
} // sugar
4869

4970
template <bool NA, typename T>
5071
inline sugar::Var<REALSXP,NA,T> var( const VectorBase<REALSXP,NA,T>& t){
51-
return sugar::Var<REALSXP,NA,T>( t ) ;
72+
return sugar::Var<REALSXP,NA,T>( t ) ;
5273
}
5374

75+
template <bool NA, typename T>
76+
inline sugar::Var<INTSXP,NA,T> var( const VectorBase<INTSXP,NA,T>& t){
77+
return sugar::Var<INTSXP,NA,T>( t ) ;
78+
}
79+
80+
template <bool NA, typename T>
81+
inline sugar::Var<LGLSXP,NA,T> var( const VectorBase<LGLSXP,NA,T>& t){
82+
return sugar::Var<LGLSXP,NA,T>( t ) ;
83+
}
84+
85+
template <bool NA, typename T>
86+
inline sugar::Var<CPLXSXP,NA,T> var( const VectorBase<CPLXSXP,NA,T>& t){
87+
return sugar::Var<CPLXSXP,NA,T>( t ) ;
88+
}
5489

5590
} // Rcpp
5691
#endif

inst/unitTests/runit.sugar.var.R

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/r -t
2+
# -*- mode: R; ess-indent-level: 4; indent-tabs-mode: nil; -*-
3+
#
4+
# Copyright (C) 2015 Wush Wu
5+
#
6+
# This file is part of Rcpp.
7+
#
8+
# Rcpp is free software: you can redistribute it and/or modify it
9+
# under the terms of the GNU General Public License as published by
10+
# the Free Software Foundation, either version 2 of the License, or
11+
# (at your option) any later version.
12+
#
13+
# Rcpp is distributed in the hope that it will be useful, but
14+
# WITHOUT ANY WARRANTY; without even the implied warranty of
15+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
# GNU General Public License for more details.
17+
#
18+
# You should have received a copy of the GNU General Public License
19+
# along with Rcpp. If not, see <http://www.gnu.org/licenses/>.
20+
21+
.runThisTest <- Sys.getenv("RunAllRcppTests") == "yes"
22+
23+
if (.runThisTest) {
24+
25+
test.Sugar.var <- function() {
26+
f1 <- Rcpp::cppFunction('double myVar(NumericVector x) { return(var(x)); }')
27+
f2 <- Rcpp::cppFunction('double myVar(IntegerVector x) { return(var(x)); }')
28+
f3 <- Rcpp::cppFunction('double myVar(ComplexVector x) { return(var(x)); }')
29+
f4 <- Rcpp::cppFunction('double myVar(LogicalVector x) { return(var(x)); }')
30+
checkEquals(f1((1:10) * 1.1), var((1:10) * 1.1))
31+
checkEquals(f2(1:10), var(1:10))
32+
checkEquals(f3(1:10 + (1 + 1i)), var(1:10 + (1 + 1i)))
33+
checkEquals(f4(c(T, F, T, F, T)), var(c(T, F, T, F, T)))
34+
}
35+
36+
}

0 commit comments

Comments
 (0)