Home  · Classes  · Annotated Classes  · Modules  · Members  · Namespaces  · Related Pages

MathFunctions.h

Go to the documentation of this file.
00001 // --------------------------------------------------------------------------
00002 //                   OpenMS -- Open-Source Mass Spectrometry
00003 // --------------------------------------------------------------------------
00004 // Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
00005 // ETH Zurich, and Freie Universitaet Berlin 2002-2012.
00006 //
00007 // This software is released under a three-clause BSD license:
00008 //  * Redistributions of source code must retain the above copyright
00009 //    notice, this list of conditions and the following disclaimer.
00010 //  * Redistributions in binary form must reproduce the above copyright
00011 //    notice, this list of conditions and the following disclaimer in the
00012 //    documentation and/or other materials provided with the distribution.
00013 //  * Neither the name of any author or any participating institution
00014 //    may be used to endorse or promote products derived from this software
00015 //    without specific prior written permission.
00016 // For a full list of authors, refer to the file AUTHORS.
00017 // --------------------------------------------------------------------------
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00019 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 // ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
00022 // INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00023 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00024 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
00025 // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
00026 // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00027 // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
00028 // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // --------------------------------------------------------------------------
00031 // $Maintainer: Stephan Aiche$
00032 // $Authors: Marc Sturm $
00033 // --------------------------------------------------------------------------
00034 
00035 #ifndef OPENMS_MATH_MISC_MATHFUNCTIONS_H
00036 #define OPENMS_MATH_MISC_MATHFUNCTIONS_H
00037 
00038 #include <OpenMS/CONCEPT/Types.h>
00039 
00040 namespace OpenMS
00041 {
00049   namespace Math
00050   {
00062     inline static double ceilDecimal(double x, int decPow)
00063     {
00064       return (ceil(x / pow(10.0, decPow))) * pow(10.0, decPow); // decimal shift right, ceiling, decimal shift left
00065     }
00066 
00077     inline static double roundDecimal(double x, int decPow)
00078     {
00079       if (x > 0)
00080         return (floor(0.5 + x / pow(10.0, decPow))) * pow(10.0, decPow);
00081 
00082       return -((floor(0.5 + fabs(x) / pow(10.0, decPow))) * pow(10.0, decPow));
00083     }
00084 
00090     inline static double intervalTransformation(double x, double left1, double right1, double left2, double right2)
00091     {
00092       return left2 + (x - left1) * (right2 - left2) / (right1 - left1);
00093     }
00094 
00102     inline double linear2log(double x)
00103     {
00104       return log10(x + 1);     //+1 to avoid negative logarithms
00105     }
00106 
00114     inline double log2linear(double x)
00115     {
00116       return pow(10, x) - 1;
00117     }
00118 
00124     inline bool isOdd(UInt x)
00125     {
00126       return (x & 1) != 0;
00127     }
00128 
00134     template <typename T>
00135     T round(T x)
00136     {
00137       if (x >= T(0))
00138       {
00139         return T(floor(x + T(0.5)));
00140       }
00141       else
00142       {
00143         return T(ceil(x - T(0.5)));
00144       }
00145     }
00146 
00152     inline static bool approximatelyEqual(DoubleReal a, DoubleReal b, DoubleReal tol)
00153     {
00154       return std::fabs(a - b) <= tol;
00155     }
00156 
00165     template <typename T>
00166     T gcd(T a, T b)
00167     {
00168       T c;
00169       while (b != 0)
00170       {
00171         c = a % b;
00172         a = b;
00173         b = c;
00174       }
00175       return a;
00176     }
00177 
00190     template <typename T>
00191     T gcd(T a, T b, T & u1, T & u2)
00192     {
00193       u1 = 1;
00194       u2 = 0;
00195       T u3 = a;
00196 
00197       T v1 = 0;
00198       T v2 = 1;
00199       T v3 = b;
00200 
00201       while (v3 != 0)
00202       {
00203         T q = u3 / v3;
00204         T t1 = u1 - v1 * q;
00205         T t2 = u2 - v2 * q;
00206         T t3 = u3 - v3 * q;
00207 
00208         u1 = v1;
00209         u2 = v2;
00210         u3 = v3;
00211 
00212         v1 = t1;
00213         v2 = t2;
00214         v3 = t3;
00215       }
00216 
00217       return u3;
00218     }
00219 
00220   }   // namespace Math
00221 } // namespace OpenMS
00222 
00223 #endif // OPENMS_MATH_MISC_MATHFUNCTIONS_H

OpenMS / TOPP release 1.10.0 Documentation generated on Thu Mar 7 2013 09:42:41 using doxygen 1.7.1