00001 // 00002 // ******************************************************************** 00003 // * License and Disclaimer * 00004 // * * 00005 // * The Geant4 software is copyright of the Copyright Holders of * 00006 // * the Geant4 Collaboration. It is provided under the terms and * 00007 // * conditions of the Geant4 Software License, included in the file * 00008 // * LICENSE and available at http://cern.ch/geant4/license . These * 00009 // * include a list of copyright holders. * 00010 // * * 00011 // * Neither the authors of this software system, nor their employing * 00012 // * institutes,nor the agencies providing financial support for this * 00013 // * work make any representation or warranty, express or implied, * 00014 // * regarding this software system or assume any liability for its * 00015 // * use. Please see the license in the file LICENSE and URL above * 00016 // * for the full disclaimer and the limitation of liability. * 00017 // * * 00018 // * This code implementation is the result of the scientific and * 00019 // * technical work of the GEANT4 collaboration. * 00020 // * By using, copying, modifying or distributing the software (or * 00021 // * any work based on the software) you agree to acknowledge its * 00022 // * use in resulting scientific publications, and indicate your * 00023 // * acceptance of all terms of the Geant4 Software license. * 00024 // ******************************************************************** 00025 // 00026 // $Id: G4Pow.hh 69386 2013-05-02 10:35:42Z vnivanch $ 00027 // 00028 // 00029 // ------------------------------------------------------------------- 00030 // 00031 // Class G4Pow 00032 // 00033 // Class description: 00034 // 00035 // Utility singleton class for the fast computation of log and pow 00036 // functions. Integer argument should in the interval 0-512, no 00037 // check is performed inside these methods for performance reasons. 00038 // For factorial integer argument should be in the interval 0-170 00039 // Computations with double arguments are fast for the interval 00040 // 0.5-255.5, standard library is used in the opposite case 00041 00042 // Author: Vladimir Ivanchenko 00043 // 00044 // Creation date: 23.05.2009 00045 // ------------------------------------------------------------------- 00046 00047 #ifndef G4Pow_h 00048 #define G4Pow_h 1 00049 00050 #include "globals.hh" 00051 #include "G4DataVector.hh" 00052 00053 class G4Pow 00054 { 00055 00056 public: 00057 00058 static G4Pow* GetInstance(); 00059 00060 // Fast computation of Z^1/3 00061 // 00062 inline G4double Z13(G4int Z); 00063 inline G4double A13(G4double A); 00064 00065 // Fast computation of Z^2/3 00066 // 00067 inline G4double Z23(G4int Z); 00068 inline G4double A23(G4double A); 00069 00070 // Fast computation of log(Z) 00071 // 00072 inline G4double logZ(G4int Z); 00073 inline G4double logA(G4double A); 00074 00075 // Fast computation of log10(Z) 00076 // 00077 inline G4double log10Z(G4int Z); 00078 inline G4double log10A(G4double A); 00079 00080 // Fast computation of pow(Z,X) 00081 // 00082 inline G4double powZ(G4int Z, G4double y); 00083 inline G4double powA(G4double A, G4double y); 00084 G4double powN(G4double x, G4int n); 00085 00086 // Fast factorial 00087 // 00088 inline G4double factorial(G4int Z); 00089 inline G4double logfactorial(G4int Z); 00090 00091 private: 00092 00093 G4Pow(); 00094 ~G4Pow(); 00095 00096 private: 00097 00098 static G4Pow* fpInstance; 00099 00100 const G4double onethird; 00101 const G4double minA; 00102 const G4double maxA; 00103 00104 G4DataVector pz13; 00105 G4DataVector lz; 00106 G4DataVector fact; 00107 G4DataVector logfact; 00108 }; 00109 00110 // ------------------------------------------------------------------- 00111 00112 inline G4double G4Pow::Z13(G4int Z) 00113 { 00114 return pz13[Z]; 00115 } 00116 00117 inline G4double G4Pow::A13(G4double A) 00118 { 00119 G4double res; 00120 G4double a = A; 00121 if(1.0 > A) { a = 1.0/A; } 00122 if(a <= maxA) 00123 { 00124 G4int i = G4int(a + 0.5); 00125 G4double x = (a/G4double(i) - 1.0)*onethird; 00126 res = pz13[i]*(1.0 + x - x*x*(1.0 - 1.66666666*x)); 00127 if(1.0 > A) { res = 1.0/res; } 00128 } 00129 else 00130 { 00131 res = std::pow(A, onethird); 00132 } 00133 return res; 00134 } 00135 00136 inline G4double G4Pow::Z23(G4int Z) 00137 { 00138 G4double x = Z13(Z); 00139 return x*x; 00140 } 00141 00142 inline G4double G4Pow::A23(G4double A) 00143 { 00144 G4double x = A13(A); 00145 return x*x; 00146 } 00147 00148 inline G4double G4Pow::logZ(G4int Z) 00149 { 00150 return lz[Z]; 00151 } 00152 00153 inline G4double G4Pow::logA(G4double A) 00154 { 00155 G4double res; 00156 G4double a = A; 00157 if(1.0 > A) { a = 1.0/A; } 00158 if(a <= maxA) 00159 { 00160 G4int i = G4int(a + 0.5); 00161 G4double x = a/G4double(i) - 1; 00162 res = lz[i] + x*(1.0 - (0.5 - onethird*x)*x); 00163 if(1.0 > A) { res = -res; } 00164 } 00165 else 00166 { 00167 res = std::log(A); 00168 } 00169 return res; 00170 } 00171 00172 inline G4double G4Pow::log10Z(G4int Z) 00173 { 00174 return lz[Z]/lz[10]; 00175 } 00176 00177 inline G4double G4Pow::log10A(G4double A) 00178 { 00179 return logA(A)/lz[10]; 00180 } 00181 00182 inline G4double G4Pow::powZ(G4int Z, G4double y) 00183 { 00184 return std::exp(y*lz[Z]); 00185 } 00186 00187 inline G4double G4Pow::powA(G4double A, G4double y) 00188 { 00189 return std::exp(y*logA(A)); 00190 } 00191 00192 inline G4double G4Pow::factorial(G4int Z) 00193 { 00194 return fact[Z]; 00195 } 00196 00197 inline G4double G4Pow::logfactorial(G4int Z) 00198 { 00199 return logfact[Z]; 00200 } 00201 00202 // ------------------------------------------------------------------- 00203 00204 #endif 00205