Rcpp Version 1.1.2
Loading...
Searching...
No Matches
cumprod.h
Go to the documentation of this file.
1// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4; tab-width: 8 -*-
2//
3// cumsum.h: Rcpp R/C++ interface class library -- cumsum
4//
5// Copyright (C) 2010 - 2025 Dirk Eddelbuettel and Romain Francois
6// Copyright (C) 2026 Dirk Eddelbuettel, Romain Francois and IƱaki Ucar
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__cumprod_h
24#define Rcpp__sugar__cumprod_h
25
26namespace Rcpp{
27namespace sugar{
28
29template <int RTYPE, bool NA, typename T>
30class Cumprod : public Lazy< Rcpp::Vector<RTYPE>, Cumprod<RTYPE, NA, T> > {
31public:
35
36 Cumprod(const VEC_TYPE& object_) : object(object_) {}
37
38 VECTOR get() const {
39 R_xlen_t n = object.size();
41 STORAGE current = object[0];
42
43 if (Rcpp::traits::is_na<RTYPE>(current)) return result;
44 result[0] = current;
45 for (R_xlen_t i = 1; i < n; i++) {
46 current = object[i];
47 if (Rcpp::traits::is_na<RTYPE>(current)) return result;
48 result[i] = RCPP_SAFE_MUL(result[i-1], current);
49 }
50 return result ;
51 }
52private:
54};
55
56} // sugar
57
58
59template <bool NA, typename T>
63
64template <bool NA, typename T>
68
69template <bool NA, typename T>
73
74
75} // Rcpp
76#endif // Rcpp__sugar__cumprod_h
77
R_xlen_t size() const
Definition Vector.h:274
Rcpp::VectorBase< RTYPE, NA, T > VEC_TYPE
Definition cumprod.h:32
const VEC_TYPE & object
Definition cumprod.h:53
Rcpp::traits::storage_type< RTYPE >::type STORAGE
Definition cumprod.h:33
Cumprod(const VEC_TYPE &object_)
Definition cumprod.h:36
Rcpp::Vector< RTYPE > VECTOR
Definition cumprod.h:34
VECTOR get() const
Definition cumprod.h:38
storage_type< RTYPE >::type get_na()
bool is_na(typename storage_type< RTYPE >::type)
Definition is_na.h:32
Rcpp API.
Definition algo.h:28
sugar::Cumprod< INTSXP, NA, T > cumprod(const VectorBase< INTSXP, NA, T > &t)
Definition cumprod.h:60
#define RCPP_SAFE_MUL(a, b)
Definition safe_math.h:35