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