Geant4-11
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes
G4LegendrePolynomial Class Reference

#include <G4LegendrePolynomial.hh>

Public Member Functions

G4double EvalAssocLegendrePoly (G4int l, G4int m, G4double x, std::map< G4int, std::map< G4int, G4double > > *cache=NULL)
 
G4double EvalLegendrePoly (G4int order, G4double x)
 
G4double GetCoefficient (size_t i, size_t order)
 

Static Public Member Functions

static size_t GetNCoefficients (size_t order)
 

Protected Member Functions

void BuildUpToOrder (size_t order)
 

Protected Attributes

std::vector< std::vector< G4double > > fCoefficients
 

Detailed Description

Definition at line 50 of file G4LegendrePolynomial.hh.

Member Function Documentation

◆ BuildUpToOrder()

void G4LegendrePolynomial::BuildUpToOrder ( size_t  order)
protected

Definition at line 123 of file G4LegendrePolynomial.cc.

124{
125 if(orderMax > 30) {
126 G4cout << "G4LegendrePolynomial::GetCoefficient(): "
127 << "I refuse to make a Legendre Polynomial of order "
128 << orderMax << G4endl;
129 return;
130 }
131 while(fCoefficients.size() < orderMax+1) { /* Loop checking, 30-Oct-2015, G.Folger */
132 size_t order = fCoefficients.size();
133 fCoefficients.resize(order+1);
134 if(order <= 1) fCoefficients[order].push_back(1.);
135 else {
136 for(size_t iCoeff = 0; iCoeff < order+1; ++iCoeff) {
137 if((order % 2) == (iCoeff % 2)) {
138 G4double coeff = 0;
139 if(iCoeff <= order-2) coeff -= fCoefficients[order-2][iCoeff/2]*G4double(order-1);
140 if(iCoeff > 0) coeff += fCoefficients[order-1][(iCoeff-1)/2]*G4double(2*order-1);
141 coeff /= G4double(order);
142 fCoefficients[order].push_back(coeff);
143 }
144 }
145 }
146 }
147}
double G4double
Definition: G4Types.hh:83
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
std::vector< std::vector< G4double > > fCoefficients

References fCoefficients, G4cout, and G4endl.

Referenced by GetCoefficient().

◆ EvalAssocLegendrePoly()

G4double G4LegendrePolynomial::EvalAssocLegendrePoly ( G4int  l,
G4int  m,
G4double  x,
std::map< G4int, std::map< G4int, G4double > > *  cache = NULL 
)

Definition at line 49 of file G4LegendrePolynomial.cc.

