OpenMS  2.7.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-2021.
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/math/special_functions/gamma.hpp>
41 #include <boost/random/mersenne_twister.hpp>
42 #include <boost/random/uniform_int.hpp>
43 #include <cmath>
44 #include <utility>
45 
46 namespace OpenMS
47 {
55  namespace Math
56  {
68  inline static double ceilDecimal(double x, int decPow)
69  {
70  return (ceil(x / pow(10.0, decPow))) * pow(10.0, decPow); // decimal shift right, ceiling, decimal shift left
71  }
72 
83  inline static double roundDecimal(double x, int decPow)
84  {
85  if (x > 0)
86  return (floor(0.5 + x / pow(10.0, decPow))) * pow(10.0, decPow);
87 
88  return -((floor(0.5 + fabs(x) / pow(10.0, decPow))) * pow(10.0, decPow));
89  }
90 
96  inline static double intervalTransformation(double x, double left1, double right1, double left2, double right2)
97  {
98  return left2 + (x - left1) * (right2 - left2) / (right1 - left1);
99  }
100 
108  inline double linear2log(double x)
109  {
110  return log10(x + 1); //+1 to avoid negative logarithms
111  }
112 
120  inline double log2linear(double x)
121  {
122  return pow(10, x) - 1;
123  }
124 
130  inline bool isOdd(UInt x)
131  {
132  return (x & 1) != 0;
133  }
134 
140  template <typename T>
141  T round(T x)
142  {
143  if (x >= T(0))
144  {
145  return T(floor(x + T(0.5)));
146  }
147  else
148  {
149  return T(ceil(x - T(0.5)));
150  }
151  }
152 
158  inline static bool approximatelyEqual(double a, double b, double tol)
159  {
160  return std::fabs(a - b) <= tol;
161  }
162 
171  template <typename T>
172  T gcd(T a, T b)
173  {
174  T c;
175  while (b != 0)
176  {
177  c = a % b;
178  a = b;
179  b = c;
180  }
181  return a;
182  }
183 
196  template <typename T>
197  T gcd(T a, T b, T & u1, T & u2)
198  {
199  u1 = 1;
200  u2 = 0;
201  T u3 = a;
202 
203  T v1 = 0;
204  T v2 = 1;
205  T v3 = b;
206 
207  while (v3 != 0)
208  {
209  T q = u3 / v3;
210  T t1 = u1 - v1 * q;
211  T t2 = u2 - v2 * q;
212  T t3 = u3 - v3 * q;
213 
214  u1 = v1;
215  u2 = v2;
216  u3 = v3;
217 
218  v1 = t1;
219  v2 = t2;
220  v3 = t3;
221  }
222 
223  return u3;
224  }
225 
235  template <typename T>
236  T getPPM(T mz_obs, T mz_ref)
237  {
238  return (mz_obs - mz_ref) / mz_ref * 1e6;
239  }
240 
250  template <typename T>
251  T getPPMAbs(T mz_obs, T mz_ref)
252  {
253  return std::fabs(getPPM(mz_obs, mz_ref));
254  }
255 
265  template <typename T>
266  T ppmToMass(T ppm, T mz_ref)
267  {
268  return (ppm / 1e6) * mz_ref;
269  }
270 
271  /*
272  @brief Compute the absolute mass diff in [Th], given a ppm value and a reference point.
273 
274  The returned mass diff is always positive!
275 
276  @param ppm Parts-per-million error
277  @param mz_ref Reference m/z
278  @return The absolute mass diff in [Th]
279  */
280  template <typename T>
281  T ppmToMassAbs(T ppm, T mz_ref)
282  {
283  return std::fabs(ppmToMass(ppm, mz_ref));
284  }
285 
299  inline static std::pair<double, double> getTolWindow(double val, double tol, bool ppm)
300  {
301  double left, right;
302 
303  if (ppm)
304  {
305  left = val - val * tol * 1e-6;
306  right = val / (1.0 - tol * 1e-6);
307  }
308  else
309  {
310  left = val - tol;
311  right = val + tol;
312  }
313 
314  return std::make_pair(left, right);
315  }
316 
325  inline double factLn(UInt x)
326  {
327  return lgamma(double(x+1));
328  }
329 
333  template <typename T1> typename T1::value_type quantile(const T1 &x, double q)
334  {
335  if (x.empty()) throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION,
336  "Quantile requested from empty container.");
337  if (q < 0.0) q = 0.;
338  if (q > 1.0) q = 1.;
339 
340  const auto n = x.size();
341  const auto id = std::max(0., n * q - 1); // -1 for c++ index starting at 0
342  const auto lo = floor(id);
343  const auto hi = ceil(id);
344  const auto qs = x[lo];
345  const auto h = (id - lo);
346 
347  return (1.0 - h) * qs + h * x[hi];
348  }
349 
350  // portable random shuffle
351  class OPENMS_DLLAPI RandomShuffler
352  {
353  public:
354  explicit RandomShuffler(int seed):
355  rng_(boost::mt19937_64(seed))
356  {}
357 
358  explicit RandomShuffler(const boost::mt19937_64& mt_rng):
359  rng_(mt_rng)
360  {}
361 
362  RandomShuffler() = default;
363  ~RandomShuffler() = default;
364 
365  boost::mt19937_64 rng_;
366  template <class RandomAccessIterator>
367  void portable_random_shuffle (RandomAccessIterator first, RandomAccessIterator last)
368  {
369  for (auto i = (last-first)-1; i > 0; --i) // OMS_CODING_TEST_EXCLUDE
370  {
371  boost::uniform_int<decltype(i)> d(0, i);
372  std::swap(first[i], first[d(rng_)]);
373  }
374  }
375 
376 
377  void seed(uint64_t val)
378  {
379  rng_.seed(val);
380  }
381  };
382  } // namespace Math
383 } // namespace OpenMS
384 
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition: Exception.h:341
Definition: MathFunctions.h:352
RandomShuffler(int seed)
Definition: MathFunctions.h:354
boost::mt19937_64 rng_
Definition: MathFunctions.h:365
void seed(uint64_t val)
Definition: MathFunctions.h:377
RandomShuffler(const boost::mt19937_64 &mt_rng)
Definition: MathFunctions.h:358
void portable_random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
Definition: MathFunctions.h:367
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
T gcd(T a, T b)
Returns the greatest common divisor (gcd) of two numbers by applying the Euclidean algorithm.
Definition: MathFunctions.h:172
static double roundDecimal(double x, int decPow)
rounds x to the next decimal power 10 ^ decPow
Definition: MathFunctions.h:83
double linear2log(double x)
Transforms a number from linear to log10 scale. Avoids negative logarithms by adding 1.
Definition: MathFunctions.h:108
T round(T x)
Rounds the value.
Definition: MathFunctions.h:141
double log2linear(double x)
Transforms a number from log10 to to linear scale. Subtracts the 1 added by linear2log(double)
Definition: MathFunctions.h:120
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:96
static double ceilDecimal(double x, int decPow)
rounds x up to the next decimal power 10 ^ decPow
Definition: MathFunctions.h:68
static bool approximatelyEqual(double a, double b, double tol)
Returns if a is approximately equal b , allowing a tolerance of tol.
Definition: MathFunctions.h:158
bool isOdd(UInt x)
Returns true if the given integer is odd.
Definition: MathFunctions.h:130
const double c
Definition: Constants.h:209
const double h
Definition: Constants.h:162
T getPPM(T mz_obs, T mz_ref)
Compute parts-per-million of two m/z values.
Definition: MathFunctions.h:236
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:333
static std::pair< double, double > getTolWindow(double val, double tol, bool ppm)
Return tolerance window around val given tolerance tol.
Definition: MathFunctions.h:299
T ppmToMass(T ppm, T mz_ref)
Compute the mass diff in [Th], given a ppm value and a reference point.
Definition: MathFunctions.h:266
double factLn(UInt x)
Return the ln(x!) of a value.
Definition: MathFunctions.h:325
T getPPMAbs(T mz_obs, T mz_ref)
Compute absolute parts-per-million of two m/z values.
Definition: MathFunctions.h:251
T ppmToMassAbs(T ppm, T mz_ref)
Definition: MathFunctions.h:281
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:47