ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4LegendrePolynomial.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file G4LegendrePolynomial.cc
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 
26 #include "G4ios.hh"
27 #include "G4LegendrePolynomial.hh"
28 #include "G4Pow.hh"
29 #include "G4Exp.hh"
30 #include "G4Log.hh"
31 
32 using namespace std;
33 
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 }
42 
44 {
45  // Call EvalAssocLegendrePoly with m=0
46  return (EvalAssocLegendrePoly(order,0,x));
47 }
48 
50  map<G4int, map<G4int, G4double> >* cache)
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 }
122 
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 }
148