51{
52 // Calculate P_l^m(x).
53 // If cache ptr is non-null, use cache[l][m] if it exists, otherwise compute
54 // P_l^m(x) and cache it in that position. The cache speeds up calculations
55 // where many P_l^m computations are need at the same value of x.
56
57 if(l<0 || m<-l || m>l) return 0;
58 G4Pow* g4pow = G4Pow::GetInstance();
59
60 // Use non-log factorial for low l, m: it is more efficient until
61 // l and m get above 10 or so.
62 // FIXME: G4Pow doesn't check whether the argument gets too large,
63 // which is unsafe! Max is 512; VI: It is assume that Geant4 does not
64 // need higher order
65 if(m<0) {
66 G4double value = (m%2 ? -1. : 1.) * EvalAssocLegendrePoly(l, -m, x);
67 if(l < 10) return value * g4pow->factorial(l+m)/g4pow->factorial(l-m);
68 else { return value * G4Exp(g4pow->logfactorial(l+m) - g4pow->logfactorial(l-m));
69 }
70 }
71
72 // hard-code the first few orders for speed
73 if(l==0) return 1;
74 if(l==1) {
75 if(m==0){return x;}
76 /*m==1*/ return -sqrt(1.-x*x);
77 }
78 if(l<5) {
79 G4double x2 = x*x;
80 if(l==2) {
81 if(m==0){return 0.5*(3.*x2 - 1.);}
82 if(m==1){return -3.*x*sqrt(1.-x2);}
83 /*m==2*/ return 3.*(1.-x2);
84 }
85 if(l==3) {
86 if(m==0){return 0.5*(5.*x*x2 - 3.*x);}
87 if(m==1){return -1.5*(5.*x2-1.)*sqrt(1.-x2);}
88 if(m==2){return 15.*x*(1.-x2);}
89 /*m==3*/ return -15.*(1.-x2)*sqrt(1.-x2);
90 }
91 if(l==4) {
92 if(m==0){return 0.125*(35.*x2*x2 - 30.*x2 + 3.);}
93 if(m==1){return -2.5*(7.*x*x2-3.*x)*sqrt(1.-x2);}
94 if(m==2){return 7.5*(7.*x2-1.)*(1.-x2);}
95 if(m==3){return -105.*x*(1.-x2)*sqrt(1.-x2);}
96 /*m==4*/ return 105.*(1. - 2.*x2 + x2*x2);
97 }
98 }
99
100 // Easy special cases
101 // FIXME: G4Pow doesn't check whether the argument gets too large, which is unsafe! Max is 512.
102 if(m==l) return (l%2 ? -1. : 1.) *
103 G4Exp(g4pow->logfactorial(2*l) - g4pow->logfactorial(l)) *
104 G4Exp(G4Log((1.-x*x)*0.25)*0.5*G4double(l));
105 if(m==l-1) return x*(2.*G4double(m)+1.)*EvalAssocLegendrePoly(m,m,x);
106
107 // See if we have this value cached.
108 if(cache != NULL && cache->count(l) > 0 && (*cache)[l].count(m) > 0) {
109 return (*cache)[l][m];
110 }
111
112 // Otherwise calculate recursively
113 G4double value = (x*G4double(2*l-1)*EvalAssocLegendrePoly(l-1,m,x) -
114 (G4double(l+m-1))*EvalAssocLegendrePoly(l-2,m,x))/G4double(l-m);
115
116 // If we are working with a cache, cache this value.
117 if(cache != NULL) {
118 (*cache)[l][m] = value;
119 }
120 return value;
121}
G4double G4Exp(G4double initial_x)
Exponential Function double precision.
Definition: G4Exp.hh:179
G4double G4Log(G4double x)
Definition: G4Log.hh:226
static constexpr double m
Definition: G4SIunits.hh:109
G4double EvalAssocLegendrePoly(G4int l, G4int m, G4double x, std::map< G4int, std::map< G4int, G4double > > *cache=NULL)
Definition: G4Pow.hh:49
static G4Pow * GetInstance()
Definition: G4Pow.cc:41
G4double factorial(G4int Z) const
Definition: G4Pow.hh:235
G4double logfactorial(G4int Z) const
Definition: G4Pow.hh:237

References EvalAssocLegendrePoly(), G4Pow::factorial(), G4Exp(), G4Log(), G4Pow::GetInstance(), G4Pow::logfactorial(), and m.

Referenced by EvalAssocLegendrePoly(), EvalLegendrePoly(), G4PolarizationTransition::GenerateGammaPhi(), and G4PolarizationTransition::SampleGammaTransition().

◆ EvalLegendrePoly()

G4double G4LegendrePolynomial::EvalLegendrePoly ( G4int  order,
G4double  x 
)

Definition at line 43 of file G4LegendrePolynomial.cc.

44{
45 // Call EvalAssocLegendrePoly with m=0
46 return (EvalAssocLegendrePoly(order,0,x));
47}

References EvalAssocLegendrePoly().

◆ GetCoefficient()

G4double G4LegendrePolynomial::GetCoefficient ( size_t  i,
size_t  order 
)

Definition at line 34 of file G4LegendrePolynomial.cc.

35{
36 if(order >= fCoefficients.size()) BuildUpToOrder(order);
37 if(order >= fCoefficients.size() ||
38 i/2 >= fCoefficients[order].size() ||
39 (i%2) != order %2) return 0;
40 return fCoefficients[order][i/2];
41}
void BuildUpToOrder(size_t order)

References BuildUpToOrder(), and fCoefficients.

Referenced by G4PolarizationTransition::GenerateGammaCosTheta().

◆ GetNCoefficients()

static size_t G4LegendrePolynomial::GetNCoefficients ( size_t  order)
inlinestatic

Definition at line 54 of file G4LegendrePolynomial.hh.

54{ return order+1; }

Referenced by G4PolarizationTransition::GenerateGammaCosTheta().

Field Documentation

◆ fCoefficients

std::vector< std::vector<G4double> > G4LegendrePolynomial::fCoefficients
protected

Definition at line 64 of file G4LegendrePolynomial.hh.

Referenced by BuildUpToOrder(), and GetCoefficient().


The documentation for this class was generated from the following files: