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-2013.
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 namespace OpenMS
41 {
49  namespace Math
50  {
62  inline static double ceilDecimal(double x, int decPow)
63  {
64  return (ceil(x / pow(10.0, decPow))) * pow(10.0, decPow); // decimal shift right, ceiling, decimal shift left
65  }
66 
77  inline static double roundDecimal(double x, int decPow)
78  {
79  if (x > 0)
80  return (floor(0.5 + x / pow(10.0, decPow))) * pow(10.0, decPow);
81 
82  return -((floor(0.5 + fabs(x) / pow(10.0, decPow))) * pow(10.0, decPow));
83  }
84 
90  inline static double intervalTransformation(double x, double left1, double right1, double left2, double right2)
91  {
92  return left2 + (x - left1) * (right2 - left2) / (right1 - left1);
93  }
94 
102  inline double linear2log(double x)
103  {
104  return log10(x + 1); //+1 to avoid negative logarithms
105  }
106 
114  inline double log2linear(double x)
115  {
116  return pow(10, x) - 1;
117  }
118 
124  inline bool isOdd(UInt x)
125  {
126  return (x & 1) != 0;
127  }
128 
134  template <typename T>
135  T round(T x)
136  {
137  if (x >= T(0))
138  {
139  return T(floor(x + T(0.5)));
140  }
141  else
142  {
143  return T(ceil(x - T(0.5)));
144  }
145  }
146 
152  inline static bool approximatelyEqual(DoubleReal a, DoubleReal b, DoubleReal tol)
153  {
154  return std::fabs(a - b) <= tol;
155  }
156 
165  template <typename T>
166  T gcd(T a, T b)
167  {
168  T c;
169  while (b != 0)
170  {
171  c = a % b;
172  a = b;
173  b = c;
174  }
175  return a;
176  }
177 
190  template <typename T>
191  T gcd(T a, T b, T & u1, T & u2)
192  {
193  u1 = 1;
194  u2 = 0;
195  T u3 = a;
196 
197  T v1 = 0;
198  T v2 = 1;
199  T v3 = b;
200 
201  while (v3 != 0)
202  {
203  T q = u3 / v3;
204  T t1 = u1 - v1 * q;
205  T t2 = u2 - v2 * q;
206  T t3 = u3 - v3 * q;
207 
208  u1 = v1;
209  u2 = v2;
210  u3 = v3;
211 
212  v1 = t1;
213  v2 = t2;
214  v3 = t3;
215  }
216 
217  return u3;
218  }
219 
220  } // namespace Math
221 } // namespace OpenMS
222 
223 #endif // OPENMS_MATH_MISC_MATHFUNCTIONS_H
bool isOdd(UInt x)
Returns true if the given interger is odd.
Definition: MathFunctions.h:124
const double c
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:90
T round(T x)
Rounds the value.
Definition: MathFunctions.h:135
static double ceilDecimal(double x, int decPow)
rounds x up to the next decimal power 10 ^ decPow
Definition: MathFunctions.h:62
T gcd(T a, T b)
Returns the greatest common divisor (gcd) of two numbers by applying the Euclidean algorithm...
Definition: MathFunctions.h:166
double log2linear(double x)
Transforms a number from log10 to to linear scale. Subtracts the 1 added by linear2log(double) ...
Definition: MathFunctions.h:114
static bool approximatelyEqual(DoubleReal a, DoubleReal b, DoubleReal tol)
Returns if a is approximately equal b , allowing a tolerance of tol.
Definition: MathFunctions.h:152
static double roundDecimal(double x, int decPow)
rounds x to the next decimal power 10 ^ decPow
Definition: MathFunctions.h:77
double linear2log(double x)
Transforms a number from linear to log10 scale. Avoids negative logarithms by adding 1...
Definition: MathFunctions.h:102

OpenMS / TOPP release 1.11.1 Documentation generated on Thu Nov 14 2013 11:19:16 using doxygen 1.8.5