OpenMS  2.5.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-2020.
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>
38 
39 #include <boost/math/special_functions/gamma.hpp>
40 #include <cmath>
41 #include <utility>
42 
43 namespace OpenMS
44 {
52  namespace Math
53  {
65  inline static double ceilDecimal(double x, int decPow)
66  {
67  return (ceil(x / pow(10.0, decPow))) * pow(10.0, decPow); // decimal shift right, ceiling, decimal shift left
68  }
69 
80  inline static double roundDecimal(double x, int decPow)
81  {
82  if (x > 0)
83  return (floor(0.5 + x / pow(10.0, decPow))) * pow(10.0, decPow);
84 
85  return -((floor(0.5 + fabs(x) / pow(10.0, decPow))) * pow(10.0, decPow));
86  }
87 
93  inline static double intervalTransformation(double x, double left1, double right1, double left2, double right2)
94  {
95  return left2 + (x - left1) * (right2 - left2) / (right1 - left1);
96  }
97 
105  inline double linear2log(double x)
106  {
107  return log10(x + 1); //+1 to avoid negative logarithms
108  }
109 
117  inline double log2linear(double x)
118  {
119  return pow(10, x) - 1;
120  }
121 
127  inline bool isOdd(UInt x)
128  {
129  return (x & 1) != 0;
130  }
131 
137  template <typename T>
138  T round(T x)
139  {
140  if (x >= T(0))
141  {
142  return T(floor(x + T(0.5)));
143  }
144  else
145  {
146  return T(ceil(x - T(0.5)));
147  }
148  }
149 
155  inline static bool approximatelyEqual(double a, double b, double tol)
156  {
157  return std::fabs(a - b) <= tol;
158  }
159 
168  template <typename T>
169  T gcd(T a, T b)
170  {
171  T c;
172  while (b != 0)
173  {
174  c = a % b;
175  a = b;
176  b = c;
177  }
178  return a;
179  }
180 
193  template <typename T>
194  T gcd(T a, T b, T & u1, T & u2)
195  {
196  u1 = 1;
197  u2 = 0;
198  T u3 = a;
199 
200  T v1 = 0;
201  T v2 = 1;
202  T v3 = b;
203 
204  while (v3 != 0)
205  {
206  T q = u3 / v3;
207  T t1 = u1 - v1 * q;
208  T t2 = u2 - v2 * q;
209  T t3 = u3 - v3 * q;
210 
211  u1 = v1;
212  u2 = v2;
213  u3 = v3;
214 
215  v1 = t1;
216  v2 = t2;
217  v3 = t3;
218  }
219 
220  return u3;
221  }
222 
232  template <typename T>
233  T getPPM(T mz_obs, T mz_ref)
234  {
235  return (mz_obs - mz_ref) / mz_ref * 1e6;
236  }
237 
247  template <typename T>
248  T getPPMAbs(T mz_obs, T mz_ref)
249  {
250  return std::fabs(getPPM(mz_obs, mz_ref));
251  }
252 
262  template <typename T>
263  T ppmToMass(T ppm, T mz_ref)
264  {
265  return (ppm / 1e6) * mz_ref;
266  }
267 
268  /*
269  @brief Compute the absolute mass diff in [Th], given a ppm value and a reference point.
270 
271  The returned mass diff is always positive!
272 
273  @param ppm Parts-per-million error
274  @param mz_ref Reference m/z
275  @return The absolute mass diff in [Th]
276  */
277  template <typename T>
278  T ppmToMassAbs(T ppm, T mz_ref)
279  {
280  return std::fabs(ppmToMass(ppm, mz_ref));
281  }
282 
296  inline static std::pair<double, double> getTolWindow(double val, double tol, bool ppm)
297  {
298  double left, right;
299 
300  if (ppm)
301  {
302  left = val - val * tol * 1e-6;
303  right = val / (1.0 - tol * 1e-6);
304  }
305  else
306  {
307  left = val - tol;
308  right = val + tol;
309  }
310 
311  return std::make_pair(left, right);
312  }
313 
322  inline double factLn(UInt x)
323  {
324  return lgamma(double(x+1));
325  }
326 
327  } // namespace Math
328 } // namespace OpenMS
329 
Types.h
OpenMS::Math::approximatelyEqual
static bool approximatelyEqual(double a, double b, double tol)
Returns if a is approximately equal b , allowing a tolerance of tol.
Definition: MathFunctions.h:155
OpenMS::Math::isOdd
bool isOdd(UInt x)
Returns true if the given integer is odd.
Definition: MathFunctions.h:127
OpenMS::Constants::c
const double c
OpenMS::Math::getTolWindow
static std::pair< double, double > getTolWindow(double val, double tol, bool ppm)
Return tolerance window around val given tolerance tol.
Definition: MathFunctions.h:296
OpenMS::Math::getPPMAbs
T getPPMAbs(T mz_obs, T mz_ref)
Compute absolute parts-per-million of two m/z values.
Definition: MathFunctions.h:248
OpenMS::Math::log2linear
double log2linear(double x)
Transforms a number from log10 to to linear scale. Subtracts the 1 added by linear2log(double)
Definition: MathFunctions.h:117
OpenMS
Main OpenMS namespace.
Definition: FeatureDeconvolution.h:46
OpenMS::Math::ppmToMassAbs
T ppmToMassAbs(T ppm, T mz_ref)
Definition: MathFunctions.h:278
OpenMS::Math::intervalTransformation
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:93
OpenMS::Math::getPPM
T getPPM(T mz_obs, T mz_ref)
Compute parts-per-million of two m/z values.
Definition: MathFunctions.h:233
OpenMS::Math::round
T round(T x)
Rounds the value.
Definition: MathFunctions.h:138
OpenMS::Math::gcd
T gcd(T a, T b)
Returns the greatest common divisor (gcd) of two numbers by applying the Euclidean algorithm.
Definition: MathFunctions.h:169
OpenMS::Math::ceilDecimal
static double ceilDecimal(double x, int decPow)
rounds x up to the next decimal power 10 ^ decPow
Definition: MathFunctions.h:65
OpenMS::UInt
unsigned int UInt
Unsigned integer type.
Definition: Types.h:94
OpenMS::Math::linear2log
double linear2log(double x)
Transforms a number from linear to log10 scale. Avoids negative logarithms by adding 1.
Definition: MathFunctions.h:105
OpenMS::Math::ppmToMass
T ppmToMass(T ppm, T mz_ref)
Compute the mass diff in [Th], given a ppm value and a reference point.
Definition: MathFunctions.h:263
OpenMS::Math::roundDecimal
static double roundDecimal(double x, int decPow)
rounds x to the next decimal power 10 ^ decPow
Definition: MathFunctions.h:80
OpenMS::Math::factLn
double factLn(UInt x)
Return the ln(x!) of a value.
Definition: MathFunctions.h:322