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-2015.
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: Stephan Aiche$
32 // $Authors: Marc Sturm $
33 // --------------------------------------------------------------------------
34 
35 #ifndef OPENMS_MATH_MISC_MATHFUNCTIONS_H
36 #define OPENMS_MATH_MISC_MATHFUNCTIONS_H
37 
38 #include <OpenMS/CONCEPT/Types.h>
39 
40 #include <cmath>
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 
222  } // namespace Math
223 } // namespace OpenMS
224 
225 #endif // OPENMS_MATH_MISC_MATHFUNCTIONS_H
bool isOdd(UInt x)
Returns true if the given integer is odd.
Definition: MathFunctions.h:126
const double c
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: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 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

OpenMS / TOPP release 2.0.0 Documentation generated on Tue Nov 1 2016 16:34:46 using doxygen 1.8.11