Rcpp Version 1.0.14
Loading...
Searching...
No Matches
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
25namespace Rcpp{
26namespace sugar{
27
28extern "C" typedef double (*DDFun)(double);
29
30template <DDFun Func, bool NA, typename VEC>
31class Vectorized : public VectorBase<REALSXP, NA, Vectorized<Func,NA,VEC> >{
32public:
35
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
42private:
43 const VEC_EXT& object ;
44} ;
45
46template <DDFun Func, bool NA, typename VEC>
47class Vectorized_INTSXP : public VectorBase<REALSXP, NA, Vectorized_INTSXP<Func,NA,VEC> >{
48public:
51
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
60private:
61 const VEC_EXT& object ;
62} ;
63template <DDFun Func, typename VEC>
65 public VectorBase<REALSXP,false, Vectorized_INTSXP<Func,false,VEC> >{
66public:
69
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
76private:
77 const VEC_EXT& object ;
78} ;
79
80} // sugar
81} // Rcpp
82
83#define VECTORIZED_MATH_1(__NAME__,__SYMBOL__) \
84namespace 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
Vectorized(const VEC_TYPE &object_)
double operator[](R_xlen_t i) const
Rcpp::VectorBase< REALSXP, NA, VEC > VEC_TYPE
double(* DDFun)(double)
Rcpp API.
Definition algo.h:28
T as(SEXP x)
Definition as.h:151