ProteoWizard
base.hpp
Go to the documentation of this file.
1 //
2 // $Id: base.hpp 7297 2015-03-12 05:30:33Z paragmallick $
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 BASE_H
23 #define BASE_H
24 
25 #include <math.h>
26 #include <algorithm>
27 #include <vector>
28 #include <functional>
29 #include <numeric>
30 #include <stdexcept>
31 #include <limits>
32 #include <iterator>
33 #include <cmath>
34 #include <string>
35 #include <boost/utility/enable_if.hpp>
36 #include <boost/type_traits/is_integral.hpp>
38 
39 namespace ralab
40 {
41  namespace base
42  {
43  namespace base
44  {
45 
46  /// generates the sequence from, from+/-1, ..., to (identical to from:to).
47  template<typename TReal>
48  void seq
49  (
50  TReal from, //!<[in] the starting value of the sequence
51  TReal to, //!<[in] the end value of the sequence
52  std::vector<TReal> &result //!<[out] result sequence
53  )
54  {
55  result.clear();
56  typedef typename std::vector<TReal>::size_type size_type;
57  if( from <= to )
58  {
59  size_type length = static_cast<size_type>(to - from) + 1;
60  result.resize(length);
61  std::generate(result.begin() , result.end() , utilities::SeqPlus<TReal>(from));
62  }
63  else
64  {
65  size_type length = static_cast<size_type>(from - to) + 1 ;
66  result.resize(length);
67  std::generate(result.begin() , result.end() , utilities::SeqMinus<TReal>(from));
68  }
69  }
70 
71  /// generates sequence: from, from+by, from + 2*by, ..., up to a sequence value less than, or equal than to.
72  /// Specifying to < from and by of positive sign is an error.
73  template<typename TReal>
74  void seq(
75  TReal from,//!<[in] the starting value of the sequence
76  TReal to,//!<[in] the end value of the sequence
77  TReal by, //!<[in] number: increment of the sequence
78  std::vector<TReal> &result //!<[out] result sequence
79  )
80  {
81  result.clear();
82  typedef typename std::vector<TReal>::size_type size_type;
83  size_type size = static_cast<size_type>( (to - from) / by ) + 1u ;
84  result.reserve( size );
85 
86  if(from <= to)
87  {
88  if(!(by > 0)){
89  throw std::logic_error(std::string( "by > 0" ));
90  }
91  for(; from <= to; from += by)
92  {
93  result.push_back(from);
94  }
95  }
96  else
97  {
98  if(! (by < 0) ){
99  throw std::logic_error(std::string( "by < 0" ));
100  }
101  for(; from >= to; from += by)
102  {
103  result.push_back(from);
104  }
105  }
106  }
107 
108  /// generates sequence: from, to of length
109  /// calls seq with \$[ by = ( ( to - from ) / ( length - 1. ) ) \$]
110  template<typename TReal>
112  TReal from,//!<[in] the starting value of the sequence
113  TReal to,//!<[in] the end value of the sequence
114  unsigned int length, //!<[in] length of sequence
115  std::vector<TReal> &result //!<[out] result sequence
116  )
117  {
118  TReal by = ( ( to - from ) / ( static_cast<TReal>( length ) - 1. ) );
119  seq(from, to, by, result);
120 
121  //this is required because of machine precision...
122  // sometimes by does not add's up to precisely _to_ nad
123  if(result.size() < length)
124  {
125  result.push_back(result[result.size()-1] + by );
126  }
127  else
128  {
129  result.resize(length);
130  }
131  }
132 
133  /// generates the sequence [1, 2, ..., length(ref)]
134  /// (as if argument along.with had been specified),
135  /// unless the argument is numeric of length 1 when it is interpreted as
136  /// 1:from (even for seq(0) for compatibility with S).
137 
138  template< typename T1 , typename T2 >
139  void seq
140  (
141  std::vector<T1> & ref, //!<[in] take the length from the length of this argument.
142  std::vector<T2> & res //!<[out] result sequence
143  )
144  {
145  T2 t(1);
146  res.assign(ref.size() , t );
147  std::partial_sum( res.begin() , res.end() , res.begin() );
148  }
149 
150  /// Generates Sequence 1,2,3,....length .
151  /// Generates 1, 2, ..., length unless length.out = 0, when it generates
152  /// integer(0).
153 
154  template<typename TSize, typename TReal>
155  typename boost::enable_if<boost::is_integral<TSize>, void>::type
157  TSize length, //!< [in] length of sequence
158  std::vector<TReal> &res //!< [out] result sequence
159  )
160  {
161  TReal t(1);
162  res.assign(length , t );
163  std::partial_sum(res.begin(),res.end(),res.begin());
164  }
165 
166 
167  /// MEAN Trimmed arithmetic mean.
168 
169  /// ## Default S3 method:
170  /// mean(x, trim = 0, na.rm = FALSE, ...)
171 
172  /// Arguments
173  /// x An R object. Currently there are methods for numeric data frames, numeric vectors and dates. A complex vector is allowed for trim = 0, only.
174  /// trim the fraction (0 to 0.5) of observations to be trimmed from each end of x before the mean is computed. Values outside that range are taken as the nearest endpoint.
175  /// na.rm a logical value indicating whether NA values should be stripped before the computation proceeds.
176  /// ... further arguments passed to or from other methods.
177 
178  /// Value
179  /// For a data frame, a named vector with the appropriate method being applied column by column.
180  /// If trim is zero (the default), the arithmetic mean of the values in x is computed, as a numeric or complex vector of length one. If x is not logical (coerced to numeric), integer, numeric or complex, NA is returned, with a warning.
181  /// If trim is non-zero, a symmetrically trimmed mean is computed with a fraction of trim observations deleted from each end before the mean is computed.
182 
183 
184  template < typename InputIterator >
185  inline
186  typename std::iterator_traits<InputIterator>::value_type
188  InputIterator begin, //!< [in]
189  InputIterator end //!< [in]
190  )
191  {
192  typedef typename std::iterator_traits<InputIterator>::value_type TReal;
193  TReal size = static_cast<TReal>(std::distance(begin,end));
194  TReal sum = std::accumulate(begin , end, 0. );
195  return(sum/size);
196  }
197 
198  /// mean
199  template <typename TReal>
200  inline TReal mean(const std::vector<TReal> & x )
201  {
202  TReal size = static_cast<TReal>(x.size());
203  TReal sum = std::accumulate(x.begin() , x.end(), 0. );
204  return ( sum / size ) ;
205  }
206 
207  /// mean
208  template <typename TReal>
209  TReal mean(
210  const std::vector<TReal> & x, //!< [in]
211  TReal trim //!< [in] trim 0 - 0.5
212  )
213  {
214  if(trim >= 0.5)
215  {
216  trim = 0.4999999;
217  }
218  TReal size = static_cast<TReal>(x.size());
219  std::vector<TReal> wc(x); //working copy
220  std::sort(wc.begin(),wc.end());
221  size_t nrelemstrim = static_cast<size_t>(round( size * trim )) ;
222  size_t nrelems = std::distance(wc.begin() + nrelemstrim, wc.end() - nrelemstrim );
223  TReal sum = std::accumulate(wc.begin() + nrelemstrim , wc.end() - nrelemstrim, 0. );
224  return ( sum / static_cast<TReal>( nrelems ) ); //static_cast will be required with boost::math::round
225  }
226 
227  /// computes the mean
228  template<class Iter_T>
229  typename std::iterator_traits<Iter_T>::value_type geometricMean(Iter_T first, Iter_T last)
230  {
231  typedef typename std::iterator_traits<Iter_T>::value_type TReal;
232  size_t cnt = distance(first, last);
233  std::vector<TReal> copyOfInput(first, last);
234 
235  // ln(x)
236  typename std::vector<TReal>::iterator inputIt;
237 
238  for(inputIt = copyOfInput.begin(); inputIt != copyOfInput.end() ; ++inputIt)
239  {
240  *inputIt = std::log(*inputIt);
241  }
242 
243  // sum(ln(x))
244  TReal sum( std::accumulate(copyOfInput.begin(), copyOfInput.end(), TReal() ));
245 
246  // e^(sum(ln(x))/N)
247  TReal geomean( std::exp(sum / cnt) );
248  return geomean;
249  }
250 
251 
252  /// Range of Values
253  /// range returns a std::pair containing minimum and maximum of all the given values.
254  template<typename TReal>
255  void Range(
256  const std::vector<TReal> & values, //!< [in] data
257  std::pair<TReal,TReal> & range //!< [out] range
258  )
259  {
260  TReal min = * std::min_element(values.begin(),values.end());
261  TReal max = * std::max_element(values.begin(),values.end());
262  range.first = min;
263  range.second = max;
264  }
265 
266  /// maximum of 3 numbers
267  template<typename T>
268  inline double max3(T a, T b, T c)
269  {
270  T max;
271  if(a>b)
272  max=a;
273  else
274  max=b;
275  if(max<c)
276  max=c;
277  return(max);
278  }
279 
280  /// log base 2
281  template<typename TReal>
282  inline TReal log2(TReal test)
283  {
284  if(test==0)
285  return(0);
286  else
287  return( log10( test ) / log10( static_cast<TReal>(2.) ));
288  }
289 
290  }//namespace BASE ends here
291  }//namespace base ends here
292 }//namespace ralab ends here
293 
294 #endif // BASE_H
double max3(T a, T b, T c)
maximum of 3 numbers
Definition: base.hpp:268
std::iterator_traits< InputIterator >::value_type mean(InputIterator begin, InputIterator end)
MEAN Trimmed arithmetic mean.
Definition: base.hpp:187
void seq(TReal from, TReal to, std::vector< TReal > &result)
generates the sequence from, from+/-1, ..., to (identical to from:to).
Definition: base.hpp:49
void test()
void Range(const std::vector< TReal > &values, std::pair< TReal, TReal > &range)
Range of Values range returns a std::pair containing minimum and maximum of all the given values...
Definition: base.hpp:255
void seq_length(TReal from, TReal to, unsigned int length, std::vector< TReal > &result)
generates sequence: from, to of length calls seq with $[ by = ( ( to - from ) / ( length - 1...
Definition: base.hpp:111
TReal log2(TReal test)
log base 2
Definition: base.hpp:282
EQUISPACEINTERPOL Interpolation on a equidistantly spaced grid.
Definition: base.hpp:39
std::iterator_traits< Iter_T >::value_type geometricMean(Iter_T first, Iter_T last)
computes the mean
Definition: base.hpp:229
KernelTraitsBase< Kernel >::space_type::abscissa_type x