IsoSpec  1.95
isoMath.h
1 /*
2  * Copyright (C) 2015-2019 Mateusz Łącki and Michał Startek.
3  *
4  * This file is part of IsoSpec.
5  *
6  * IsoSpec is free software: you can redistribute it and/or modify
7  * it under the terms of the Simplified ("2-clause") BSD licence.
8  *
9  * IsoSpec is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12  *
13  * You should have received a copy of the Simplified BSD Licence
14  * along with IsoSpec. If not, see <https://opensource.org/licenses/BSD-2-Clause>.
15  */
16 
17 #pragma once
18 
19 #include <cmath>
20 #include <fenv.h>
21 
22 #if !defined(ISOSPEC_G_FACT_TABLE_SIZE)
23 // 10M should be enough for anyone, right?
24 // Actually, yes. If anyone tries to input a molecule that has more than 10M atoms,
25 // he deserves to get an exception thrown in his face.
26 #define ISOSPEC_G_FACT_TABLE_SIZE 1024*1024*10
27 #endif
28 
29 namespace IsoSpec
30 {
31 
32 extern double* g_lfact_table;
33 
34 static inline double minuslogFactorial(int n)
35 {
36  if (n < 2)
37  return 0.0;
38  if (g_lfact_table[n] == 0.0)
39  g_lfact_table[n] = -lgamma(n+1);
40 
41  return g_lfact_table[n];
42 }
43 
44 const double pi = 3.14159265358979323846264338328;
45 const double log2pluslogpi = log(2.0) + log(pi);
46 
47 double NormalCDFInverse(double p);
48 double NormalCDFInverse(double p, double mean, double stdev);
49 double NormalCDF(double x, double mean, double stdev);
50 double NormalPDF(double x, double mean = 0.0, double stdev = 1.0);
51 
52 // Returns lower incomplete gamma function of a/2, x, where a is int and > 0.
53 double LowerIncompleteGamma2(int a, double x);
54 
55 // Returns y such that LowerIncompleteGamma2(a, y) == x. Approximately.
56 double InverseLowerIncompleteGamma2(int a, double x);
57 
58 // Computes the inverse Cumulative Distribution Funcion of the Chi-Square distribution with k degrees of freedom
59 inline double InverseChiSquareCDF2(int k, double x)
60 {
61  return InverseLowerIncompleteGamma2(k, x*tgamma(static_cast<double>(k)/2.0)) * 2.0;
62 }
63 
64 
65 
66 } // namespace IsoSpec
67 
IsoSpec
Definition: allocator.cpp:21