OpenMS  3.0.0
MathFunctions.h
Go to the documentation of this file.
1 // --------------------------------------------------------------------------
2 // OpenMS -- Open-Source Mass Spectrometry
3 // --------------------------------------------------------------------------
4 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
5 // ETH Zurich, and Freie Universitaet Berlin 2002-2022.
6 //
7 // This software is released under a three-clause BSD license:
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 // * Neither the name of any author or any participating institution
14 // may be used to endorse or promote products derived from this software
15 // without specific prior written permission.
16 // For a full list of authors, refer to the file AUTHORS.
17 // --------------------------------------------------------------------------
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
22 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 //
30 // --------------------------------------------------------------------------
31 // $Maintainer: Timo Sachsenberg$
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #pragma once
36 
37 #include <OpenMS/CONCEPT/Types.h>
39 
40 #include <boost/random/mersenne_twister.hpp>
41 #include <boost/random/uniform_int.hpp>
42 #include <cmath>
43 #include <utility>
44 
45 namespace OpenMS
46 {
54  namespace Math
55  {
56 
65  template<typename T>
66  bool extendRange(T& min, T& max, const T& value)
67  {
68  if (value < min)
69  {
70  min = value;
71  return true;
72  }
73  if (value > max)
74  {
75  max = value;
76  return true;
77  }
78  return false;
79  }
80 
86  template<typename T>
87  bool contains(T value, T min, T max)
88  {
89  return min <= value && value <= max;
90  }
91 
92 
104  inline static double ceilDecimal(double x, int decPow)
105  {
106  return (ceil(x / pow(10.0, decPow))) * pow(10.0, decPow); // decimal shift right, ceiling, decimal shift left
107  }
108 
119  inline static double roundDecimal(double x, int decPow)
120  {
121  if (x > 0)
122  return (floor(0.5 + x / pow(10.0, decPow))) * pow(10.0, decPow);
123 
124  return -((floor(0.5 + fabs(x) / pow(10.0, decPow))) * pow(10.0, decPow));
125  }
126 
132  inline static double intervalTransformation(double x, double left1, double right1, double left2, double right2)
133  {
134  return left2 + (x - left1) * (right2 - left2) / (right1 - left1);
135  }
136 
144  inline double linear2log(double x)
145  {
146  return log10(x + 1); //+1 to avoid negative logarithms
147  }
148 
156  inline double log2linear(double x)
157  {
158  return pow(10, x) - 1;
159  }
160 
166  inline bool isOdd(UInt x)
167  {
168  return (x & 1) != 0;
169  }
170 
176  template <typename T>
177  T round(T x)
178  {
179  if (x >= T(0))
180  {
181  return T(floor(x + T(0.5)));
182  }
183  else
184  {
185  return T(ceil(x - T(0.5)));
186  }
187  }
188 
194  inline static bool approximatelyEqual(double a, double b, double tol)
195  {
196  return std::fabs(a - b) <= tol;
197  }
198 
207  template <typename T>
208  T gcd(T a, T b)
209  {
210  T c;
211  while (b != 0)
212  {
213  c = a % b;
214  a = b;
215  b = c;
216  }
217  return a;
218  }
219 
232  template <typename T>
233  T gcd(T a, T b, T & u1, T & u2)
234  {
235  u1 = 1;
236  u2 = 0;
237  T u3 = a;
238 
239  T v1 = 0;
240  T v2 = 1;
241  T v3 = b;
242 
243  while (v3 != 0)
244  {
245  T q = u3 / v3;
246  T t1 = u1 - v1 * q;
247  T t2 = u2 - v2 * q;
248  T t3 = u3 - v3 * q;
249 
250  u1 = v1;
251  u2 = v2;
252  u3 = v3;
253 
254  v1 = t1;
255  v2 = t2;
256  v3 = t3;
257  }
258 
259  return u3;
260  }
261 
271  template <typename T>
272  T getPPM(T mz_obs, T mz_ref)
273  {
274  return (mz_obs - mz_ref) / mz_ref * 1e6;
275  }
276 
286  template <typename T>
287  T getPPMAbs(T mz_obs, T mz_ref)
288  {
289  return std::fabs(getPPM(mz_obs, mz_ref));
290  }
291 
301  template <typename T>
302  T ppmToMass(T ppm, T mz_ref)
303  {
304  return (ppm / 1e6) * mz_ref;
305  }
306 
307  /*
308  @brief Compute the absolute mass diff in [Th], given a ppm value and a reference point.
309 
310  The returned mass diff is always positive!
311 
312  @param ppm Parts-per-million error
313  @param mz_ref Reference m/z
314  @return The absolute mass diff in [Th]
315  */
316  template <typename T>
317  T ppmToMassAbs(T ppm, T mz_ref)
318  {
319  return std::fabs(ppmToMass(ppm, mz_ref));
320  }
321 
335  inline static std::pair<double, double> getTolWindow(double val, double tol, bool ppm)
336  {
337  double left, right;
338 
339  if (ppm)
340  {
341  left = val - val * tol * 1e-6;
342  right = val / (1.0 - tol * 1e-6);
343  }
344  else
345  {
346  left = val - tol;
347  right = val + tol;
348  }
349 
350  return std::make_pair(left, right);
351  }
352 
356  template <typename T1> typename T1::value_type quantile(const T1 &x, double q)
357  {
358  if (x.empty()) throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
359  "Quantile requested from empty container.");
360  if (q < 0.0) q = 0.;
361  if (q > 1.0) q = 1.;
362 
363  const auto n = x.size();
364  const auto id = std::max(0., n * q - 1); // -1 for c++ index starting at 0
365  const auto lo = floor(id);
366  const auto hi = ceil(id);
367  const auto qs = x[lo];
368  const auto h = (id - lo);
369 
370  return (1.0 - h) * qs + h * x[hi];
371  }
372 
373  // portable random shuffle
374  class OPENMS_DLLAPI RandomShuffler
375  {
376  public:
377  explicit RandomShuffler(int seed):
378  rng_(boost::mt19937_64(seed))
379  {}
380 
381  explicit RandomShuffler(const boost::mt19937_64& mt_rng):
382  rng_(mt_rng)
383  {}
384 
385  RandomShuffler() = default;
386  ~RandomShuffler() = default;
387 
388  boost::mt19937_64 rng_;
389  template <class RandomAccessIterator>
390  void portable_random_shuffle (RandomAccessIterator first, RandomAccessIterator last)
391  {
392  for (auto i = (last-first)-1; i > 0; --i) // OMS_CODING_TEST_EXCLUDE
393  {
394  boost::uniform_int<decltype(i)> d(0, i);
395  std::swap(first[i], first[d(rng_)]);
396  }
397  }
398 
399  void seed(uint64_t val)
400  {
401  rng_.seed(val);
402  }
403  };
404  } // namespace Math
405 } // namespace OpenMS
406 
bool isOdd(UInt x)
Returns true if the given integer is odd.
Definition: MathFunctions.h:166
static std::pair< double, double > getTolWindow(double val, double tol, bool ppm)
Return tolerance window around val given tolerance tol.
Definition: MathFunctions.h:335
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
T getPPM(T mz_obs, T mz_ref)
Compute parts-per-million of two m/z values.
Definition: MathFunctions.h:272
const double c
Definition: Constants.h:209
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47
static double intervalTransformation(double x, double left1, double right1, double left2, double right2)
transforms point x of interval [left1,right1] into interval [left2,right2]
Definition: MathFunctions.h:132
T round(T x)
Rounds the value.
Definition: MathFunctions.h:177
static double ceilDecimal(double x, int decPow)
rounds x up to the next decimal power 10 ^ decPow
Definition: MathFunctions.h:104
void portable_random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
Definition: MathFunctions.h:390
T1::value_type quantile(const T1 &x, double q)
Returns the value of the q th quantile (0-1) in a sorted non-empty vector x.
Definition: MathFunctions.h:356
bool extendRange(T &min, T &max, const T &value)
Given an interval/range and a new value, extend the range to include the new value if needed...
Definition: MathFunctions.h:66
T ppmToMassAbs(T ppm, T mz_ref)
Definition: MathFunctions.h:317
T gcd(T a, T b)
Returns the greatest common divisor (gcd) of two numbers by applying the Euclidean algorithm...
Definition: MathFunctions.h:208
const double h
Definition: Constants.h:162
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition: Exception.h:339
RandomShuffler(const boost::mt19937_64 &mt_rng)
Definition: MathFunctions.h:381
static bool approximatelyEqual(double a, double b, double tol)
Returns if a is approximately equal b , allowing a tolerance of tol.
Definition: MathFunctions.h:194
Definition: MathFunctions.h:374
double log2linear(double x)
Transforms a number from log10 to to linear scale. Subtracts the 1 added by linear2log(double) ...
Definition: MathFunctions.h:156
boost::mt19937_64 rng_
Definition: MathFunctions.h:388
static double roundDecimal(double x, int decPow)
rounds x to the next decimal power 10 ^ decPow
Definition: MathFunctions.h:119
double linear2log(double x)
Transforms a number from linear to log10 scale. Avoids negative logarithms by adding 1...
Definition: MathFunctions.h:144
bool contains(T value, T min, T max)
Is a value contained in [min, max] ?
Definition: MathFunctions.h:87
T getPPMAbs(T mz_obs, T mz_ref)
Compute absolute parts-per-million of two m/z values.
Definition: MathFunctions.h:287
void seed(uint64_t val)
Definition: MathFunctions.h:399
T ppmToMass(T ppm, T mz_ref)
Compute the mass diff in [Th], given a ppm value and a reference point.
Definition: MathFunctions.h:302
RandomShuffler(int seed)
Definition: MathFunctions.h:377