gamma.f

Go to the documentation of this file.
00001 *DECK GAMMA
00002       FUNCTION GAMMA (X)
00003 C***BEGIN PROLOGUE  GAMMA
00004 C***PURPOSE  Compute the complete Gamma function.
00005 C***LIBRARY   SLATEC (FNLIB)
00006 C***CATEGORY  C7A
00007 C***TYPE      SINGLE PRECISION (GAMMA-S, DGAMMA-D, CGAMMA-C)
00008 C***KEYWORDS  COMPLETE GAMMA FUNCTION, FNLIB, SPECIAL FUNCTIONS
00009 C***AUTHOR  Fullerton, W., (LANL)
00010 C***DESCRIPTION
00011 C
00012 C GAMMA computes the gamma function at X, where X is not 0, -1, -2, ....
00013 C GAMMA and X are single precision.
00014 C
00015 C***REFERENCES  (NONE)
00016 C***ROUTINES CALLED  CSEVL, GAMLIM, INITS, R1MACH, R9LGMC, XERMSG
00017 C***REVISION HISTORY  (YYMMDD)
00018 C   770601  DATE WRITTEN
00019 C   890531  Changed all specific intrinsics to generic.  (WRB)
00020 C   890531  REVISION DATE from Version 3.2
00021 C   891214  Prologue converted to Version 4.0 format.  (BAB)
00022 C   900315  CALLs to XERROR changed to CALLs to XERMSG.  (THJ)
00023 C***END PROLOGUE  GAMMA
00024       DIMENSION GCS(23)
00025       LOGICAL FIRST
00026       SAVE GCS, PI, SQ2PIL, NGCS, XMIN, XMAX, DXREL, FIRST
00027       DATA GCS   ( 1) / .0085711955 90989331E0/
00028       DATA GCS   ( 2) / .0044153813 24841007E0/
00029       DATA GCS   ( 3) / .0568504368 1599363E0/
00030       DATA GCS   ( 4) /-.0042198353 96418561E0/
00031       DATA GCS   ( 5) / .0013268081 81212460E0/
00032       DATA GCS   ( 6) /-.0001893024 529798880E0/
00033       DATA GCS   ( 7) / .0000360692 532744124E0/
00034       DATA GCS   ( 8) /-.0000060567 619044608E0/
00035       DATA GCS   ( 9) / .0000010558 295463022E0/
00036       DATA GCS   (10) /-.0000001811 967365542E0/
00037       DATA GCS   (11) / .0000000311 772496471E0/
00038       DATA GCS   (12) /-.0000000053 542196390E0/
00039       DATA GCS   (13) / .0000000009 193275519E0/
00040       DATA GCS   (14) /-.0000000001 577941280E0/
00041       DATA GCS   (15) / .0000000000 270798062E0/
00042       DATA GCS   (16) /-.0000000000 046468186E0/
00043       DATA GCS   (17) / .0000000000 007973350E0/
00044       DATA GCS   (18) /-.0000000000 001368078E0/
00045       DATA GCS   (19) / .0000000000 000234731E0/
00046       DATA GCS   (20) /-.0000000000 000040274E0/
00047       DATA GCS   (21) / .0000000000 000006910E0/
00048       DATA GCS   (22) /-.0000000000 000001185E0/
00049       DATA GCS   (23) / .0000000000 000000203E0/
00050       DATA PI /3.14159 26535 89793 24E0/
00051 C SQ2PIL IS LOG (SQRT (2.*PI) )
00052       DATA SQ2PIL /0.91893 85332 04672 74E0/
00053       DATA FIRST /.TRUE./
00054 C
00055 C LANL DEPENDENT CODE REMOVED 81.02.04
00056 C
00057 C***FIRST EXECUTABLE STATEMENT  GAMMA
00058       IF (FIRST) THEN
00059 C
00060 C ---------------------------------------------------------------------
00061 C INITIALIZE.  FIND LEGAL BOUNDS FOR X, AND DETERMINE THE NUMBER OF
00062 C TERMS IN THE SERIES REQUIRED TO ATTAIN AN ACCURACY TEN TIMES BETTER
00063 C THAN MACHINE PRECISION.
00064 C
00065          NGCS = INITS (GCS, 23, 0.1*R1MACH(3))
00066 C
00067          CALL GAMLIM (XMIN, XMAX)
00068          DXREL = SQRT (R1MACH(4))
00069 C
00070 C ---------------------------------------------------------------------
00071 C FINISH INITIALIZATION.  START EVALUATING GAMMA(X).
00072 C
00073       ENDIF
00074       FIRST = .FALSE.
00075 C
00076       Y = ABS(X)
00077       IF (Y.GT.10.0) GO TO 50
00078 C
00079 C COMPUTE GAMMA(X) FOR ABS(X) .LE. 10.0.  REDUCE INTERVAL AND
00080 C FIND GAMMA(1+Y) FOR 0. .LE. Y .LT. 1. FIRST OF ALL.
00081 C
00082       N = X
00083       IF (X.LT.0.) N = N - 1
00084       Y = X - N
00085       N = N - 1
00086       GAMMA = 0.9375 + CSEVL(2.*Y-1., GCS, NGCS)
00087       IF (N.EQ.0) RETURN
00088 C
00089       IF (N.GT.0) GO TO 30
00090 C
00091 C COMPUTE GAMMA(X) FOR X .LT. 1.
00092 C
00093       N = -N
00094       IF (X .EQ. 0.) CALL XERMSG ('SLATEC', 'GAMMA', 'X IS 0', 4, 2)
00095       IF (X .LT. 0. .AND. X+N-2 .EQ. 0.) CALL XERMSG ('SLATEC', 'GAMMA'
00096      1, 'X IS A NEGATIVE INTEGER', 4, 2)
00097       IF (X .LT. (-0.5) .AND. ABS((X-AINT(X-0.5))/X) .LT. DXREL) CALL
00098      1XERMSG ( 'SLATEC', 'GAMMA',
00099      2'ANSWER LT HALF PRECISION BECAUSE X TOO NEAR NEGATIVE INTEGER'
00100      3, 1, 1)
00101 C
00102       DO 20 I=1,N
00103         GAMMA = GAMMA / (X+I-1)
00104  20   CONTINUE
00105       RETURN
00106 C
00107 C GAMMA(X) FOR X .GE. 2.
00108 C
00109  30   DO 40 I=1,N
00110         GAMMA = (Y+I)*GAMMA
00111  40   CONTINUE
00112       RETURN
00113 C
00114 C COMPUTE GAMMA(X) FOR ABS(X) .GT. 10.0.  RECALL Y = ABS(X).
00115 C
00116  50   IF (X .GT. XMAX) CALL XERMSG ('SLATEC', 'GAMMA',
00117      +   'X SO BIG GAMMA OVERFLOWS', 3, 2)
00118 C
00119       GAMMA = 0.
00120       IF (X .LT. XMIN) CALL XERMSG ('SLATEC', 'GAMMA',
00121      +   'X SO SMALL GAMMA UNDERFLOWS', 2, 1)
00122       IF (X.LT.XMIN) RETURN
00123 C
00124       GAMMA = EXP((Y-0.5)*LOG(Y) - Y + SQ2PIL + R9LGMC(Y) )
00125       IF (X.GT.0.) RETURN
00126 C
00127       IF (ABS((X-AINT(X-0.5))/X) .LT. DXREL) CALL XERMSG ('SLATEC',
00128      +   'GAMMA',
00129      +   'ANSWER LT HALF PRECISION, X TOO NEAR NEGATIVE INTEGER', 1, 1)
00130 C
00131       SINPIY = SIN (PI*Y)
00132       IF (SINPIY .EQ. 0.) CALL XERMSG ('SLATEC', 'GAMMA',
00133      +   'X IS A NEGATIVE INTEGER', 4, 2)
00134 C
00135       GAMMA = -PI / (Y*SINPIY*GAMMA)
00136 C
00137       RETURN
00138       END
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines