Rcpp Version 1.0.9
pow.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2 //
3 // pow.h: Rcpp R/C++ interface class library -- pow
4 //
5 // Copyright (C) 2010 - 2011 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__pow_h
23 #define Rcpp__sugar__pow_h
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28 template <int RTYPE, bool NA, typename T, typename EXPONENT_TYPE>
29 class Pow : public Rcpp::VectorBase< REALSXP ,NA, Pow<RTYPE,NA,T,EXPONENT_TYPE> > {
30 public:
32 
33  Pow( const T& object_, EXPONENT_TYPE exponent ) : object(object_), op(exponent) {}
34 
35  inline double operator[]( R_xlen_t i ) const {
36  return ::pow( object[i], op );
37  }
38  inline R_xlen_t size() const { return object.size() ; }
39 
40 private:
41  const T& object ;
42  EXPONENT_TYPE op ;
43 } ;
44 
45 template <bool NA, typename T, typename EXPONENT_TYPE>
46 class Pow<INTSXP,NA,T,EXPONENT_TYPE> : public Rcpp::VectorBase< REALSXP ,NA, Pow<INTSXP,NA,T,EXPONENT_TYPE> > {
47 public:
48  Pow( const T& object_, EXPONENT_TYPE exponent ) : object(object_), op(exponent) {}
49 
50  inline double operator[]( R_xlen_t i ) const {
51  int x = object[i] ;
52  return x == NA_INTEGER ? NA_INTEGER : ::pow( x, op );
53  }
54  inline R_xlen_t size() const { return object.size() ; }
55 
56 private:
57  const T& object ;
58  EXPONENT_TYPE op ;
59 } ;
60 template <typename T, typename EXPONENT_TYPE>
61 class Pow<INTSXP,false,T,EXPONENT_TYPE> : public Rcpp::VectorBase< REALSXP ,false, Pow<INTSXP,false,T,EXPONENT_TYPE> > {
62 public:
63  Pow( const T& object_, EXPONENT_TYPE exponent ) : object(object_), op(exponent) {}
64 
65  inline double operator[]( R_xlen_t i ) const {
66  return ::pow( object[i], op );
67  }
68  inline R_xlen_t size() const { return object.size() ; }
69 
70 private:
71  const T& object ;
72  EXPONENT_TYPE op ;
73 } ;
74 
75 
76 } // sugar
77 
78 template <int RTYPE, bool NA, typename T, typename EXPONENT_TYPE>
80  const VectorBase<RTYPE,NA,T>& t,
81  EXPONENT_TYPE exponent
82 ){
83  return sugar::Pow<RTYPE,NA,T,EXPONENT_TYPE>( t.get_ref() , exponent ) ;
84 }
85 
86 
87 } // Rcpp
88 #endif
89 
VECTOR & get_ref()
Definition: VectorBase.h:37
double operator[](R_xlen_t i) const
Definition: pow.h:50
Pow(const T &object_, EXPONENT_TYPE exponent)
Definition: pow.h:48
Pow(const T &object_, EXPONENT_TYPE exponent)
Definition: pow.h:63
Pow(const T &object_, EXPONENT_TYPE exponent)
Definition: pow.h:33
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition: pow.h:31
double operator[](R_xlen_t i) const
Definition: pow.h:35
const T & object
Definition: pow.h:41
R_xlen_t size() const
Definition: pow.h:38
EXPONENT_TYPE op
Definition: pow.h:42
Rcpp API.
Definition: algo.h:28
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