Geant4  10.02.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 93311 2015-10-16 10:16: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 = 0.0;
135  if(A > 0.0)
136  {
137  G4double a = (1.0 <= A) ? A : 1.0/A;
138  if(1.0 > A) { a = 1.0/A; }
139  if(a <= maxA)
140  {
141  G4int i = G4int(a + 0.5);
142  G4double x = (a/G4double(i) - 1.0)*onethird;
143  res = pz13[i]*(1.0 + x - x*x*(1.0 - 1.66666666*x));
144  if(1.0 > A) { res = 1.0/res; }
145  }
146  else
147  {
148  res = std::pow(A, onethird);
149  }
150  }
151  return res;
152 }
153 
154 inline G4double G4Pow::Z23(G4int Z) const
155 {
156  G4double x = Z13(Z);
157  return x*x;
158 }
159 
161 {
162  G4double x = A13(A);
163  return x*x;
164 }
165 
166 inline G4double G4Pow::logZ(G4int Z) const
167 {
168  return lz[Z];
169 }
170 
172 {
173  G4double res;
174  if(a <= maxA2)
175  {
176  G4int i = G4int(max2*(a - 1) + 0.5);
177  if(i > max2) { i = max2; }
178  G4double x = a/(G4double(i)/max2 + 1) - 1;
179  res = lz2[i] + x*(1.0 - (0.5 - onethird*x)*x);
180  }
181  else if(a <= maxA)
182  {
183  G4int i = G4int(a + 0.5);
184  G4double x = a/G4double(i) - 1;
185  res = lz[i] + x*(1.0 - (0.5 - onethird*x)*x);
186  }
187  else
188  {
189  res = G4Log(a);
190  }
191  return res;
192 }
193 
195 {
196  return (1.0 <= A ? logBase(A) : -logBase(1./A));
197 }
198 
200 {
201  G4double res = 0.0;
202  G4double a = (1.0 <= x) ? x : 1.0/x;
203 
204  if(a <= maxA)
205  {
206  res = logBase(a);
207  }
208  else if(a <= ener[2])
209  {
210  res = logen[1] + logBase(a/ener[1]);
211  }
212  else if(a <= ener[3])
213  {
214  res = logen[2] + logBase(a/ener[2]);
215  }
216  else
217  {
218  res = G4Log(a);
219  }
220 
221  if(1.0 > x) { res = -res; }
222  return res;
223 }
224 
225 inline G4double G4Pow::log10Z(G4int Z) const
226 {
227  return lz[Z]/lz[10];
228 }
229 
231 {
232  return logX(A)/lz[10];
233 }
234 
236 {
237  G4double res;
238  G4double a = (0.0 <= A) ? A : -A;
239 
240  if(a <= maxAexp)
241  {
242  G4int i = G4int(2*a + 0.5);
243  G4double x = a - i*0.5;
244  res = fexp[i]*(1.0 + x*(1.0 + 0.5*(1.0 + onethird*x)*x));
245  }
246  else
247  {
248  res = G4Exp(a);
249  }
250  if(0.0 > A) { res = 1.0/res; }
251  return res;
252 }
253 
254 inline G4double G4Pow::powZ(G4int Z, G4double y) const
255 {
256  return expA(y*lz[Z]);
257 }
258 
260 {
261  return (0.0 == A ? 0.0 : expA(y*logX(A)));
262 }
263 
265 {
266  return fact[Z];
267 }
268 
270 {
271  return logfact[Z];
272 }
273 
274 // -------------------------------------------------------------------
275 
276 #endif
static G4Pow * GetInstance()
Definition: G4Pow.cc:55
G4double powA(G4double A, G4double y) const
Definition: G4Pow.hh:259
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:235
G4double log10A(G4double A) const
Definition: G4Pow.hh:230
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:166
G4double maxA2
Definition: G4Pow.hh:112
G4double A23(G4double A) const
Definition: G4Pow.hh:160
G4double Z13(G4int Z) const
Definition: G4Pow.hh:127
double A(double temperature)
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:264
const G4int n
G4double logBase(G4double x) const
Definition: G4Pow.hh:171
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:269
G4DataVector ener
Definition: G4Pow.hh:115
const G4double x[NPOINTSGL]
G4double logA(G4double A) const
Definition: G4Pow.hh:194
G4double Z23(G4int Z) const
Definition: G4Pow.hh:154
G4Pow()
Definition: G4Pow.cc:67
G4double powZ(G4int Z, G4double y) const
Definition: G4Pow.hh:254
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:225
G4DataVector lz
Definition: G4Pow.hh:118