ProteoWizard
diff.hpp
Go to the documentation of this file.
1 //
2 // $Id: diff.hpp 5313 2013-12-17 18:06:54Z chambm $
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 #ifndef DIFF_H
23 #define DIFF_H
24 
25 #include <math.h>
26 #include <algorithm>
27 #include <vector>
28 #include <functional>
29 #include <numeric>
30 #include <assert.h>
31 
32 namespace ralab
33 {
34  namespace base
35  {
36  namespace base
37  {
38  /*! DIFF Lagged and iterated differences.
39 
40  for more detials see R::base::diff <br>
41  diff(x, ...) <br>
42  ## Default S3 method: <br>
43  diff(x, lag = 1, differences = 1, ...) <br>
44 
45 
46  */
47  /*! \brief lagged differences
48 
49  \return .end() Iterator in destination container.
50 
51  */
52  template <
53  typename InputIterator,
54  typename OutputIterator,
55  typename TN // = int32_t
56  >
57  OutputIterator diff
58  (
59  InputIterator begin, //!< [in] begin
60  InputIterator end, //!< [in] end
61  OutputIterator destBegin, //!< [out] dest begin
62  TN lag//!< [in] an integer indicating which lag to use.
63  )
64  {
65  typedef typename InputIterator::value_type vtype;
66  return( std::transform(begin + lag
67  , end
68  , begin
69  , destBegin
70  , std::minus< vtype >())
71  );
72  }
73 
74  /*! \brief lagged difference
75 
76  The result of the computation is performed in place!
77  \return - .end() in result container.
78  */
79  template <typename InputIterator,
80  typename TN// = int32_t
81  >
82  InputIterator diff
83  (
84  InputIterator begin, //!< begin
85  InputIterator end, //!< end
86  TN lag, //!< An integer indicating which lag to use.
87  TN differences //!< An integer indicating the order of the difference.
88  )
89  {
90  if(std::distance( begin,end ) <= static_cast<int>(lag * differences) ){
91  return(begin);
92  }
93  else{
94  TN i = TN();
95  InputIterator itmp(end) ;
96  while(differences > i )
97  {
98 
99  itmp = std::transform(
100  begin + lag ,
101  itmp ,
102  begin ,
103  begin ,
104  std::minus<typename InputIterator::value_type >()
105  ) ;
106  ++i ;
107  }
108  return(itmp);
109  }
110  }
111 
112  }//end base
113  }//end ralab
114 }
115 
116 
117 #endif // DIFF_H
OutputIterator diff(InputIterator begin, InputIterator end, OutputIterator destBegin, TN lag)
lagged differences
Definition: diff.hpp:58
EQUISPACEINTERPOL Interpolation on a equidistantly spaced grid.
Definition: base.hpp:39