The Go Programming Language

Source file src/pkg/math/lgamma.go

     1	// Copyright 2010 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	package math
     6	
     7	/*
     8		Floating-point logarithm of the Gamma function.
     9	*/
    10	
    11	// The original C code and the long comment below are
    12	// from FreeBSD's /usr/src/lib/msun/src/e_lgamma_r.c and
    13	// came with this notice.  The go code is a simplified
    14	// version of the original C.
    15	//
    16	// ====================================================
    17	// Copyright (C) 1993 by Sun Microsystems, Inc. All rights reserved.
    18	//
    19	// Developed at SunPro, a Sun Microsystems, Inc. business.
    20	// Permission to use, copy, modify, and distribute this
    21	// software is freely granted, provided that this notice
    22	// is preserved.
    23	// ====================================================
    24	//
    25	// __ieee754_lgamma_r(x, signgamp)
    26	// Reentrant version of the logarithm of the Gamma function
    27	// with user provided pointer for the sign of Gamma(x).
    28	//
    29	// Method:
    30	//   1. Argument Reduction for 0 < x <= 8
    31	//      Since gamma(1+s)=s*gamma(s), for x in [0,8], we may
    32	//      reduce x to a number in [1.5,2.5] by
    33	//              lgamma(1+s) = log(s) + lgamma(s)
    34	//      for example,
    35	//              lgamma(7.3) = log(6.3) + lgamma(6.3)
    36	//                          = log(6.3*5.3) + lgamma(5.3)
    37	//                          = log(6.3*5.3*4.3*3.3*2.3) + lgamma(2.3)
    38	//   2. Polynomial approximation of lgamma around its
    39	//      minimum (ymin=1.461632144968362245) to maintain monotonicity.
    40	//      On [ymin-0.23, ymin+0.27] (i.e., [1.23164,1.73163]), use
    41	//              Let z = x-ymin;
    42	//              lgamma(x) = -1.214862905358496078218 + z**2*poly(z)
    43	//              poly(z) is a 14 degree polynomial.
    44	//   2. Rational approximation in the primary interval [2,3]
    45	//      We use the following approximation:
    46	//              s = x-2.0;
    47	//              lgamma(x) = 0.5*s + s*P(s)/Q(s)
    48	//      with accuracy
    49	//              |P/Q - (lgamma(x)-0.5s)| < 2**-61.71
    50	//      Our algorithms are based on the following observation
    51	//
    52	//                             zeta(2)-1    2    zeta(3)-1    3
    53	// lgamma(2+s) = s*(1-Euler) + --------- * s  -  --------- * s  + ...
    54	//                                 2                 3
    55	//
    56	//      where Euler = 0.5772156649... is the Euler constant, which
    57	//      is very close to 0.5.
    58	//
    59	//   3. For x>=8, we have
    60	//      lgamma(x)~(x-0.5)log(x)-x+0.5*log(2pi)+1/(12x)-1/(360x**3)+....
    61	//      (better formula:
    62	//         lgamma(x)~(x-0.5)*(log(x)-1)-.5*(log(2pi)-1) + ...)
    63	//      Let z = 1/x, then we approximation
    64	//              f(z) = lgamma(x) - (x-0.5)(log(x)-1)
    65	//      by
    66	//                                  3       5             11
    67	//              w = w0 + w1*z + w2*z  + w3*z  + ... + w6*z
    68	//      where
    69	//              |w - f(z)| < 2**-58.74
    70	//
    71	//   4. For negative x, since (G is gamma function)
    72	//              -x*G(-x)*G(x) = pi/sin(pi*x),
    73	//      we have
    74	//              G(x) = pi/(sin(pi*x)*(-x)*G(-x))
    75	//      since G(-x) is positive, sign(G(x)) = sign(sin(pi*x)) for x<0
    76	//      Hence, for x<0, signgam = sign(sin(pi*x)) and
    77	//              lgamma(x) = log(|Gamma(x)|)
    78	//                        = log(pi/(|x*sin(pi*x)|)) - lgamma(-x);
    79	//      Note: one should avoid computing pi*(-x) directly in the
    80	//            computation of sin(pi*(-x)).
    81	//
    82	//   5. Special Cases
    83	//              lgamma(2+s) ~ s*(1-Euler) for tiny s
    84	//              lgamma(1)=lgamma(2)=0
    85	//              lgamma(x) ~ -log(x) for tiny x
    86	//              lgamma(0) = lgamma(inf) = inf
    87	//              lgamma(-integer) = +-inf
    88	//
    89	//
    90	
    91	// Lgamma returns the natural logarithm and sign (-1 or +1) of Gamma(x).
    92	//
    93	// Special cases are:
    94	//	Lgamma(+Inf) = +Inf
    95	//	Lgamma(0) = +Inf
    96	//	Lgamma(-integer) = +Inf
    97	//	Lgamma(-Inf) = -Inf
    98	//	Lgamma(NaN) = NaN
    99	func Lgamma(x float64) (lgamma float64, sign int) {
   100		const (
   101			Ymin  = 1.461632144968362245
   102			Two52 = 1 << 52                     // 0x4330000000000000 ~4.5036e+15
   103			Two53 = 1 << 53                     // 0x4340000000000000 ~9.0072e+15
   104			Two58 = 1 << 58                     // 0x4390000000000000 ~2.8823e+17
   105			Tiny  = 1.0 / (1 << 70)             // 0x3b90000000000000 ~8.47033e-22
   106			A0    = 7.72156649015328655494e-02  // 0x3FB3C467E37DB0C8
   107			A1    = 3.22467033424113591611e-01  // 0x3FD4A34CC4A60FAD
   108			A2    = 6.73523010531292681824e-02  // 0x3FB13E001A5562A7
   109			A3    = 2.05808084325167332806e-02  // 0x3F951322AC92547B
   110			A4    = 7.38555086081402883957e-03  // 0x3F7E404FB68FEFE8
   111			A5    = 2.89051383673415629091e-03  // 0x3F67ADD8CCB7926B
   112			A6    = 1.19270763183362067845e-03  // 0x3F538A94116F3F5D
   113			A7    = 5.10069792153511336608e-04  // 0x3F40B6C689B99C00
   114			A8    = 2.20862790713908385557e-04  // 0x3F2CF2ECED10E54D
   115			A9    = 1.08011567247583939954e-04  // 0x3F1C5088987DFB07
   116			A10   = 2.52144565451257326939e-05  // 0x3EFA7074428CFA52
   117			A11   = 4.48640949618915160150e-05  // 0x3F07858E90A45837
   118			Tc    = 1.46163214496836224576e+00  // 0x3FF762D86356BE3F
   119			Tf    = -1.21486290535849611461e-01 // 0xBFBF19B9BCC38A42
   120			// Tt = -(tail of Tf)
   121			Tt  = -3.63867699703950536541e-18 // 0xBC50C7CAA48A971F
   122			T0  = 4.83836122723810047042e-01  // 0x3FDEF72BC8EE38A2
   123			T1  = -1.47587722994593911752e-01 // 0xBFC2E4278DC6C509
   124			T2  = 6.46249402391333854778e-02  // 0x3FB08B4294D5419B
   125			T3  = -3.27885410759859649565e-02 // 0xBFA0C9A8DF35B713
   126			T4  = 1.79706750811820387126e-02  // 0x3F9266E7970AF9EC
   127			T5  = -1.03142241298341437450e-02 // 0xBF851F9FBA91EC6A
   128			T6  = 6.10053870246291332635e-03  // 0x3F78FCE0E370E344
   129			T7  = -3.68452016781138256760e-03 // 0xBF6E2EFFB3E914D7
   130			T8  = 2.25964780900612472250e-03  // 0x3F6282D32E15C915
   131			T9  = -1.40346469989232843813e-03 // 0xBF56FE8EBF2D1AF1
   132			T10 = 8.81081882437654011382e-04  // 0x3F4CDF0CEF61A8E9
   133			T11 = -5.38595305356740546715e-04 // 0xBF41A6109C73E0EC
   134			T12 = 3.15632070903625950361e-04  // 0x3F34AF6D6C0EBBF7
   135			T13 = -3.12754168375120860518e-04 // 0xBF347F24ECC38C38
   136			T14 = 3.35529192635519073543e-04  // 0x3F35FD3EE8C2D3F4
   137			U0  = -7.72156649015328655494e-02 // 0xBFB3C467E37DB0C8
   138			U1  = 6.32827064025093366517e-01  // 0x3FE4401E8B005DFF
   139			U2  = 1.45492250137234768737e+00  // 0x3FF7475CD119BD6F
   140			U3  = 9.77717527963372745603e-01  // 0x3FEF497644EA8450
   141			U4  = 2.28963728064692451092e-01  // 0x3FCD4EAEF6010924
   142			U5  = 1.33810918536787660377e-02  // 0x3F8B678BBF2BAB09
   143			V1  = 2.45597793713041134822e+00  // 0x4003A5D7C2BD619C
   144			V2  = 2.12848976379893395361e+00  // 0x40010725A42B18F5
   145			V3  = 7.69285150456672783825e-01  // 0x3FE89DFBE45050AF
   146			V4  = 1.04222645593369134254e-01  // 0x3FBAAE55D6537C88
   147			V5  = 3.21709242282423911810e-03  // 0x3F6A5ABB57D0CF61
   148			S0  = -7.72156649015328655494e-02 // 0xBFB3C467E37DB0C8
   149			S1  = 2.14982415960608852501e-01  // 0x3FCB848B36E20878
   150			S2  = 3.25778796408930981787e-01  // 0x3FD4D98F4F139F59
   151			S3  = 1.46350472652464452805e-01  // 0x3FC2BB9CBEE5F2F7
   152			S4  = 2.66422703033638609560e-02  // 0x3F9B481C7E939961
   153			S5  = 1.84028451407337715652e-03  // 0x3F5E26B67368F239
   154			S6  = 3.19475326584100867617e-05  // 0x3F00BFECDD17E945
   155			R1  = 1.39200533467621045958e+00  // 0x3FF645A762C4AB74
   156			R2  = 7.21935547567138069525e-01  // 0x3FE71A1893D3DCDC
   157			R3  = 1.71933865632803078993e-01  // 0x3FC601EDCCFBDF27
   158			R4  = 1.86459191715652901344e-02  // 0x3F9317EA742ED475
   159			R5  = 7.77942496381893596434e-04  // 0x3F497DDACA41A95B
   160			R6  = 7.32668430744625636189e-06  // 0x3EDEBAF7A5B38140
   161			W0  = 4.18938533204672725052e-01  // 0x3FDACFE390C97D69
   162			W1  = 8.33333333333329678849e-02  // 0x3FB555555555553B
   163			W2  = -2.77777777728775536470e-03 // 0xBF66C16C16B02E5C
   164			W3  = 7.93650558643019558500e-04  // 0x3F4A019F98CF38B6
   165			W4  = -5.95187557450339963135e-04 // 0xBF4380CB8C0FE741
   166			W5  = 8.36339918996282139126e-04  // 0x3F4B67BA4CDAD5D1
   167			W6  = -1.63092934096575273989e-03 // 0xBF5AB89D0B9E43E4
   168		)
   169		// TODO(rsc): Remove manual inlining of IsNaN, IsInf
   170		// when compiler does it for us
   171		// special cases
   172		sign = 1
   173		switch {
   174		case x != x: // IsNaN(x):
   175			lgamma = x
   176			return
   177		case x < -MaxFloat64 || x > MaxFloat64: // IsInf(x, 0):
   178			lgamma = x
   179			return
   180		case x == 0:
   181			lgamma = Inf(1)
   182			return
   183		}
   184	
   185		neg := false
   186		if x < 0 {
   187			x = -x
   188			neg = true
   189		}
   190	
   191		if x < Tiny { // if |x| < 2**-70, return -log(|x|)
   192			if neg {
   193				sign = -1
   194			}
   195			lgamma = -Log(x)
   196			return
   197		}
   198		var nadj float64
   199		if neg {
   200			if x >= Two52 { // |x| >= 2**52, must be -integer
   201				lgamma = Inf(1)
   202				return
   203			}
   204			t := sinPi(x)
   205			if t == 0 {
   206				lgamma = Inf(1) // -integer
   207				return
   208			}
   209			nadj = Log(Pi / Fabs(t*x))
   210			if t < 0 {
   211				sign = -1
   212			}
   213		}
   214	
   215		switch {
   216		case x == 1 || x == 2: // purge off 1 and 2
   217			lgamma = 0
   218			return
   219		case x < 2: // use lgamma(x) = lgamma(x+1) - log(x)
   220			var y float64
   221			var i int
   222			if x <= 0.9 {
   223				lgamma = -Log(x)
   224				switch {
   225				case x >= (Ymin - 1 + 0.27): // 0.7316 <= x <=  0.9
   226					y = 1 - x
   227					i = 0
   228				case x >= (Ymin - 1 - 0.27): // 0.2316 <= x < 0.7316
   229					y = x - (Tc - 1)
   230					i = 1
   231				default: // 0 < x < 0.2316
   232					y = x
   233					i = 2
   234				}
   235			} else {
   236				lgamma = 0
   237				switch {
   238				case x >= (Ymin + 0.27): // 1.7316 <= x < 2
   239					y = 2 - x
   240					i = 0
   241				case x >= (Ymin - 0.27): // 1.2316 <= x < 1.7316
   242					y = x - Tc
   243					i = 1
   244				default: // 0.9 < x < 1.2316
   245					y = x - 1
   246					i = 2
   247				}
   248			}
   249			switch i {
   250			case 0:
   251				z := y * y
   252				p1 := A0 + z*(A2+z*(A4+z*(A6+z*(A8+z*A10))))
   253				p2 := z * (A1 + z*(A3+z*(A5+z*(A7+z*(A9+z*A11)))))
   254				p := y*p1 + p2
   255				lgamma += (p - 0.5*y)
   256			case 1:
   257				z := y * y
   258				w := z * y
   259				p1 := T0 + w*(T3+w*(T6+w*(T9+w*T12))) // parallel comp
   260				p2 := T1 + w*(T4+w*(T7+w*(T10+w*T13)))
   261				p3 := T2 + w*(T5+w*(T8+w*(T11+w*T14)))
   262				p := z*p1 - (Tt - w*(p2+y*p3))
   263				lgamma += (Tf + p)
   264			case 2:
   265				p1 := y * (U0 + y*(U1+y*(U2+y*(U3+y*(U4+y*U5)))))
   266				p2 := 1 + y*(V1+y*(V2+y*(V3+y*(V4+y*V5))))
   267				lgamma += (-0.5*y + p1/p2)
   268			}
   269		case x < 8: // 2 <= x < 8
   270			i := int(x)
   271			y := x - float64(i)
   272			p := y * (S0 + y*(S1+y*(S2+y*(S3+y*(S4+y*(S5+y*S6))))))
   273			q := 1 + y*(R1+y*(R2+y*(R3+y*(R4+y*(R5+y*R6)))))
   274			lgamma = 0.5*y + p/q
   275			z := 1.0 // Lgamma(1+s) = Log(s) + Lgamma(s)
   276			switch i {
   277			case 7:
   278				z *= (y + 6)
   279				fallthrough
   280			case 6:
   281				z *= (y + 5)
   282				fallthrough
   283			case 5:
   284				z *= (y + 4)
   285				fallthrough
   286			case 4:
   287				z *= (y + 3)
   288				fallthrough
   289			case 3:
   290				z *= (y + 2)
   291				lgamma += Log(z)
   292			}
   293		case x < Two58: // 8 <= x < 2**58
   294			t := Log(x)
   295			z := 1 / x
   296			y := z * z
   297			w := W0 + z*(W1+y*(W2+y*(W3+y*(W4+y*(W5+y*W6)))))
   298			lgamma = (x-0.5)*(t-1) + w
   299		default: // 2**58 <= x <= Inf
   300			lgamma = x * (Log(x) - 1)
   301		}
   302		if neg {
   303			lgamma = nadj - lgamma
   304		}
   305		return
   306	}
   307	
   308	// sinPi(x) is a helper function for negative x
   309	func sinPi(x float64) float64 {
   310		const (
   311			Two52 = 1 << 52 // 0x4330000000000000 ~4.5036e+15
   312			Two53 = 1 << 53 // 0x4340000000000000 ~9.0072e+15
   313		)
   314		if x < 0.25 {
   315			return -Sin(Pi * x)
   316		}
   317	
   318		// argument reduction
   319		z := Floor(x)
   320		var n int
   321		if z != x { // inexact
   322			x = Fmod(x, 2)
   323			n = int(x * 4)
   324		} else {
   325			if x >= Two53 { // x must be even
   326				x = 0
   327				n = 0
   328			} else {
   329				if x < Two52 {
   330					z = x + Two52 // exact
   331				}
   332				n = int(1 & Float64bits(z))
   333				x = float64(n)
   334				n <<= 2
   335			}
   336		}
   337		switch n {
   338		case 0:
   339			x = Sin(Pi * x)
   340		case 1, 2:
   341			x = Cos(Pi * (0.5 - x))
   342		case 3, 4:
   343			x = Sin(Pi * (1 - x))
   344		case 5, 6:
   345			x = -Cos(Pi * (x - 1.5))
   346		default:
   347			x = Sin(Pi * (x - 2))
   348		}
   349		return -x
   350	}

release.r60.3. Except as noted, this content is licensed under a Creative Commons Attribution 3.0 License.