Garfield 0.3
Toolkit for the detailed simulation of particle detectors based on ionization measurement in gases and semiconductors
Loading...
Searching...
No Matches
Numerics.hh
Go to the documentation of this file.
1#ifndef G_NUMERICS_H
2#define G_NUMERICS_H
3
4#include <array>
5#include <complex>
6#include <functional>
7#include <vector>
8
10
11namespace Garfield {
12
14namespace Numerics {
15
16constexpr std::array<double, 2> GaussLegendreNodes2() {
17 return {-0.577350269189625765, 0.577350269189625765};
18}
19constexpr std::array<double, 2> GaussLegendreWeights2() { return {1., 1.}; }
20constexpr std::array<double, 3> GaussLegendreNodes3() {
21 return {-0.774596669241483377, 0., 0.774596669241483377};
22}
23constexpr std::array<double, 3> GaussLegendreWeights3() {
24 return {5. / 9., 8. / 9., 5. / 9.};
25}
26constexpr std::array<double, 4> GaussLegendreNodes4() {
27 return {-0.861136311594052575, -0.339981043584856265, 0.339981043584856265,
28 0.861136311594052575};
29}
30constexpr std::array<double, 4> GaussLegendreWeights4() {
31 return {0.347854845137453857, 0.652145154862546143, 0.652145154862546143,
32 0.347854845137453857};
33}
34constexpr std::array<double, 5> GaussLegendreNodes5() {
35 return {-0.906179845938663993, -0.538469310105683091, 0.,
36 0.538469310105683091, 0.906179845938663993};
37}
38constexpr std::array<double, 5> GaussLegendreWeights5() {
39 return {0.236926885056189088, 0.478628670499366468, 128. / 225.,
40 0.478628670499366468, 0.236926885056189088};
41}
42constexpr std::array<double, 6> GaussLegendreNodes6() {
43 return {-0.932469514203152028, -0.661209386466264514, -0.238619186083196909,
44 0.238619186083196909, 0.661209386466264514, 0.932469514203152028};
45}
46constexpr std::array<double, 6> GaussLegendreWeights6() {
47 return {0.171324492379170345, 0.360761573048138608, 0.467913934572691047,
48 0.467913934572691047, 0.360761573048138608, 0.171324492379170345};
49}
50
57namespace QUADPACK {
58
98void qagi(std::function<double(double)> f, double bound, const int inf,
99 const double epsabs, const double epsrel, double& result,
100 double& abserr, unsigned int& status);
101
113void qk15i(std::function<double(double)> f, double bound, const int inf,
114 const double a, const double b, double& result, double& abserr,
115 double& resabs, double& resasc);
116
118void qk15(std::function<double(double)> f, const double a, const double b,
119 double& result, double& abserr, double& resabs, double& resasc);
120
121} // namespace QUADPACK
122
124namespace CERNLIB {
125
131int deqn(const int n, std::vector<std::vector<double> >& a,
132 std::vector<double>& b);
134int deqinv(const int n, std::vector<std::vector<double> >& a,
135 std::vector<double>& b);
136
137void dfact(const int n, std::vector<std::vector<double> >& a,
138 std::vector<int>& ir, int& ifail, double& det, int& jfail);
139void dfeqn(const int n, std::vector<std::vector<double> >& a,
140 std::vector<int>& ir, std::vector<double>& b);
141void dfinv(const int n, std::vector<std::vector<double> >& a,
142 std::vector<int>& ir);
144int dinv(const int n, std::vector<std::vector<double> >& a);
145
146void cfact(const int n, std::vector<std::vector<std::complex<double> > >& a,
147 std::vector<int>& ir, int& ifail, std::complex<double>& det,
148 int& jfail);
149void cfinv(const int n, std::vector<std::vector<std::complex<double> > >& a,
150 std::vector<int>& ir);
151
153int cinv(const int n, std::vector<std::vector<std::complex<double> > >& a);
154
155void cfft(std::vector<std::complex<double> >& a, const int msign);
156} // namespace CERNLIB
157
159inline double Legendre(const unsigned int n, const double x) {
160 if (std::abs(x) > 1.) return 0.;
161 double p0 = 1.;
162 double p1 = x;
163 if (n == 0) return p0;
164 if (n == 1) return p1;
165 for (unsigned int k = 1; k < n; ++k) {
166 p0 = ((2 * k + 1) * x * p1 - k * p0) / (k + 1);
167 std::swap(p0, p1);
168 }
169 return p1;
170}
171
174inline double BesselI0S(const double xx) {
175 const double y = xx / 3.75;
176 const double y2 = y * y;
177 return 1. + 3.5156229 * y2 + 3.0899424 * y2 * y2 + 1.2067492 * pow(y2, 3) +
178 0.2659732 * pow(y2, 4) + 0.0360768 * pow(y2, 5) +
179 0.0045813 * pow(y2, 6);
180}
181
182inline double BesselI1S(const double xx) {
183 const double y = xx / 3.75;
184 const double y2 = y * y;
185 return xx * (0.5 + 0.87890594 * y2 + 0.51498869 * y2 * y2 +
186 0.15084934 * pow(y2, 3) + 0.02658733 * pow(y2, 4) +
187 0.00301532 * pow(y2, 5) + 0.00032411 * pow(y2, 6));
188}
189
190inline double BesselK0S(const double xx) {
191 const double y = xx / 2.;
192 const double y2 = y * y;
193 return -log(y) * BesselI0S(xx) - Gamma + 0.42278420 * y2 +
194 0.23069756 * y2 * y2 + 0.03488590 * pow(y2, 3) +
195 0.00262698 * pow(y2, 4) + 0.00010750 * pow(y2, 5) +
196 0.00000740 * pow(y2, 6);
197}
198
199inline double BesselK0L(const double xx) {
200 const double y = 2. / xx;
201 return (exp(-xx) / sqrt(xx)) *
202 (1.25331414 - 0.07832358 * y + 0.02189568 * y * y -
203 0.01062446 * pow(y, 3) + 0.00587872 * pow(y, 4) -
204 0.00251540 * pow(y, 5) + 0.00053208 * pow(y, 6));
205}
206
207inline double BesselK1S(const double xx) {
208 const double y = xx / 2.;
209 const double y2 = y * y;
210 return log(y) * BesselI1S(xx) +
211 (1. / xx) * (1. + 0.15443144 * y2 - 0.67278579 * y2 * y2 -
212 0.18156897 * pow(y2, 3) - 0.01919402 * pow(y2, 4) -
213 0.00110404 * pow(y2, 5) - 0.00004686 * pow(y2, 6));
214}
215
216inline double BesselK1L(const double xx) {
217 const double y = 2. / xx;
218 return (exp(-xx) / sqrt(xx)) *
219 (1.25331414 + 0.23498619 * y - 0.03655620 * y * y +
220 0.01504268 * pow(y, 3) - 0.00780353 * pow(y, 4) +
221 0.00325614 * pow(y, 5) - 0.00068245 * pow(y, 6));
222}
223
226double Divdif(const std::vector<double>& f, const std::vector<double>& a,
227 int nn, double x, int mm);
228
229double LinearInterpolation(const std::vector<double>& ytab,
230 const std::vector<double>& xtab, const double x);
231
234bool Boxin2(const std::vector<std::vector<double> >& value,
235 const std::vector<double>& xAxis, const std::vector<double>& yAxis,
236 const int nx, const int ny, const double xx, const double yy,
237 double& f, const int iOrder);
240bool Boxin3(const std::vector<std::vector<std::vector<double> > >& value,
241 const std::vector<double>& xAxis, const std::vector<double>& yAxis,
242 const std::vector<double>& zAxis, const int nx, const int ny,
243 const int nz, const double xx, const double yy, const double zz,
244 double& f, const int iOrder);
245
248 std::function<double(double, const std::vector<double>&)> f,
249 std::vector<double>& par, std::vector<double>& epar,
250 const std::vector<double>& x, const std::vector<double>& y,
251 const std::vector<double>& ey, const unsigned int nMaxIter,
252 const double diff, double& chi2, const double eps, const bool debug,
253 const bool verbose);
254
255} // namespace Numerics
256} // namespace Garfield
257
258#endif
Linear algebra routines from CERNLIB.
Definition Numerics.hh:124
void dfinv(const int n, std::vector< std::vector< double > > &a, std::vector< int > &ir)
void cfact(const int n, std::vector< std::vector< std::complex< double > > > &a, std::vector< int > &ir, int &ifail, std::complex< double > &det, int &jfail)
int deqn(const int n, std::vector< std::vector< double > > &a, std::vector< double > &b)
Replaces b by the solution x of Ax = b, after which A is undefined.
void dfact(const int n, std::vector< std::vector< double > > &a, std::vector< int > &ir, int &ifail, double &det, int &jfail)
void cfinv(const int n, std::vector< std::vector< std::complex< double > > > &a, std::vector< int > &ir)
int dinv(const int n, std::vector< std::vector< double > > &a)
Replace square matrix A by its inverse.
void dfeqn(const int n, std::vector< std::vector< double > > &a, std::vector< int > &ir, std::vector< double > &b)
int cinv(const int n, std::vector< std::vector< std::complex< double > > > &a)
Replace square matrix A by its inverse.
int deqinv(const int n, std::vector< std::vector< double > > &a, std::vector< double > &b)
Replaces b by the solution x of Ax = b, and replace A by its inverse.
void cfft(std::vector< std::complex< double > > &a, const int msign)
Functions for performing numerical integration (quadrature).
Definition Numerics.hh:57
void qk15(std::function< double(double)> f, const double a, const double b, double &result, double &abserr, double &resabs, double &resasc)
15-point Gauss-Kronrod integration with finite integration range.
void qagi(std::function< double(double)> f, double bound, const int inf, const double epsabs, const double epsrel, double &result, double &abserr, unsigned int &status)
Estimates an integral over a semi-infinite or infinite interval.
void qk15i(std::function< double(double)> f, double bound, const int inf, const double a, const double b, double &result, double &abserr, double &resabs, double &resasc)
15-point Gauss-Kronrod integration with (semi-)infinite integration range.
Collection of numerical routines.
Definition Numerics.hh:14
constexpr std::array< double, 4 > GaussLegendreWeights4()
Definition Numerics.hh:30
constexpr std::array< double, 2 > GaussLegendreNodes2()
Definition Numerics.hh:16
bool Boxin3(const std::vector< std::vector< std::vector< double > > > &value, const std::vector< double > &xAxis, const std::vector< double > &yAxis, const std::vector< double > &zAxis, const int nx, const int ny, const int nz, const double xx, const double yy, const double zz, double &f, const int iOrder)
Interpolation of order 1 and 2 in an irregular rectangular three-dimensional grid.
double BesselK0L(const double xx)
Definition Numerics.hh:199
double BesselI0S(const double xx)
Modified Bessel functions.
Definition Numerics.hh:174
double BesselI1S(const double xx)
Definition Numerics.hh:182
constexpr std::array< double, 5 > GaussLegendreWeights5()
Definition Numerics.hh:38
double Legendre(const unsigned int n, const double x)
Legendre polynomials.
Definition Numerics.hh:159
constexpr std::array< double, 6 > GaussLegendreWeights6()
Definition Numerics.hh:46
double BesselK1S(const double xx)
Definition Numerics.hh:207
bool LeastSquaresFit(std::function< double(double, const std::vector< double > &)> f, std::vector< double > &par, std::vector< double > &epar, const std::vector< double > &x, const std::vector< double > &y, const std::vector< double > &ey, const unsigned int nMaxIter, const double diff, double &chi2, const double eps, const bool debug, const bool verbose)
Least-squares minimisation.
constexpr std::array< double, 2 > GaussLegendreWeights2()
Definition Numerics.hh:19
bool Boxin2(const std::vector< std::vector< double > > &value, const std::vector< double > &xAxis, const std::vector< double > &yAxis, const int nx, const int ny, const double xx, const double yy, double &f, const int iOrder)
Interpolation of order 1 and 2 in an irregular rectangular two-dimensional grid.
constexpr std::array< double, 3 > GaussLegendreNodes3()
Definition Numerics.hh:20
double LinearInterpolation(const std::vector< double > &ytab, const std::vector< double > &xtab, const double x)
constexpr std::array< double, 4 > GaussLegendreNodes4()
Definition Numerics.hh:26
double Divdif(const std::vector< double > &f, const std::vector< double > &a, int nn, double x, int mm)
C++ version of DIVDIF (CERN program library E105) which performs tabular interpolation using symmetri...
constexpr std::array< double, 5 > GaussLegendreNodes5()
Definition Numerics.hh:34
constexpr std::array< double, 3 > GaussLegendreWeights3()
Definition Numerics.hh:23
double BesselK0S(const double xx)
Definition Numerics.hh:190
double BesselK1L(const double xx)
Definition Numerics.hh:216
constexpr std::array< double, 6 > GaussLegendreNodes6()
Definition Numerics.hh:42