Geant4  10.01.p02
G4Pow.hh
Go to the documentation of this file.
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 // $Id: G4Pow.hh 83383 2014-08-21 14:20:37Z gcosmo $
27 //
28 //
29 // -------------------------------------------------------------------
30 //
31 // Class G4Pow
32 //
33 // Class description:
34 //
35 // Utility singleton class for the fast computation of log and pow
36 // functions. Integer argument should in the interval 0-512, no
37 // check is performed inside these methods for performance reasons.
38 // For factorial integer argument should be in the interval 0-170
39 // Computations with double arguments are fast for the interval
40 // 0.002-511.5 for all functions except exponent, which is computed
41 // for the interval 0-84.4, standard library is used in the opposite case
42 
43 // Author: Vladimir Ivanchenko
44 //
45 // Creation date: 23.05.2009
46 // -------------------------------------------------------------------
47 
48 #ifndef G4Pow_h
49 #define G4Pow_h 1
50 
51 #include "globals.hh"
52 #include "G4Log.hh"
53 #include "G4Exp.hh"
54 #include "G4DataVector.hh"
55 
56 class G4Pow
57 {
58 
59  public:
60 
61  static G4Pow* GetInstance();
62  ~G4Pow();
63 
64  // Fast computation of Z^1/3
65  //
66  inline G4double Z13(G4int Z) const;
67  inline G4double A13(G4double A) const;
68 
69  // Fast computation of Z^2/3
70  //
71  inline G4double Z23(G4int Z) const;
72  inline G4double A23(G4double A) const;
73 
74  // Fast computation of log(Z)
75  //
76  inline G4double logZ(G4int Z) const;
77  inline G4double logA(G4double A) const;
78  inline G4double logX(G4double x) const;
79 
80  // Fast computation of log10(Z)
81  //
82  inline G4double log10Z(G4int Z) const;
83  inline G4double log10A(G4double A) const;
84 
85  // Fast computation of exp(X)
86  //
87  inline G4double expA(G4double A) const;
88 
89  // Fast computation of pow(Z,X)
90  //
91  inline G4double powZ(G4int Z, G4double y) const;
92  inline G4double powA(G4double A, G4double y) const;
93  G4double powN(G4double x, G4int n) const;
94 
95  // Fast factorial
96  //
97  inline G4double factorial(G4int Z) const;
98  inline G4double logfactorial(G4int Z) const;
99 
100  private:
101 
102  G4Pow();
103 
104  inline G4double logBase(G4double x) const;
105 
106  static G4Pow* fpInstance;
107 
109  const G4int max2;
110 
114 
123 };
124 
125 // -------------------------------------------------------------------
126 
127 inline G4double G4Pow::Z13(G4int Z) const
128 {
129  return pz13[Z];
130 }
131 
133 {
134  G4double res;
135  G4double a = A;
136  if(1.0 > A) { a = 1.0/A; }
137  if(a <= maxA)
138  {
139  G4int i = G4int(a + 0.5);
140  G4double x = (a/G4double(i) - 1.0)*onethird;
141  res = pz13[i]*(1.0 + x - x*x*(1.0 - 1.66666666*x));
142  if(1.0 > A) { res = 1.0/res; }
143  }
144  else
145  {
146  res = std::pow(A, onethird);
147  }
148  return res;
149 }
150 
151 inline G4double G4Pow::Z23(G4int Z) const
152 {
153  G4double x = Z13(Z);
154  return x*x;
155 }
156 
158 {
159  G4double x = A13(A);
160  return x*x;
161 }
162 
163 inline G4double G4Pow::logZ(G4int Z) const
164 {
165  return lz[Z];
166 }
167 
169 {
170  G4double res;
171  if(a <= maxA2)
172  {
173  G4int i = G4int(max2*(a - 1) + 0.5);
174  if(i > max2) { i = max2; }
175  G4double x = a/(G4double(i)/max2 + 1) - 1;
176  res = lz2[i] + x*(1.0 - (0.5 - onethird*x)*x);
177  }
178  else if(a <= maxA)
179  {
180  G4int i = G4int(a + 0.5);
181  G4double x = a/G4double(i) - 1;
182  res = lz[i] + x*(1.0 - (0.5 - onethird*x)*x);
183  }
184  else
185  {
186  res = G4Log(a);
187  }
188  return res;
189 }
190 
192 {
193  G4double res;
194  if(1.0 <= A) { res = logBase(A); }
195  else { res = -logBase(1./A); }
196  return res;
197 }
198 
200 {
201  G4double res = 0.0;
202  G4double a = x;
203  if(1.0 > x) { a = 1.0/x; }
204 
205  if(a <= maxA)
206  {
207  res = logBase(a);
208  }
209  else if(a <= ener[2])
210  {
211  res = logen[1] + logBase(a/ener[1]);
212  }
213  else if(a <= ener[3])
214  {
215  res = logen[2] + logBase(a/ener[2]);
216  }
217  else
218  {
219  res = G4Log(a);
220  }
221 
222  if(1.0 > x) { res = -res; }
223  return res;
224 }
225 
226 inline G4double G4Pow::log10Z(G4int Z) const
227 {
228  return lz[Z]/lz[10];
229 }
230 
232 {
233  return logX(A)/lz[10];
234 }
235 
237 {
238  G4double res;
239  G4double a = A;
240  if(0.0 > A) { a = -A; }
241 
242  if(a <= maxAexp)
243  {
244  G4int i = G4int(2*a + 0.5);
245  G4double x = a - i*0.5;
246  res = fexp[i]*(1.0 + x*(1.0 + 0.5*(1.0 + onethird*x)*x));
247  }
248  else
249  {
250  res = G4Exp(a);
251  }
252  if(0.0 > A) { res = 1.0/res; }
253  return res;
254 }
255 
256 inline G4double G4Pow::powZ(G4int Z, G4double y) const
257 {
258  return expA(y*lz[Z]);
259 }
260 
262 {
263  return expA(y*logX(A));
264 }
265 
267 {
268  return fact[Z];
269 }
270 
272 {
273  return logfact[Z];
274 }
275 
276 // -------------------------------------------------------------------
277 
278 #endif
static G4Pow * GetInstance()
Definition: G4Pow.cc:55
G4double powA(G4double A, G4double y) const
Definition: G4Pow.hh:261
G4DataVector fact
Definition: G4Pow.hh:121
G4double maxAexp
Definition: G4Pow.hh:113
~G4Pow()
Definition: G4Pow.cc:123
G4double powN(G4double x, G4int n) const
Definition: G4Pow.cc:128
Definition: G4Pow.hh:56
G4double expA(G4double A) const
Definition: G4Pow.hh:236
G4double log10A(G4double A) const
Definition: G4Pow.hh:231
const G4double onethird
Definition: G4Pow.hh:108
G4double a
Definition: TRTMaterials.hh:39
int G4int
Definition: G4Types.hh:78
G4double logZ(G4int Z) const
Definition: G4Pow.hh:163
G4double maxA2
Definition: G4Pow.hh:112
G4double A23(G4double A) const
Definition: G4Pow.hh:157
G4double Z13(G4int Z) const
Definition: G4Pow.hh:127
G4DataVector pz13
Definition: G4Pow.hh:117
static G4Pow * fpInstance
Definition: G4Pow.hh:106
G4DataVector logfact
Definition: G4Pow.hh:122
const G4int max2
Definition: G4Pow.hh:109
G4double maxA
Definition: G4Pow.hh:111
G4double factorial(G4int Z) const
Definition: G4Pow.hh:266
const G4int n
G4double logBase(G4double x) const
Definition: G4Pow.hh:168
static const G4double A[nN]
G4double G4Log(G4double x)
Definition: G4Log.hh:230
G4double G4Exp(G4double initial_x)
Exponential Function double precision.
Definition: G4Exp.hh:183
G4double logX(G4double x) const
Definition: G4Pow.hh:199
G4DataVector fexp
Definition: G4Pow.hh:120
G4double A13(G4double A) const
Definition: G4Pow.hh:132
G4double logfactorial(G4int Z) const
Definition: G4Pow.hh:271
G4DataVector ener
Definition: G4Pow.hh:115
G4double logA(G4double A) const
Definition: G4Pow.hh:191
G4double Z23(G4int Z) const
Definition: G4Pow.hh:151
G4Pow()
Definition: G4Pow.cc:67
G4double powZ(G4int Z, G4double y) const
Definition: G4Pow.hh:256
double G4double
Definition: G4Types.hh:76
G4DataVector logen
Definition: G4Pow.hh:116
G4DataVector lz2
Definition: G4Pow.hh:119
G4double log10Z(G4int Z) const
Definition: G4Pow.hh:226
G4DataVector lz
Definition: G4Pow.hh:118