Geant4  10.00.p01
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 74256 2013-10-02 14:24:02Z 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 
63  // Fast computation of Z^1/3
64  //
65  inline G4double Z13(G4int Z) const;
66  inline G4double A13(G4double A) const;
67 
68  // Fast computation of Z^2/3
69  //
70  inline G4double Z23(G4int Z) const;
71  inline G4double A23(G4double A) const;
72 
73  // Fast computation of log(Z)
74  //
75  inline G4double logZ(G4int Z) const;
76  inline G4double logA(G4double A) const;
77  inline G4double logX(G4double x) const;
78 
79  // Fast computation of log10(Z)
80  //
81  inline G4double log10Z(G4int Z) const;
82  inline G4double log10A(G4double A) const;
83 
84  // Fast computation of exp(X)
85  //
86  inline G4double expA(G4double A) const;
87 
88  // Fast computation of pow(Z,X)
89  //
90  inline G4double powZ(G4int Z, G4double y) const;
91  inline G4double powA(G4double A, G4double y) const;
92  G4double powN(G4double x, G4int n) const;
93 
94  // Fast factorial
95  //
96  inline G4double factorial(G4int Z) const;
97  inline G4double logfactorial(G4int Z) const;
98 
99  private:
100 
101  G4Pow();
102  ~G4Pow();
103 
104  inline G4double logBase(G4double x) const;
105 
106  private:
107 
108  static G4Pow* fpInstance;
109 
111  const G4int max2;
112 
116 
125 };
126 
127 // -------------------------------------------------------------------
128 
129 inline G4double G4Pow::Z13(G4int Z) const
130 {
131  return pz13[Z];
132 }
133 
135 {
136  G4double res;
137  G4double a = 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  return res;
151 }
152 
153 inline G4double G4Pow::Z23(G4int Z) const
154 {
155  G4double x = Z13(Z);
156  return x*x;
157 }
158 
160 {
161  G4double x = A13(A);
162  return x*x;
163 }
164 
165 inline G4double G4Pow::logZ(G4int Z) const
166 {
167  return lz[Z];
168 }
169 
171 {
172  G4double res;
173  if(a <= maxA2)
174  {
175  G4int i = G4int(max2*(a - 1) + 0.5);
176  if(i > max2) { i = max2; }
177  G4double x = a/(G4double(i)/max2 + 1) - 1;
178  res = lz2[i] + x*(1.0 - (0.5 - onethird*x)*x);
179  }
180  else if(a <= maxA)
181  {
182  G4int i = G4int(a + 0.5);
183  G4double x = a/G4double(i) - 1;
184  res = lz[i] + x*(1.0 - (0.5 - onethird*x)*x);
185  }
186  else
187  {
188  res = G4Log(a);
189  }
190  return res;
191 }
192 
194 {
195  G4double res;
196  if(1.0 <= A) { res = logBase(A); }
197  else { res = -logBase(1./A); }
198  return res;
199 }
200 
202 {
203  G4double res = 0.0;
204  G4double a = x;
205  if(1.0 > x) { a = 1.0/x; }
206 
207  if(a <= maxA)
208  {
209  res = logBase(a);
210  }
211  else if(a <= ener[2])
212  {
213  res = logen[1] + logBase(a/ener[1]);
214  }
215  else if(a <= ener[3])
216  {
217  res = logen[2] + logBase(a/ener[2]);
218  }
219  else
220  {
221  res = G4Log(a);
222  }
223 
224  if(1.0 > x) { res = -res; }
225  return res;
226 }
227 
228 inline G4double G4Pow::log10Z(G4int Z) const
229 {
230  return lz[Z]/lz[10];
231 }
232 
234 {
235  return logX(A)/lz[10];
236 }
237 
239 {
240  G4double res;
241  G4double a = A;
242  if(0.0 > A) { a = -A; }
243 
244  if(a <= maxAexp)
245  {
246  G4int i = G4int(2*a + 0.5);
247  G4double x = a - i*0.5;
248  res = fexp[i]*(1.0 + x*(1.0 + 0.5*(1.0 + onethird*x)*x));
249  }
250  else
251  {
252  res = G4Exp(a);
253  }
254  if(0.0 > A) { res = 1.0/res; }
255  return res;
256 }
257 
258 inline G4double G4Pow::powZ(G4int Z, G4double y) const
259 {
260  return expA(y*lz[Z]);
261 }
262 
264 {
265  return expA(y*logX(A));
266 }
267 
269 {
270  return fact[Z];
271 }
272 
274 {
275  return logfact[Z];
276 }
277 
278 // -------------------------------------------------------------------
279 
280 #endif
static G4Pow * GetInstance()
Definition: G4Pow.cc:53
G4double powA(G4double A, G4double y) const
Definition: G4Pow.hh:263
G4DataVector fact
Definition: G4Pow.hh:123
G4double maxAexp
Definition: G4Pow.hh:115
~G4Pow()
Definition: G4Pow.cc:118
G4double powN(G4double x, G4int n) const
Definition: G4Pow.cc:125
Definition: G4Pow.hh:56
G4double expA(G4double A) const
Definition: G4Pow.hh:238
G4double log10A(G4double A) const
Definition: G4Pow.hh:233
const G4double onethird
Definition: G4Pow.hh:110
G4double a
Definition: TRTMaterials.hh:39
int G4int
Definition: G4Types.hh:78
G4double logZ(G4int Z) const
Definition: G4Pow.hh:165
G4double maxA2
Definition: G4Pow.hh:114
G4double A23(G4double A) const
Definition: G4Pow.hh:159
G4double Z13(G4int Z) const
Definition: G4Pow.hh:129
G4DataVector pz13
Definition: G4Pow.hh:119
static G4Pow * fpInstance
Definition: G4Pow.hh:108
G4DataVector logfact
Definition: G4Pow.hh:124
const G4int max2
Definition: G4Pow.hh:111
G4double maxA
Definition: G4Pow.hh:113
G4double factorial(G4int Z) const
Definition: G4Pow.hh:268
const G4int n
G4double logBase(G4double x) const
Definition: G4Pow.hh:170
static const G4double A[nN]
G4double G4Log(G4double x)
Definition: G4Log.hh:227
G4double G4Exp(G4double initial_x)
Exponential Function double precision.
Definition: G4Exp.hh:180
G4double logX(G4double x) const
Definition: G4Pow.hh:201
G4DataVector fexp
Definition: G4Pow.hh:122
G4double A13(G4double A) const
Definition: G4Pow.hh:134
G4double logfactorial(G4int Z) const
Definition: G4Pow.hh:273
G4DataVector ener
Definition: G4Pow.hh:117
G4double logA(G4double A) const
Definition: G4Pow.hh:193
G4double Z23(G4int Z) const
Definition: G4Pow.hh:153
G4Pow()
Definition: G4Pow.cc:64
G4double powZ(G4int Z, G4double y) const
Definition: G4Pow.hh:258
double G4double
Definition: G4Types.hh:76
G4DataVector logen
Definition: G4Pow.hh:118
G4DataVector lz2
Definition: G4Pow.hh:121
G4double log10Z(G4int Z) const
Definition: G4Pow.hh:228
G4DataVector lz
Definition: G4Pow.hh:120