ProteoWizard
cumsum.hpp
Go to the documentation of this file.
1//
2// $Id$
3//
4//
5// Original author: Witold Wolski <wewolski@gmail.com>
6//
7// Copyright : ETH Zurich
8//
9// Licensed under the Apache License, Version 2.0 (the "License");
10// you may not use this file except in compliance with the License.
11// You may obtain a copy of the License at
12//
13// http://www.apache.org/licenses/LICENSE-2.0
14//
15// Unless required by applicable law or agreed to in writing, software
16// distributed under the License is distributed on an "AS IS" BASIS,
17// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18// See the License for the specific language governing permissions and
19// limitations under the License.
20//
21
22
23#ifndef CUMSUM_H
24#define CUMSUM_H
25#include <vector>
26#include <cstddef>
27/*! \file cumsum.h
28\brief functions for package base: cumsum, cumprod, cummax, cummin
29
30*/
31
32namespace ralab
33{
34 namespace base
35 {
36
37 /*! CUMSUM Returns a vector whose elements are the cumulative sums, products, minima or maxima of the elements of the argument.*/
38
39 template<typename TIterator, typename T>
40 void cumsum(
41 TIterator beg,//!< [in] vector of values of type T
42 TIterator end,
43 std::vector<T> & res //!<[out] cumulative sum
44 )
45 {
46 if(beg!=end){
47
48 res.assign(beg,end);
49 typename std::vector<T>::iterator begRes = res.begin();
50 typename std::vector<T>::iterator begResDelayed = begRes;
51 ++begRes;
52
53 typename std::vector<T>::iterator begEnd = res.end();
54 for( ;begRes != begEnd ; ++begRes, ++begResDelayed)
55 {
56 *begRes += *(begResDelayed) ;
57 }
58 }
59 }
60
61 //in place version of cumulative sum.
62 template<typename TIterator>
63 TIterator cumsum(
64 TIterator beg,//!< [in] vector of values of type T
65 TIterator end //<! [in] end iterator of sequence
66 )
67 {
68 TIterator begRes = beg;
69 ++begRes;
70 for( ;begRes != end ; ++begRes, ++beg)
71 {
72 *begRes += *(beg) ;
73 }
74 return begRes;
75 }
76
77
78 /*! \brief Returns a vector whose elements are the cumulative products of the elements of the argument. */
79 template<typename T>
80 void cumprod(std::vector<T> & x,//!< [in]
81 std::vector<T> & res //!< cumulative product
82 )
83 {
84 res.resize(x.size());
85 for(size_t i = 0; i < x.size() ; ++i)
86 {
87 res[i] = x[i];
88 }
89 for(size_t i = 1; i < x.size(); ++i)
90 {
91 res[i] *= res[i-1] ;
92 }
93 }
94
95 /*! \brief Returns a vector whose elements are the cumulative maximum of the elements of the argument. */
96 template<typename T>
97 void cummax(
98 std::vector<T> & x, //!<[in] vector of type T
99 std::vector<T> & res //!<[out] cumulative maximum
100 )
101 {
102 res.resize(x.size());
103 for(size_t i = 0; i < x.size() ; ++i)
104 {
105 res[i] = x[i];
106 }
107 for(size_t i = 1; i < x.size(); ++i)
108 {
109 res[i] = std::max( res[i-1] , res[i]) ;
110 }
111 }
112
113 /*! \brief Returns a vector whose elements are the cumulative sums, products, minima or maxima of the elements of the argument. */
114 template<typename T>
115 void cummin(
116 std::vector<T> & x, //!<[in] vector of type T
117 std::vector<T> & res //!<[in] cumulative minimum
118 )
119 {
120 res.resize(x.size());
121 for(size_t i = 0; i < x.size() ; ++i)
122 {
123 res[i] = x[i];
124 }
125 for(size_t i = 1; i < x.size(); ++i)
126 {
127 res[i] = std::min( res[i-1] , res[i]) ;
128 }
129 }
130
131 }// end base
132}//end namespace ralab
133
134#endif // CUMSUM_H
KernelTraitsBase< Kernel >::space_type::abscissa_type x
void cumprod(std::vector< T > &x, std::vector< T > &res)
Returns a vector whose elements are the cumulative products of the elements of the argument.
Definition cumsum.hpp:80
void cumsum(TIterator beg, TIterator end, std::vector< T > &res)
Definition cumsum.hpp:40
void cummax(std::vector< T > &x, std::vector< T > &res)
Returns a vector whose elements are the cumulative maximum of the elements of the argument.
Definition cumsum.hpp:97
void cummin(std::vector< T > &x, std::vector< T > &res)
Returns a vector whose elements are the cumulative sums, products, minima or maxima of the elements o...
Definition cumsum.hpp:115
EQUISPACEINTERPOL Interpolation on a equidistantly spaced grid.
Definition base.hpp:40