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

OpenMS / TOPP release 2.3.0 Documentation generated on Wed Apr 18 2018 19:29:06 using doxygen 1.8.14