Rcpp Version 1.0.9
Vectorized_Math.h
Go to the documentation of this file.
1 // -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; indent-tabs-mode: nil; -*-
2 //
3 // SugarBlock.h: Rcpp R/C++ interface class library -- sugar functions
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_VECTORIZEDMATH_H
23 #define RCPP_SUGAR_VECTORIZEDMATH_H
24 
25 namespace Rcpp{
26 namespace sugar{
27 
28 extern "C" typedef double (*DDFun)(double);
29 
30 template <DDFun Func, bool NA, typename VEC>
31 class Vectorized : public VectorBase<REALSXP, NA, Vectorized<Func,NA,VEC> >{
32 public:
35 
36  Vectorized( const VEC_TYPE& object_) : object( object_.get_ref() ){}
37  inline double operator[]( R_xlen_t i) const {
38  return Func( object[i] ) ;
39  }
40  inline R_xlen_t size() const { return object.size(); }
41 
42 private:
43  const VEC_EXT& object ;
44 } ;
45 
46 template <DDFun Func, bool NA, typename VEC>
47 class Vectorized_INTSXP : public VectorBase<REALSXP, NA, Vectorized_INTSXP<Func,NA,VEC> >{
48 public:
51 
52  Vectorized_INTSXP( const VEC_TYPE& object_) : object( object_.get_ref() ){}
53  inline double operator[]( R_xlen_t i) const {
54  int x = object[i] ;
55  if( x == NA_INTEGER ) return NA_REAL ;
56  return Func( x ) ;
57  }
58  inline R_xlen_t size() const { return object.size(); }
59 
60 private:
61  const VEC_EXT& object ;
62 } ;
63 template <DDFun Func, typename VEC>
64 class Vectorized_INTSXP<Func,false,VEC> :
65  public VectorBase<REALSXP,false, Vectorized_INTSXP<Func,false,VEC> >{
66 public:
69 
70  Vectorized_INTSXP( const VEC_TYPE& object_) : object( object_.get_ref() ){}
71  inline double operator[]( R_xlen_t i) const {
72  return Func( object[i] ) ;
73  }
74  inline R_xlen_t size() const { return object.size(); }
75 
76 private:
77  const VEC_EXT& object ;
78 } ;
79 
80 } // sugar
81 } // Rcpp
82 
83 #define VECTORIZED_MATH_1(__NAME__,__SYMBOL__) \
84 namespace Rcpp{ \
85  template <bool NA, typename T> \
86  inline sugar::Vectorized<__SYMBOL__,NA,T> \
87  __NAME__( const VectorBase<REALSXP,NA,T>& t ){ \
88  return sugar::Vectorized<__SYMBOL__,NA,T>( t ) ; \
89  } \
90  inline sugar::Vectorized<__SYMBOL__,true,NumericVector> \
91  __NAME__( SEXP x){ return __NAME__( NumericVector( x ) ) ; } \
92  template <bool NA, typename T> \
93  inline sugar::Vectorized_INTSXP<__SYMBOL__,NA,T> \
94  __NAME__( const VectorBase<INTSXP,NA,T>& t ){ \
95  return sugar::Vectorized_INTSXP<__SYMBOL__,NA,T>( t ) ; \
96  } \
97 }
98 
99 
100 #endif
Rcpp::VectorBase< INTSXP, false, VEC > VEC_TYPE
Rcpp::traits::Extractor< INTSXP, false, VEC >::type VEC_EXT
Vectorized_INTSXP(const VEC_TYPE &object_)
Rcpp::traits::Extractor< INTSXP, NA, VEC >::type VEC_EXT
double operator[](R_xlen_t i) const
Rcpp::VectorBase< INTSXP, NA, VEC > VEC_TYPE
Rcpp::traits::Extractor< REALSXP, NA, VEC >::type VEC_EXT
const VEC_EXT & object
Vectorized(const VEC_TYPE &object_)
R_xlen_t size() const
double operator[](R_xlen_t i) const
Rcpp::VectorBase< REALSXP, NA, VEC > VEC_TYPE
double(* DDFun)(double)
Rcpp API.
Definition: algo.h:28