OpenMS
Loading...
Searching...
No Matches
MathFunctions.h
Go to the documentation of this file.
1// Copyright (c) 2002-present, OpenMS Inc. -- EKU Tuebingen, ETH Zurich, and FU Berlin
2// SPDX-License-Identifier: BSD-3-Clause
3//
4// --------------------------------------------------------------------------
5// $Maintainer: Timo Sachsenberg$
6// $Authors: Marc Sturm $
7// --------------------------------------------------------------------------
8
9#pragma once
10
16
17#include <boost/random/mersenne_twister.hpp> // for mt19937_64
18#include <boost/random/uniform_int.hpp>
19#include <cmath>
20#include <boost/math/special_functions/binomial.hpp>
21#include <boost/math/special_functions/gamma.hpp>
22#include <boost/math/special_functions/log1p.hpp>
23#include <boost/math/distributions/binomial.hpp>
24#include <boost/math/distributions/complement.hpp>
25#include <limits>
26#include <utility> // for std::pair
27#include <vector>
28
29namespace OpenMS
30{
38namespace Math
39{
40
49 template<typename T>
50 bool extendRange(T& min, T& max, const T& value)
51 {
52 if (value < min)
53 {
54 min = value;
55 return true;
56 }
57 if (value > max)
58 {
59 max = value;
60 return true;
61 }
62 return false;
63 }
64
70 template<typename T>
71 bool contains(T value, T min, T max)
72 {
73 return min <= value && value <= max;
74 }
75
96 inline std::pair<double, double> zoomIn(const double left, const double right, const float factor, const float align)
97 {
98 OPENMS_PRECONDITION(factor >= 0, "Factor must be >=0")
99 OPENMS_PRECONDITION(align >= 0, "align must be >=0")
100 OPENMS_PRECONDITION(align <= 1, "align must be <=1")
101 std::pair<double, double> res;
102 auto old_width = right - left;
103 auto offset_left = (1.0f - factor) * old_width * align;
104 res.first = left + offset_left;
105 res.second = res.first + old_width * factor;
106 return res;
107 }
108
109 using BinContainer = std::vector<RangeBase>;
125 inline BinContainer createBins(double min, double max, uint32_t number_of_bins, double extend_margin = 0)
126 {
127 OPENMS_PRECONDITION(number_of_bins >= 1, "Number of bins must be >= 1")
128 OPENMS_PRECONDITION(min < max, "Require min < max");
129 std::vector<RangeBase> res(number_of_bins);
130 const double bin_width = (max - min) / number_of_bins;
131 for (uint32_t i = 0; i < number_of_bins; ++i)
132 {
133 res[i] = RangeBase(min + i * bin_width, min + (i + 1) * bin_width);
134 res[i].extendLeftRight(extend_margin);
135 }
136 res.front().setMin(min); // undo potential margin
137 res.back().setMax(max); // undo potential margin
138
139 return res;
140 }
141
142
154 inline double ceilDecimal(double x, int decPow)
155 {
156 return (ceil(x / pow(10.0, decPow))) * pow(10.0, decPow); // decimal shift right, ceiling, decimal shift left
157 }
158
169 inline double roundDecimal(double x, int decPow)
170 {
171 if (x > 0) return (floor(0.5 + x / pow(10.0, decPow))) * pow(10.0, decPow);
172
173 return -((floor(0.5 + fabs(x) / pow(10.0, decPow))) * pow(10.0, decPow));
174 }
175
181 inline double intervalTransformation(double x, double left1, double right1, double left2, double right2)
182 {
183 return left2 + (x - left1) * (right2 - left2) / (right1 - left1);
184 }
185
193 inline double linear2log(double x)
194 {
195 return log10(x + 1); //+1 to avoid negative logarithms
196 }
197
205 inline double log2linear(double x)
206 {
207 return pow(10, x) - 1;
208 }
209
215 inline bool isOdd(UInt x)
216 {
217 return (x & 1) != 0;
218 }
219
225 template<typename T>
226 T round(T x)
227 {
228 return std::round(x);
229 }
230
245 template<typename T>
246 T roundTo(const T value, int digits)
247 {
248 T factor = 1.0;
249 if (digits > 0)
250 {
251 for (int i = 0; i < digits; ++i)
252 factor *= 10.0;
253 }
254 else if (digits < 0)
255 {
256 for (int i = 0; i < -digits; ++i)
257 factor /= 10.0;
258 }
259
260 return std::round(value * factor) / factor;
261 }
262
280 template<typename T>
281 double percentOf(T value, T total, int digits)
282 {
283 if (value < 0) { throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Value must be non-negative",StringUtils::toStr(value)); }
284 if (total < 0) { throw Exception::InvalidValue(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Total must be non-negative",StringUtils::toStr(total)); }
285 if (total <= 0) // avoid float equality compare
286 {
287 return 0.0; // avoid division by zero
288 }
289 return roundTo(value * 100.0 / total, digits);
290 }
291
297 inline bool approximatelyEqual(double a, double b, double tol)
298 {
299 return std::fabs(a - b) <= tol;
300 }
301
310 template<typename T>
311 T gcd(T a, T b)
312 {
313 T c;
314 while (b != 0)
315 {
316 c = a % b;
317 a = b;
318 b = c;
319 }
320 return a;
321 }
322
335 template<typename T>
336 T gcd(T a, T b, T& u1, T& u2)
337 {
338 u1 = 1;
339 u2 = 0;
340 T u3 = a;
341
342 T v1 = 0;
343 T v2 = 1;
344 T v3 = b;
345
346 while (v3 != 0)
347 {
348 T q = u3 / v3;
349 T t1 = u1 - v1 * q;
350 T t2 = u2 - v2 * q;
351 T t3 = u3 - v3 * q;
352
353 u1 = v1;
354 u2 = v2;
355 u3 = v3;
356
357 v1 = t1;
358 v2 = t2;
359 v3 = t3;
360 }
361
362 return u3;
363 }
364
374 template<typename T>
375 T getPPM(T mz_obs, T mz_ref)
376 {
377 return (mz_obs - mz_ref) / mz_ref * 1e6;
378 }
379
389 template<typename T>
390 T getPPMAbs(T mz_obs, T mz_ref)
391 {
392 return std::fabs(getPPM(mz_obs, mz_ref));
393 }
394
404 template<typename T>
405 T ppmToMass(T ppm, T mz_ref)
406 {
407 return (ppm / T(1e6)) * mz_ref;
408 }
409
410 /*
411 @brief Compute the absolute mass diff in [Th], given a ppm value and a reference point.
412
413 The returned mass diff is always positive!
414
415 @param[in] ppm Parts-per-million error
416 @param[in] mz_ref Reference m/z
417 @return The absolute mass diff in [Th]
418 */
419 template<typename T>
420 T ppmToMassAbs(T ppm, T mz_ref)
421 {
422 return std::fabs(ppmToMass(ppm, mz_ref));
423 }
424
438 inline std::pair<double, double> getTolWindow(double val, double tol, bool ppm)
439 {
440 double left, right;
441
442 if (ppm)
443 {
444 left = val - val * tol * 1e-6;
445 right = val / (1.0 - tol * 1e-6);
446 }
447 else
448 {
449 left = val - tol;
450 right = val + tol;
451 }
452
453 return std::make_pair(left, right);
454 }
455
459 template<typename T1>
460 typename T1::value_type quantile(const T1& x, double q)
461 {
462 if (x.empty()) throw Exception::InvalidParameter(__FILE__, __LINE__, OPENMS_PRETTY_FUNCTION, "Quantile requested from empty container.");
463 if (q < 0.0) q = 0.;
464 if (q > 1.0) q = 1.;
465
466 const auto n = x.size();
467 const auto id = std::max(0., n * q - 1); // -1 for c++ index starting at 0
468 const auto lo = floor(id);
469 const auto hi = ceil(id);
470 const auto qs = x[lo];
471 const auto h = (id - lo);
472
473 return (1.0 - h) * qs + h * x[hi];
474 }
475
476 // portable random shuffle
477 class OPENMS_DLLAPI RandomShuffler
478 {
479 public:
480 explicit RandomShuffler(int seed): rng_(boost::mt19937_64(seed))
481 {
482 }
483
484 explicit RandomShuffler(const boost::mt19937_64& mt_rng): rng_(mt_rng)
485 {
486 }
487
488 RandomShuffler() = default;
489 ~RandomShuffler() = default;
490
491 boost::mt19937_64 rng_;
492 template<class RandomAccessIterator>
493 void portable_random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
494 {
495 for (auto i = (last - first) - 1; i > 0; --i) // OMS_CODING_TEST_EXCLUDE
496 {
497 boost::uniform_int<decltype(i)> d(0, i);
498 std::swap(first[i], first[d(rng_)]);
499 }
500 }
501
502 void seed(uint64_t val)
503 {
504 rng_.seed(val);
505 }
506 };
507
516 inline double log_binomial_coef(unsigned n, unsigned k)
517 {
518 // Handle edge cases for improved numerical stability
519 if (k > n)
520 {
521 throw std::invalid_argument("k cannot be greater than n in binomial coefficient");
522 }
523
524 if (k == 0 || k == n)
525 {
526 return 0.0; // log(1) = 0
527 }
528
529 // Use symmetry to minimize computation for large k
530 if (k > n / 2)
531 {
532 k = n - k;
533 }
534
535 return boost::math::lgamma(n + 1.0) - boost::math::lgamma(k + 1.0) - boost::math::lgamma(n - k + 1.0);
536 }
537
545 inline double log_sum_exp(double x, double y)
546 {
547 // Handle infinite cases
548 if (std::isinf(x) && x < 0) return y;
549 if (std::isinf(y) && y < 0) return x;
550
551 // Use the maximum value for numerical stability
552 double max_val = std::max(x, y);
553 return max_val + std::log(std::exp(x - max_val) + std::exp(y - max_val));
554 }
555
568 inline double binomial_cdf_complement(unsigned N, unsigned n, double p)
569 {
570 if (p < 0.0 || p > 1.0)
571 {
572 throw std::invalid_argument("Probability p must be between 0 and 1");
573 }
574 if (n > N)
575 {
576 throw std::invalid_argument("n cannot be greater than N");
577 }
578
579 if (n == 0) return 1.0; // P(X ≥ 0) = 1
580 if (p == 0.0) return (n == 0) ? 1.0 : 0.0;
581 if (p == 1.0) return 1.0; // all mass at N
582
583 const boost::math::binomial_distribution<double> dist(N, p);
584 return boost::math::cdf(boost::math::complement(dist, n - 1));
585 }
586} // namespace Math
587} // namespace OpenMS
Exception indicating that an invalid parameter was handed over to an algorithm.
Definition Exception.h:317
Invalid value exception.
Definition Exception.h:306
Definition MathFunctions.h:478
RandomShuffler(int seed)
Definition MathFunctions.h:480
boost::mt19937_64 rng_
Definition MathFunctions.h:491
void seed(uint64_t val)
Definition MathFunctions.h:502
RandomShuffler(const boost::mt19937_64 &mt_rng)
Definition MathFunctions.h:484
void portable_random_shuffle(RandomAccessIterator first, RandomAccessIterator last)
Definition MathFunctions.h:493
unsigned int UInt
Unsigned integer type.
Definition Types.h:64
#define OPENMS_PRECONDITION(condition, message)
Precondition macro.
Definition openms/include/OpenMS/CONCEPT/Macros.h:94
bool approximatelyEqual(double a, double b, double tol)
Returns if a is approximately equal b , allowing a tolerance of tol.
Definition MathFunctions.h:297
bool isOdd(UInt x)
Returns true if the given integer is odd.
Definition MathFunctions.h:215
T gcd(T a, T b)
Returns the greatest common divisor (gcd) of two numbers by applying the Euclidean algorithm.
Definition MathFunctions.h:311
double log2linear(double x)
Transforms a number from log10 to to linear scale. Subtracts the 1 added by linear2log(double)
Definition MathFunctions.h:205
double roundDecimal(double x, int decPow)
rounds x to the next decimal power 10 ^ decPow
Definition MathFunctions.h:169
double linear2log(double x)
Transforms a number from linear to log10 scale. Avoids negative logarithms by adding 1.
Definition MathFunctions.h:193
T round(T x)
Rounds the value.
Definition MathFunctions.h:226
double ceilDecimal(double x, int decPow)
rounds x up to the next decimal power 10 ^ decPow
Definition MathFunctions.h:154
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:181
double log_sum_exp(double x, double y)
Log-sum-exp operation for numerical stability.
Definition MathFunctions.h:545
T getPPMAbs(T mz_obs, T mz_ref)
Compute absolute parts-per-million of two m/z values.
Definition MathFunctions.h:390
BinContainer createBins(double min, double max, uint32_t number_of_bins, double extend_margin=0)
Split a range [min,max] into number_of_bins (with optional overlap) and return the ranges of each bin...
Definition MathFunctions.h:125
T1::value_type quantile(const T1 &x, double q)
Returns the value of the q th quantile (0-1) in a sorted non-empty vector x.
Definition MathFunctions.h:460
double binomial_cdf_complement(unsigned N, unsigned n, double p)
Calculate binomial cumulative distribution function P(X ≥ n)
Definition MathFunctions.h:568
std::pair< double, double > zoomIn(const double left, const double right, const float factor, const float align)
Zoom into an interval [left, right], decreasing its width by factor (which must be in [0,...
Definition MathFunctions.h:96
double log_binomial_coef(unsigned n, unsigned k)
Calculate logarithm of binomial coefficient C(n,k) using log-gamma function.
Definition MathFunctions.h:516
T getPPM(T mz_obs, T mz_ref)
Compute parts-per-million of two m/z values.
Definition MathFunctions.h:375
std::pair< double, double > getTolWindow(double val, double tol, bool ppm)
Return tolerance window around val given tolerance tol.
Definition MathFunctions.h:438
T roundTo(const T value, int digits)
Definition MathFunctions.h:246
T ppmToMass(T ppm, T mz_ref)
Compute the mass diff in [Th], given a ppm value and a reference point.
Definition MathFunctions.h:405
T ppmToMassAbs(T ppm, T mz_ref)
Definition MathFunctions.h:420
double percentOf(T value, T total, int digits)
Definition MathFunctions.h:281
bool contains(T value, T min, T max)
Is a value contained in [min, max] ?
Definition MathFunctions.h:71
std::vector< RangeBase > BinContainer
Definition MathFunctions.h:109
bool extendRange(T &min, T &max, const T &value)
Given an interval/range and a new value, extend the range to include the new value if needed.
Definition MathFunctions.h:50
std::string toStr(int i)
Definition StringUtils.h:257
Main OpenMS namespace.
Definition openswathalgo/include/OpenMS/OPENSWATHALGO/DATAACCESS/ISpectrumAccess.h:19
Base class for a simple range with minimum and maximum.
Definition RangeManager.h:37