Garfield++ v2r0
A toolkit for the detailed simulation of particle detectors based on ionisation 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 <vector>
5#include <complex>
6
7namespace Garfield {
8
9/// Collection of numerical routines.
10namespace Numerics {
11
12// Linear algebra routines from CERNLIB
13void Dfact(const int n, std::vector<std::vector<double> >& a,
14 std::vector<int>& ir, int& ifail, double& det, int& jfail);
15void Dfeqn(const int n, std::vector<std::vector<double> >& a,
16 std::vector<int>& ir, std::vector<double>& b);
17void Dfinv(const int n, std::vector<std::vector<double> >& a,
18 std::vector<int>& ir);
19void Deqinv(const int n, std::vector<std::vector<double> >& a, int& ifail,
20 std::vector<double>& b);
21
22void Cfact(const int n, std::vector<std::vector<std::complex<double> > >& a,
23 std::vector<int>& ir, int& ifail, std::complex<double>& det,
24 int& jfail);
25void Cfinv(const int n, std::vector<std::vector<std::complex<double> > >& a,
26 std::vector<int>& ir);
27void Cinv(const int n, std::vector<std::vector<std::complex<double> > >& a,
28 int& ifail);
29
30/// Numerical integration using 15-point Gauss-Kronrod algorithm
31double GaussKronrod15(double (*f)(const double), const double a,
32 const double b);
33
34/// Modified Bessel functions.
35/// Series expansions from Abramowitz and Stegun.
36inline double BesselI0S(const double xx) {
37 return 1. + 3.5156229 * pow(xx / 3.75, 2) + 3.0899424 * pow(xx / 3.75, 4) +
38 1.2067492 * pow(xx / 3.75, 6) + 0.2659732 * pow(xx / 3.75, 8) +
39 0.0360768 * pow(xx / 3.75, 10) + 0.0045813 * pow(xx / 3.75, 12);
40}
41
42inline double BesselI1S(const double xx) {
43 return xx *
44 (0.5 + 0.87890594 * pow(xx / 3.75, 2) +
45 0.51498869 * pow(xx / 3.75, 4) + 0.15084934 * pow(xx / 3.75, 6) +
46 0.02658733 * pow(xx / 3.75, 8) + 0.00301532 * pow(xx / 3.75, 10) +
47 0.00032411 * pow(xx / 3.75, 12));
48}
49
50inline double BesselK0S(const double xx) {
51 return -log(xx / 2.) * BesselI0S(xx) - 0.57721566 +
52 0.42278420 * pow(xx / 2., 2) + 0.23069756 * pow(xx / 2., 4) +
53 0.03488590 * pow(xx / 2., 6) + 0.00262698 * pow(xx / 2., 8) +
54 0.00010750 * pow(xx / 2., 10) + 0.00000740 * pow(xx / 2., 12);
55}
56
57inline double BesselK0L(const double xx) {
58 return (exp(-xx) / sqrt(xx)) *
59 (1.25331414 - 0.07832358 * (2. / xx) + 0.02189568 * pow(2. / xx, 2) -
60 0.01062446 * pow(2. / xx, 3) + 0.00587872 * pow(2. / xx, 4) -
61 0.00251540 * pow(2. / xx, 5) + 0.00053208 * pow(2. / xx, 6));
62}
63
64inline double BesselK1S(const double xx) {
65 return log(xx / 2.) * BesselI1S(xx) +
66 (1. / xx) *
67 (1. + 0.15443144 * pow(xx / 2., 2) - 0.67278579 * pow(xx / 2., 4) -
68 0.18156897 * pow(xx / 2., 6) - 0.01919402 * pow(xx / 2., 8) -
69 0.00110404 * pow(xx / 2., 10) - 0.00004686 * pow(xx / 2., 12));
70}
71
72inline double BesselK1L(const double xx) {
73 return (exp(-xx) / sqrt(xx)) *
74 (1.25331414 + 0.23498619 * (2. / xx) - 0.03655620 * pow(2. / xx, 2) +
75 0.01504268 * pow(2. / xx, 3) - 0.00780353 * pow(2. / xx, 4) +
76 0.00325614 * pow(2. / xx, 5) - 0.00068245 * pow(2. / xx, 6));
77}
78
79double Divdif(const std::vector<double>& f, const std::vector<double>& a,
80 int nn, double x, int mm);
81
82bool Boxin3(const std::vector<std::vector<std::vector<double> > >& value,
83 const std::vector<double>& xAxis,
84 const std::vector<double>& yAxis,
85 const std::vector<double>& zAxis,
86 const int nx, const int ny, const int nz,
87 const double xx, const double yy, const double zz,
88 double& f, const int iOrder);
89
90}
91
92inline double InterpolateBinarySearch(const std::vector<double>& x,
93 const std::vector<double>& y,
94 const double x0) {
95
96 int iLow = 0;
97 int iUp = x.size() - 1;
98 while (iUp - iLow > 1) {
99 const int iM = (iUp + iLow) >> 1;
100 if (x0 >= x[iM]) {
101 iLow = iM;
102 } else {
103 iUp = iM;
104 }
105 }
106 // Linear interpolation.
107 return y[iLow] + (x0 - x[iLow]) * (y[iUp] - y[iLow]) / (x[iUp] - x[iLow]);
108}
109
110}
111
112#endif
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)
Definition: Numerics.cc:770
double BesselK0L(const double xx)
Definition: Numerics.hh:57
double BesselI0S(const double xx)
Definition: Numerics.hh:36
double BesselI1S(const double xx)
Definition: Numerics.hh:42
void Dfinv(const int n, std::vector< std::vector< double > > &a, std::vector< int > &ir)
Definition: Numerics.cc:138
double BesselK1S(const double xx)
Definition: Numerics.hh:64
void Dfact(const int n, std::vector< std::vector< double > > &a, std::vector< int > &ir, int &ifail, double &det, int &jfail)
Definition: Numerics.cc:10
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)
Definition: Numerics.cc:333
double GaussKronrod15(double(*f)(const double), const double a, const double b)
Numerical integration using 15-point Gauss-Kronrod algorithm.
Definition: Numerics.cc:599
void Cinv(const int n, std::vector< std::vector< std::complex< double > > > &a, int &ifail)
Definition: Numerics.cc:494
double Divdif(const std::vector< double > &f, const std::vector< double > &a, int nn, double x, int mm)
Definition: Numerics.cc:650
double BesselK0S(const double xx)
Definition: Numerics.hh:50
void Deqinv(const int n, std::vector< std::vector< double > > &a, int &ifail, std::vector< double > &b)
Definition: Numerics.cc:216
void Cfinv(const int n, std::vector< std::vector< std::complex< double > > > &a, std::vector< int > &ir)
Definition: Numerics.cc:424
double BesselK1L(const double xx)
Definition: Numerics.hh:72
void Dfeqn(const int n, std::vector< std::vector< double > > &a, std::vector< int > &ir, std::vector< double > &b)
Definition: Numerics.cc:98
double InterpolateBinarySearch(const std::vector< double > &x, const std::vector< double > &y, const double x0)
Definition: Numerics.hh:92