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 10.5 ] [ 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

Diff markup

Differences between /processes/hadronic/models/lend/src/nf_gammaFunctions.cc (Version 11.3.0) and /processes/hadronic/models/lend/src/nf_gammaFunctions.cc (Version 9.4.p4)


  1 /*                            gamma.c               1 
  2  *                                                
  3  *    Gamma function                              
  4  *                                                
  5  * DESCRIPTION:                                   
  6  *                                                
  7  * Returns gamma function of the argument.  Th    
  8  * correctly signed, and the sign (+1 or -1) i    
  9  * returned in a global (extern) variable name    
 10  * This variable is also filled in by the loga    
 11  * function lgam().                               
 12  *                                                
 13  * Arguments |x| <= 34 are reduced by recurren    
 14  * approximated by a rational function of degr    
 15  * interval (2,3).  Large arguments are handle    
 16  * formula. Large negative arguments are made     
 17  * a reflection formula.                          
 18  *                                                
 19  *                                                
 20  * ACCURACY:                                      
 21  *                                                
 22  *                      Relative error:           
 23  * arithmetic   domain     # trials      peak     
 24  *    IEEE    -170,-33      20000       2.3e-1    
 25  *    IEEE     -33,  33     20000       9.4e-1    
 26  *    IEEE      33, 171.6   20000       2.3e-1    
 27  *                                                
 28  * Error for arguments outside the test range     
 29  * owing to error amplification by the exponen    
 30  *                                                
 31  */                                               
 32 /*                            lgam()              
 33  *                                                
 34  *    Natural logarithm of gamma function         
 35  *                                                
 36  *                                                
 37  * DESCRIPTION:                                   
 38  *                                                
 39  * Returns the base e (2.718...) logarithm of     
 40  * value of the gamma function of the argument    
 41  * The sign (+1 or -1) of the gamma function i    
 42  * global (extern) variable named sgngam.         
 43  *                                                
 44  * For arguments greater than 13, the logarith    
 45  * function is approximated by the logarithmic    
 46  * Stirling's formula using a polynomial appro    
 47  * degree 4. Arguments between -33 and +33 are    
 48  * recurrence to the interval [2,3] of a ratio    
 49  * The cosecant reflection formula is employed    
 50  * less than -33.                                 
 51  *                                                
 52  * Arguments greater than MAXLGM return DBL_MA    
 53  * message.  MAXLGM = 2.556348e305 for IEEE ar    
 54  *                                                
 55  *                                                
 56  * ACCURACY:                                      
 57  *                                                
 58  * arithmetic      domain        # trials         
 59  *    IEEE    0, 3                 28000     5    
 60  *    IEEE    2.718, 2.556e305     40000     3    
 61  * The error criterion was relative when the f    
 62  * was greater than one but absolute when it w    
 63  *                                                
 64  * The following test used the relative error     
 65  * at certain points the relative error could     
 66  * indicated.                                     
 67  *    IEEE    -200, -4             10000     4    
 68  *                                                
 69  */                                               
 70 /*                            gamma.c    */       
 71 /*    gamma function    */                        
 72                                                   
 73 /*                                                
 74 Cephes Math Library Release 2.8:  June, 2000      
 75 Copyright 1984, 1987, 1989, 1992, 2000 by Step    
 76 */                                                
 77                                                   
 78 #include "nf_specialFunctions.h"                  
 79                                                   
 80 #if defined __cplusplus                           
 81 #include "G4Exp.hh"                               
 82 #include "G4Log.hh"                               
 83 #include "G4Pow.hh"                               
 84 namespace GIDI {                                  
 85 using namespace GIDI;                             
 86 using namespace std;                              
 87 #endif                                            
 88                                                   
 89 static double P[] = { 1.60119522476751861407E-    
 90                       2.07448227648435975150E-    
 91 static double Q[] = { -2.31581873324120129819E    
 92                        3.58236398605498653373E    
 93 #define MAXGAM 171.624376956302725                
 94 static double LOGPI = 1.14472988584940017414;     
 95 static double SQTPI = 2.50662827463100050242E0    
 96                                                   
 97 /* Stirling's formula for the gamma function *    
 98 static double STIR[5] = { 7.873113957930936284    
 99 #define MAXSTIR 143.01608                         
100                                                   
101 static double stirf( double x, nfu_status *sta    
102 static double lgam( double x, int *sgngam, nfu    
103 /*                                                
104 **********************************************    
105 */                                                
106 static double stirf( double x, nfu_status * /*    
107 /* Gamma function computed by Stirling's formu    
108                                                   
109     double y, w, v;                               
110                                                   
111     w = 1.0 / x;                                  
112     w = 1.0 + w * nf_polevl( w, STIR, 4 );        
113     y = G4Exp( x );                               
114     if( x > MAXSTIR ) {                     /*    
115         v = G4Pow::GetInstance()->powA( x, 0.5    
116         y = v * (v / y); }                        
117     else {                                        
118         y = G4Pow::GetInstance()->powA( x, x -    
119     }                                             
120     y = SQTPI * y * w;                            
121     return( y );                                  
122 }                                                 
123 /*                                                
124 **********************************************    
125 */                                                
126 double nf_gammaFunction( double x, nfu_status     
127                                                   
128     double p, q, z;                               
129     int i, sgngam = 1;                            
130                                                   
131     *status = nfu_badInput;                       
132     if( !isfinite( x ) ) return( x );             
133     *status = nfu_Okay;                           
134                                                   
135     q = fabs( x );                                
136                                                   
137     if( q > 33.0 ) {                              
138         if( x < 0.0 ) {                           
139             p = floor( q );                       
140             if( p == q ) goto goverf;             
141             i = (int) p;                          
142             if( ( i & 1 ) == 0 ) sgngam = -1;     
143             z = q - p;                            
144             if( z > 0.5 ) {                       
145                 p += 1.0;                         
146                 z = q - p;                        
147             }                                     
148             z = q * sin( M_PI * z );              
149             if( z == 0.0 ) goto goverf;           
150             z = M_PI / ( fabs( z ) * stirf( q,    
151         }                                         
152         else {                                    
153             z = stirf( x, status );               
154         }                                         
155         return( sgngam * z );                     
156     }                                             
157                                                   
158     z = 1.0;                                      
159     while( x >= 3.0 ) {                           
160         x -= 1.0;                                 
161         z *= x;                                   
162     } // Loop checking, 11.06.2015, T. Koi        
163                                                   
164     while( x < 0.0 ) {                            
165         if( x > -1.E-9 ) goto small;              
166         z /= x;                                   
167         x += 1.0;                                 
168     } // Loop checking, 11.06.2015, T. Koi        
169                                                   
170     while( x < 2.0 ) {                            
171         if( x < 1.e-9 ) goto small;               
172         z /= x;                                   
173         x += 1.0;                                 
174     } // Loop checking, 11.06.2015, T. Koi        
175                                                   
176     if( x == 2.0 ) return( z );                   
177                                                   
178     x -= 2.0;                                     
179     p = nf_polevl( x, P, 6 );                     
180     q = nf_polevl( x, Q, 7 );                     
181     return( z * p / q );                          
182                                                   
183 small:                                            
184     if( x == 0.0 ) goto goverf;                   
185     return( z / ( ( 1.0 + 0.5772156649015329 *    
186                                                   
187 goverf:                                           
188     return( sgngam * DBL_MAX );                   
189 }                                                 
190                                                   
191 /* A[]: Stirling's formula expansion of log ga    
192 *  B[], C[]: log gamma function between 2 and     
193 */                                                
194 static double A[] = { 8.11614167470508450300E-    
195                      -2.77777777730099687205E-    
196 static double B[] = { -1.37825152569120859100E    
197                       -1.16237097492762307383E    
198 static double C[] = { -3.51815701436523470549E    
199                       -1.13933444367982507207E    
200 static double LS2PI  =  0.91893853320467274178    
201 #define MAXLGM 2.556348e305                       
202                                                   
203 /*                                                
204 **********************************************    
205 */                                                
206 double nf_logGammaFunction( double x, nfu_stat    
207 /* Logarithm of gamma function */                 
208                                                   
209     int sgngam;                                   
210                                                   
211     *status = nfu_badInput;                       
212     if( !isfinite( x ) ) return( x );             
213     *status = nfu_Okay;                           
214     return( lgam( x, &sgngam, status ) );         
215 }                                                 
216 /*                                                
217 **********************************************    
218 */                                                
219 static double lgam( double x, int *sgngam, nfu    
220                                                   
221     double p, q, u, w, z;                         
222     int i;                                        
223                                                   
224     *sgngam = 1;                                  
225                                                   
226     if( x < -34.0 ) {                             
227         q = -x;                                   
228         w = lgam( q, sgngam, status );            
229         p = floor( q );                           
230         if( p == q ) goto lgsing;                 
231         i = (int) p;                              
232         if( ( i & 1 ) == 0 ) {                    
233             *sgngam = -1; }                       
234         else {                                    
235             *sgngam = 1;                          
236         }                                         
237         z = q - p;                                
238         if( z > 0.5 ) {                           
239             p += 1.0;                             
240             z = p - q;                            
241         }                                         
242         z = q * sin( M_PI * z );                  
243         if( z == 0.0 ) goto lgsing;               
244         z = LOGPI - G4Log( z ) - w;               
245         return( z );                              
246     }                                             
247                                                   
248     if( x < 13.0 ) {                              
249         z = 1.0;                                  
250         p = 0.0;                                  
251         u = x;                                    
252         while( u >= 3.0 ) {                       
253             p -= 1.0;                             
254             u = x + p;                            
255             z *= u;                               
256         } // Loop checking, 11.06.2015, T. Koi    
257         while( u < 2.0 ) {                        
258             if( u == 0.0 ) goto lgsing;           
259             z /= u;                               
260             p += 1.0;                             
261             u = x + p;                            
262         } // Loop checking, 11.06.2015, T. Koi    
263         if( z < 0.0 ) {                           
264             *sgngam = -1;                         
265             z = -z; }                             
266         else {                                    
267             *sgngam = 1;                          
268         }                                         
269         if( u == 2.0 ) return( G4Log( z ) );      
270         p -= 2.0;                                 
271         x = x + p;                                
272         p = x * nf_polevl( x, B, 5 ) / nf_p1ev    
273         return( G4Log( z ) + p );                 
274     }                                             
275                                                   
276     if( x > MAXLGM ) goto lgsing;                 
277     q = ( x - 0.5 ) * G4Log( x ) - x + LS2PI;     
278     if( x > 1.0e8 ) return( q );                  
279                                                   
280     p = 1.0 / ( x * x );                          
281     if( x >= 1000.0 ) {                           
282         q += ( ( 7.9365079365079365079365e-4 *    
283     else {                                        
284         q += nf_polevl( p, A, 4 ) / x;            
285     }                                             
286     return( q );                                  
287                                                   
288 lgsing:                                           
289     return( *sgngam * DBL_MAX );                  
290 }                                                 
291                                                   
292 #if defined __cplusplus                           
293 }                                                 
294 #endif                                            
295