dgamln.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /* dgamln.f -- translated by f2c (version 20100827).
  2. This file no longer depends on f2c.
  3. */
  4. #include "slatec-internal.hpp"
  5. /* Table of constant values */
  6. double dgamln_(double const *z__, integer *ierr)
  7. {
  8. /* Initialized data */
  9. static double const gln[100] = { 0.,0.,.693147180559945309,
  10. 1.791759469228055,3.17805383034794562,4.78749174278204599,
  11. 6.579251212010101,8.5251613610654143,10.6046029027452502,
  12. 12.8018274800814696,15.1044125730755153,17.5023078458738858,
  13. 19.9872144956618861,22.5521638531234229,25.1912211827386815,
  14. 27.8992713838408916,30.6718601060806728,33.5050734501368889,
  15. 36.3954452080330536,39.339884187199494,42.335616460753485,
  16. 45.380138898476908,48.4711813518352239,51.6066755677643736,
  17. 54.7847293981123192,58.0036052229805199,61.261701761002002,
  18. 64.5575386270063311,67.889743137181535,71.257038967168009,
  19. 74.6582363488301644,78.0922235533153106,81.5579594561150372,
  20. 85.0544670175815174,88.5808275421976788,92.1361756036870925,
  21. 95.7196945421432025,99.3306124547874269,102.968198614513813,
  22. 106.631760260643459,110.320639714757395,114.034211781461703,
  23. 117.771881399745072,121.533081515438634,125.317271149356895,
  24. 129.123933639127215,132.95257503561631,136.802722637326368,
  25. 140.673923648234259,144.565743946344886,148.477766951773032,
  26. 152.409592584497358,156.360836303078785,160.331128216630907,
  27. 164.320112263195181,168.327445448427652,172.352797139162802,
  28. 176.395848406997352,180.456291417543771,184.533828861449491,
  29. 188.628173423671591,192.739047287844902,196.866181672889994,
  30. 201.009316399281527,205.168199482641199,209.342586752536836,
  31. 213.532241494563261,217.736934113954227,221.956441819130334,
  32. 226.190548323727593,230.439043565776952,234.701723442818268,
  33. 238.978389561834323,243.268849002982714,247.572914096186884,
  34. 251.890402209723194,256.221135550009525,260.564940971863209,
  35. 264.921649798552801,269.291097651019823,273.673124285693704,
  36. 278.067573440366143,282.474292687630396,286.893133295426994,
  37. 291.323950094270308,295.766601350760624,300.220948647014132,
  38. 304.686856765668715,309.164193580146922,313.652829949879062,
  39. 318.152639620209327,322.663499126726177,327.185287703775217,
  40. 331.717887196928473,336.261181979198477,340.815058870799018,
  41. 345.379407062266854,349.954118040770237,354.539085519440809,
  42. 359.134205369575399 };
  43. static double const cf[22] = { .0833333333333333333,-.00277777777777777778,
  44. 7.93650793650793651e-4,-5.95238095238095238e-4,
  45. 8.41750841750841751e-4,-.00191752691752691753,
  46. .00641025641025641026,-.0295506535947712418,.179644372368830573,
  47. -1.39243221690590112,13.402864044168392,-156.848284626002017,
  48. 2193.10333333333333,-36108.7712537249894,691472.268851313067,
  49. -15238221.5394074162,382900751.391414141,-10882266035.7843911,
  50. 347320283765.002252,-12369602142269.2745,488788064793079.335,
  51. -21320333960919373.9 };
  52. static double const con = 1.83787706640934548;
  53. /* System generated locals */
  54. integer i__1;
  55. double ret_val;
  56. /* Local variables. Some initialized to avoid -Wmaybe-uninitialized */
  57. integer i__, k;
  58. double s, t1, fz, zm;
  59. integer mz, nz = 0;
  60. double zp;
  61. integer i1m;
  62. double fln, tlg, rln, trm, tst, zsq, zinc, zmin, zdmy, wdtol;
  63. /* ***BEGIN PROLOGUE DGAMLN */
  64. /* ***SUBSIDIARY */
  65. /* ***PURPOSE Compute the logarithm of the Gamma function */
  66. /* ***LIBRARY SLATEC */
  67. /* ***CATEGORY C7A */
  68. /* ***TYPE DOUBLE PRECISION (GAMLN-S, DGAMLN-D) */
  69. /* ***KEYWORDS LOGARITHM OF GAMMA FUNCTION */
  70. /* ***AUTHOR Amos, D. E., (SNL) */
  71. /* ***DESCRIPTION */
  72. /* **** A DOUBLE PRECISION ROUTINE **** */
  73. /* DGAMLN COMPUTES THE NATURAL LOG OF THE GAMMA FUNCTION FOR */
  74. /* Z.GT.0. THE ASYMPTOTIC EXPANSION IS USED TO GENERATE VALUES */
  75. /* GREATER THAN ZMIN WHICH ARE ADJUSTED BY THE RECURSION */
  76. /* G(Z+1)=Z*G(Z) FOR Z.LE.ZMIN. THE FUNCTION WAS MADE AS */
  77. /* PORTABLE AS POSSIBLE BY COMPUTING ZMIN FROM THE NUMBER OF BASE */
  78. /* 10 DIGITS IN A WORD, RLN=MAX(-ALOG10(R1MACH(4)),0.5E-18) */
  79. /* LIMITED TO 18 DIGITS OF (RELATIVE) ACCURACY. */
  80. /* SINCE INTEGER ARGUMENTS ARE COMMON, A TABLE LOOK UP ON 100 */
  81. /* VALUES IS USED FOR SPEED OF EXECUTION. */
  82. /* DESCRIPTION OF ARGUMENTS */
  83. /* INPUT Z IS D0UBLE PRECISION */
  84. /* Z - ARGUMENT, Z.GT.0.0D0 */
  85. /* OUTPUT DGAMLN IS DOUBLE PRECISION */
  86. /* DGAMLN - NATURAL LOG OF THE GAMMA FUNCTION AT Z.NE.0.0D0 */
  87. /* IERR - ERROR FLAG */
  88. /* IERR=0, NORMAL RETURN, COMPUTATION COMPLETED */
  89. /* IERR=1, Z.LE.0.0D0, NO COMPUTATION */
  90. /* ***REFERENCES COMPUTATION OF BESSEL FUNCTIONS OF COMPLEX ARGUMENT */
  91. /* BY D. E. AMOS, SAND83-0083, MAY, 1983. */
  92. /* ***ROUTINES CALLED D1MACH, I1MACH */
  93. /* ***REVISION HISTORY (YYMMDD) */
  94. /* 830501 DATE WRITTEN */
  95. /* 830501 REVISION DATE from Version 3.2 */
  96. /* 910415 Prologue converted to Version 4.0 format. (BAB) */
  97. /* 920128 Category corrected. (WRB) */
  98. /* 921215 DGAMLN defined for Z negative. (WRB) */
  99. /* ***END PROLOGUE DGAMLN */
  100. /* LNGAMMA(N), N=1,100 */
  101. /* COEFFICIENTS OF ASYMPTOTIC EXPANSION */
  102. /* LN(2*PI) */
  103. /* ***FIRST EXECUTABLE STATEMENT DGAMLN */
  104. *ierr = 0;
  105. if (*z__ <= 0.) {
  106. goto L70;
  107. }
  108. if (*z__ > 101.) {
  109. goto L10;
  110. }
  111. nz = (integer) (*z__);
  112. fz = *z__ - nz;
  113. if (fz > 0.) {
  114. goto L10;
  115. }
  116. if (nz > 100) {
  117. goto L10;
  118. }
  119. ret_val = gln[nz - 1];
  120. return ret_val;
  121. L10:
  122. wdtol = d1mach_(4);
  123. wdtol = max(wdtol,5e-19);
  124. i1m = i1mach_(14);
  125. rln = d1mach_(5) * i1m;
  126. fln = min(rln,20.);
  127. fln = max(fln,3.);
  128. fln += -3.;
  129. zm = fln * .3875 + 1.8;
  130. mz = (integer) (zm + 1);
  131. zmin = (double) mz;
  132. zdmy = *z__;
  133. zinc = 0.;
  134. if (*z__ >= zmin) {
  135. goto L20;
  136. }
  137. zinc = zmin - nz;
  138. zdmy = *z__ + zinc;
  139. L20:
  140. zp = 1. / zdmy;
  141. t1 = cf[0] * zp;
  142. s = t1;
  143. if (zp < wdtol) {
  144. goto L40;
  145. }
  146. zsq = zp * zp;
  147. tst = t1 * wdtol;
  148. for (k = 2; k <= 22; ++k) {
  149. zp *= zsq;
  150. trm = cf[k - 1] * zp;
  151. if (abs(trm) < tst) {
  152. goto L40;
  153. }
  154. s += trm;
  155. /* L30: */
  156. }
  157. L40:
  158. if (zinc != 0.) {
  159. goto L50;
  160. }
  161. tlg = log(*z__);
  162. ret_val = *z__ * (tlg - 1.) + (con - tlg) * .5 + s;
  163. return ret_val;
  164. L50:
  165. zp = 1.;
  166. nz = (integer) zinc;
  167. i__1 = nz;
  168. for (i__ = 1; i__ <= i__1; ++i__) {
  169. zp *= *z__ + (i__ - 1);
  170. /* L60: */
  171. }
  172. tlg = log(zdmy);
  173. ret_val = zdmy * (tlg - 1.) - log(zp) + (con - tlg) * .5 + s;
  174. return ret_val;
  175. L70:
  176. ret_val = d1mach_(2);
  177. *ierr = 1;
  178. return ret_val;
  179. } /* dgamln_ */