mirror of
https://github.com/golang/go
synced 2024-11-20 03:24:41 -07:00
math: faster Lgamma
Converting from polynomial constants to counted array speeds up Lgamma from 51.3 to 37.7 ns/op. Variables renamed in Gamma to avoid overlap in Lgamma. R=rsc, golang-dev CC=golang-dev https://golang.org/cl/5359045
This commit is contained in:
parent
3b860269ee
commit
300b443ade
@ -63,7 +63,7 @@ package math
|
||||
// Stephen L. Moshier
|
||||
// moshier@na-net.ornl.gov
|
||||
|
||||
var _P = [...]float64{
|
||||
var _gamP = [...]float64{
|
||||
1.60119522476751861407e-04,
|
||||
1.19135147006586384913e-03,
|
||||
1.04213797561761569935e-02,
|
||||
@ -72,7 +72,7 @@ var _P = [...]float64{
|
||||
4.94214826801497100753e-01,
|
||||
9.99999999999999996796e-01,
|
||||
}
|
||||
var _Q = [...]float64{
|
||||
var _gamQ = [...]float64{
|
||||
-2.31581873324120129819e-05,
|
||||
5.39605580493303397842e-04,
|
||||
-4.45641913851797240494e-03,
|
||||
@ -82,7 +82,7 @@ var _Q = [...]float64{
|
||||
7.14304917030273074085e-02,
|
||||
1.00000000000000000320e+00,
|
||||
}
|
||||
var _S = [...]float64{
|
||||
var _gamS = [...]float64{
|
||||
7.87311395793093628397e-04,
|
||||
-2.29549961613378126380e-04,
|
||||
-2.68132617805781232825e-03,
|
||||
@ -98,7 +98,7 @@ func stirling(x float64) float64 {
|
||||
MaxStirling = 143.01608
|
||||
)
|
||||
w := 1 / x
|
||||
w = 1 + w*((((_S[0]*w+_S[1])*w+_S[2])*w+_S[3])*w+_S[4])
|
||||
w = 1 + w*((((_gamS[0]*w+_gamS[1])*w+_gamS[2])*w+_gamS[3])*w+_gamS[4])
|
||||
y := Exp(x)
|
||||
if x > MaxStirling { // avoid Pow() overflow
|
||||
v := Pow(x, 0.5*x-0.25)
|
||||
@ -176,8 +176,8 @@ func Gamma(x float64) float64 {
|
||||
}
|
||||
|
||||
x = x - 2
|
||||
p = (((((x*_P[0]+_P[1])*x+_P[2])*x+_P[3])*x+_P[4])*x+_P[5])*x + _P[6]
|
||||
q = ((((((x*_Q[0]+_Q[1])*x+_Q[2])*x+_Q[3])*x+_Q[4])*x+_Q[5])*x+_Q[6])*x + _Q[7]
|
||||
p = (((((x*_gamP[0]+_gamP[1])*x+_gamP[2])*x+_gamP[3])*x+_gamP[4])*x+_gamP[5])*x + _gamP[6]
|
||||
q = ((((((x*_gamQ[0]+_gamQ[1])*x+_gamQ[2])*x+_gamQ[3])*x+_gamQ[4])*x+_gamQ[5])*x+_gamQ[6])*x + _gamQ[7]
|
||||
return z * p / q
|
||||
|
||||
small:
|
||||
|
@ -88,6 +88,81 @@ package math
|
||||
//
|
||||
//
|
||||
|
||||
var _lgamA = [...]float64{
|
||||
7.72156649015328655494e-02, // 0x3FB3C467E37DB0C8
|
||||
3.22467033424113591611e-01, // 0x3FD4A34CC4A60FAD
|
||||
6.73523010531292681824e-02, // 0x3FB13E001A5562A7
|
||||
2.05808084325167332806e-02, // 0x3F951322AC92547B
|
||||
7.38555086081402883957e-03, // 0x3F7E404FB68FEFE8
|
||||
2.89051383673415629091e-03, // 0x3F67ADD8CCB7926B
|
||||
1.19270763183362067845e-03, // 0x3F538A94116F3F5D
|
||||
5.10069792153511336608e-04, // 0x3F40B6C689B99C00
|
||||
2.20862790713908385557e-04, // 0x3F2CF2ECED10E54D
|
||||
1.08011567247583939954e-04, // 0x3F1C5088987DFB07
|
||||
2.52144565451257326939e-05, // 0x3EFA7074428CFA52
|
||||
4.48640949618915160150e-05, // 0x3F07858E90A45837
|
||||
}
|
||||
var _lgamR = [...]float64{
|
||||
1.0, // placeholder
|
||||
1.39200533467621045958e+00, // 0x3FF645A762C4AB74
|
||||
7.21935547567138069525e-01, // 0x3FE71A1893D3DCDC
|
||||
1.71933865632803078993e-01, // 0x3FC601EDCCFBDF27
|
||||
1.86459191715652901344e-02, // 0x3F9317EA742ED475
|
||||
7.77942496381893596434e-04, // 0x3F497DDACA41A95B
|
||||
7.32668430744625636189e-06, // 0x3EDEBAF7A5B38140
|
||||
}
|
||||
var _lgamS = [...]float64{
|
||||
-7.72156649015328655494e-02, // 0xBFB3C467E37DB0C8
|
||||
2.14982415960608852501e-01, // 0x3FCB848B36E20878
|
||||
3.25778796408930981787e-01, // 0x3FD4D98F4F139F59
|
||||
1.46350472652464452805e-01, // 0x3FC2BB9CBEE5F2F7
|
||||
2.66422703033638609560e-02, // 0x3F9B481C7E939961
|
||||
1.84028451407337715652e-03, // 0x3F5E26B67368F239
|
||||
3.19475326584100867617e-05, // 0x3F00BFECDD17E945
|
||||
}
|
||||
var _lgamT = [...]float64{
|
||||
4.83836122723810047042e-01, // 0x3FDEF72BC8EE38A2
|
||||
-1.47587722994593911752e-01, // 0xBFC2E4278DC6C509
|
||||
6.46249402391333854778e-02, // 0x3FB08B4294D5419B
|
||||
-3.27885410759859649565e-02, // 0xBFA0C9A8DF35B713
|
||||
1.79706750811820387126e-02, // 0x3F9266E7970AF9EC
|
||||
-1.03142241298341437450e-02, // 0xBF851F9FBA91EC6A
|
||||
6.10053870246291332635e-03, // 0x3F78FCE0E370E344
|
||||
-3.68452016781138256760e-03, // 0xBF6E2EFFB3E914D7
|
||||
2.25964780900612472250e-03, // 0x3F6282D32E15C915
|
||||
-1.40346469989232843813e-03, // 0xBF56FE8EBF2D1AF1
|
||||
8.81081882437654011382e-04, // 0x3F4CDF0CEF61A8E9
|
||||
-5.38595305356740546715e-04, // 0xBF41A6109C73E0EC
|
||||
3.15632070903625950361e-04, // 0x3F34AF6D6C0EBBF7
|
||||
-3.12754168375120860518e-04, // 0xBF347F24ECC38C38
|
||||
3.35529192635519073543e-04, // 0x3F35FD3EE8C2D3F4
|
||||
}
|
||||
var _lgamU = [...]float64{
|
||||
-7.72156649015328655494e-02, // 0xBFB3C467E37DB0C8
|
||||
6.32827064025093366517e-01, // 0x3FE4401E8B005DFF
|
||||
1.45492250137234768737e+00, // 0x3FF7475CD119BD6F
|
||||
9.77717527963372745603e-01, // 0x3FEF497644EA8450
|
||||
2.28963728064692451092e-01, // 0x3FCD4EAEF6010924
|
||||
1.33810918536787660377e-02, // 0x3F8B678BBF2BAB09
|
||||
}
|
||||
var _lgamV = [...]float64{
|
||||
1.0,
|
||||
2.45597793713041134822e+00, // 0x4003A5D7C2BD619C
|
||||
2.12848976379893395361e+00, // 0x40010725A42B18F5
|
||||
7.69285150456672783825e-01, // 0x3FE89DFBE45050AF
|
||||
1.04222645593369134254e-01, // 0x3FBAAE55D6537C88
|
||||
3.21709242282423911810e-03, // 0x3F6A5ABB57D0CF61
|
||||
}
|
||||
var _lgamW = [...]float64{
|
||||
4.18938533204672725052e-01, // 0x3FDACFE390C97D69
|
||||
8.33333333333329678849e-02, // 0x3FB555555555553B
|
||||
-2.77777777728775536470e-03, // 0xBF66C16C16B02E5C
|
||||
7.93650558643019558500e-04, // 0x3F4A019F98CF38B6
|
||||
-5.95187557450339963135e-04, // 0xBF4380CB8C0FE741
|
||||
8.36339918996282139126e-04, // 0x3F4B67BA4CDAD5D1
|
||||
-1.63092934096575273989e-03, // 0xBF5AB89D0B9E43E4
|
||||
}
|
||||
|
||||
// Lgamma returns the natural logarithm and sign (-1 or +1) of Gamma(x).
|
||||
//
|
||||
// Special cases are:
|
||||
@ -103,68 +178,10 @@ func Lgamma(x float64) (lgamma float64, sign int) {
|
||||
Two53 = 1 << 53 // 0x4340000000000000 ~9.0072e+15
|
||||
Two58 = 1 << 58 // 0x4390000000000000 ~2.8823e+17
|
||||
Tiny = 1.0 / (1 << 70) // 0x3b90000000000000 ~8.47033e-22
|
||||
A0 = 7.72156649015328655494e-02 // 0x3FB3C467E37DB0C8
|
||||
A1 = 3.22467033424113591611e-01 // 0x3FD4A34CC4A60FAD
|
||||
A2 = 6.73523010531292681824e-02 // 0x3FB13E001A5562A7
|
||||
A3 = 2.05808084325167332806e-02 // 0x3F951322AC92547B
|
||||
A4 = 7.38555086081402883957e-03 // 0x3F7E404FB68FEFE8
|
||||
A5 = 2.89051383673415629091e-03 // 0x3F67ADD8CCB7926B
|
||||
A6 = 1.19270763183362067845e-03 // 0x3F538A94116F3F5D
|
||||
A7 = 5.10069792153511336608e-04 // 0x3F40B6C689B99C00
|
||||
A8 = 2.20862790713908385557e-04 // 0x3F2CF2ECED10E54D
|
||||
A9 = 1.08011567247583939954e-04 // 0x3F1C5088987DFB07
|
||||
A10 = 2.52144565451257326939e-05 // 0x3EFA7074428CFA52
|
||||
A11 = 4.48640949618915160150e-05 // 0x3F07858E90A45837
|
||||
Tc = 1.46163214496836224576e+00 // 0x3FF762D86356BE3F
|
||||
Tf = -1.21486290535849611461e-01 // 0xBFBF19B9BCC38A42
|
||||
// Tt = -(tail of Tf)
|
||||
Tt = -3.63867699703950536541e-18 // 0xBC50C7CAA48A971F
|
||||
T0 = 4.83836122723810047042e-01 // 0x3FDEF72BC8EE38A2
|
||||
T1 = -1.47587722994593911752e-01 // 0xBFC2E4278DC6C509
|
||||
T2 = 6.46249402391333854778e-02 // 0x3FB08B4294D5419B
|
||||
T3 = -3.27885410759859649565e-02 // 0xBFA0C9A8DF35B713
|
||||
T4 = 1.79706750811820387126e-02 // 0x3F9266E7970AF9EC
|
||||
T5 = -1.03142241298341437450e-02 // 0xBF851F9FBA91EC6A
|
||||
T6 = 6.10053870246291332635e-03 // 0x3F78FCE0E370E344
|
||||
T7 = -3.68452016781138256760e-03 // 0xBF6E2EFFB3E914D7
|
||||
T8 = 2.25964780900612472250e-03 // 0x3F6282D32E15C915
|
||||
T9 = -1.40346469989232843813e-03 // 0xBF56FE8EBF2D1AF1
|
||||
T10 = 8.81081882437654011382e-04 // 0x3F4CDF0CEF61A8E9
|
||||
T11 = -5.38595305356740546715e-04 // 0xBF41A6109C73E0EC
|
||||
T12 = 3.15632070903625950361e-04 // 0x3F34AF6D6C0EBBF7
|
||||
T13 = -3.12754168375120860518e-04 // 0xBF347F24ECC38C38
|
||||
T14 = 3.35529192635519073543e-04 // 0x3F35FD3EE8C2D3F4
|
||||
U0 = -7.72156649015328655494e-02 // 0xBFB3C467E37DB0C8
|
||||
U1 = 6.32827064025093366517e-01 // 0x3FE4401E8B005DFF
|
||||
U2 = 1.45492250137234768737e+00 // 0x3FF7475CD119BD6F
|
||||
U3 = 9.77717527963372745603e-01 // 0x3FEF497644EA8450
|
||||
U4 = 2.28963728064692451092e-01 // 0x3FCD4EAEF6010924
|
||||
U5 = 1.33810918536787660377e-02 // 0x3F8B678BBF2BAB09
|
||||
V1 = 2.45597793713041134822e+00 // 0x4003A5D7C2BD619C
|
||||
V2 = 2.12848976379893395361e+00 // 0x40010725A42B18F5
|
||||
V3 = 7.69285150456672783825e-01 // 0x3FE89DFBE45050AF
|
||||
V4 = 1.04222645593369134254e-01 // 0x3FBAAE55D6537C88
|
||||
V5 = 3.21709242282423911810e-03 // 0x3F6A5ABB57D0CF61
|
||||
S0 = -7.72156649015328655494e-02 // 0xBFB3C467E37DB0C8
|
||||
S1 = 2.14982415960608852501e-01 // 0x3FCB848B36E20878
|
||||
S2 = 3.25778796408930981787e-01 // 0x3FD4D98F4F139F59
|
||||
S3 = 1.46350472652464452805e-01 // 0x3FC2BB9CBEE5F2F7
|
||||
S4 = 2.66422703033638609560e-02 // 0x3F9B481C7E939961
|
||||
S5 = 1.84028451407337715652e-03 // 0x3F5E26B67368F239
|
||||
S6 = 3.19475326584100867617e-05 // 0x3F00BFECDD17E945
|
||||
R1 = 1.39200533467621045958e+00 // 0x3FF645A762C4AB74
|
||||
R2 = 7.21935547567138069525e-01 // 0x3FE71A1893D3DCDC
|
||||
R3 = 1.71933865632803078993e-01 // 0x3FC601EDCCFBDF27
|
||||
R4 = 1.86459191715652901344e-02 // 0x3F9317EA742ED475
|
||||
R5 = 7.77942496381893596434e-04 // 0x3F497DDACA41A95B
|
||||
R6 = 7.32668430744625636189e-06 // 0x3EDEBAF7A5B38140
|
||||
W0 = 4.18938533204672725052e-01 // 0x3FDACFE390C97D69
|
||||
W1 = 8.33333333333329678849e-02 // 0x3FB555555555553B
|
||||
W2 = -2.77777777728775536470e-03 // 0xBF66C16C16B02E5C
|
||||
W3 = 7.93650558643019558500e-04 // 0x3F4A019F98CF38B6
|
||||
W4 = -5.95187557450339963135e-04 // 0xBF4380CB8C0FE741
|
||||
W5 = 8.36339918996282139126e-04 // 0x3F4B67BA4CDAD5D1
|
||||
W6 = -1.63092934096575273989e-03 // 0xBF5AB89D0B9E43E4
|
||||
Tt = -3.63867699703950536541e-18 // 0xBC50C7CAA48A971F
|
||||
)
|
||||
// TODO(rsc): Remove manual inlining of IsNaN, IsInf
|
||||
// when compiler does it for us
|
||||
@ -249,28 +266,28 @@ func Lgamma(x float64) (lgamma float64, sign int) {
|
||||
switch i {
|
||||
case 0:
|
||||
z := y * y
|
||||
p1 := A0 + z*(A2+z*(A4+z*(A6+z*(A8+z*A10))))
|
||||
p2 := z * (A1 + z*(A3+z*(A5+z*(A7+z*(A9+z*A11)))))
|
||||
p1 := _lgamA[0] + z*(_lgamA[2]+z*(_lgamA[4]+z*(_lgamA[6]+z*(_lgamA[8]+z*_lgamA[10]))))
|
||||
p2 := z * (_lgamA[1] + z*(+_lgamA[3]+z*(_lgamA[5]+z*(_lgamA[7]+z*(_lgamA[9]+z*_lgamA[11])))))
|
||||
p := y*p1 + p2
|
||||
lgamma += (p - 0.5*y)
|
||||
case 1:
|
||||
z := y * y
|
||||
w := z * y
|
||||
p1 := T0 + w*(T3+w*(T6+w*(T9+w*T12))) // parallel comp
|
||||
p2 := T1 + w*(T4+w*(T7+w*(T10+w*T13)))
|
||||
p3 := T2 + w*(T5+w*(T8+w*(T11+w*T14)))
|
||||
p1 := _lgamT[0] + w*(_lgamT[3]+w*(_lgamT[6]+w*(_lgamT[9]+w*_lgamT[12]))) // parallel comp
|
||||
p2 := _lgamT[1] + w*(_lgamT[4]+w*(_lgamT[7]+w*(_lgamT[10]+w*_lgamT[13])))
|
||||
p3 := _lgamT[2] + w*(_lgamT[5]+w*(_lgamT[8]+w*(_lgamT[11]+w*_lgamT[14])))
|
||||
p := z*p1 - (Tt - w*(p2+y*p3))
|
||||
lgamma += (Tf + p)
|
||||
case 2:
|
||||
p1 := y * (U0 + y*(U1+y*(U2+y*(U3+y*(U4+y*U5)))))
|
||||
p2 := 1 + y*(V1+y*(V2+y*(V3+y*(V4+y*V5))))
|
||||
p1 := y * (_lgamU[0] + y*(_lgamU[1]+y*(_lgamU[2]+y*(_lgamU[3]+y*(_lgamU[4]+y*_lgamU[5])))))
|
||||
p2 := 1 + y*(_lgamV[1]+y*(_lgamV[2]+y*(_lgamV[3]+y*(_lgamV[4]+y*_lgamV[5]))))
|
||||
lgamma += (-0.5*y + p1/p2)
|
||||
}
|
||||
case x < 8: // 2 <= x < 8
|
||||
i := int(x)
|
||||
y := x - float64(i)
|
||||
p := y * (S0 + y*(S1+y*(S2+y*(S3+y*(S4+y*(S5+y*S6))))))
|
||||
q := 1 + y*(R1+y*(R2+y*(R3+y*(R4+y*(R5+y*R6)))))
|
||||
p := y * (_lgamS[0] + y*(_lgamS[1]+y*(_lgamS[2]+y*(_lgamS[3]+y*(_lgamS[4]+y*(_lgamS[5]+y*_lgamS[6]))))))
|
||||
q := 1 + y*(_lgamR[1]+y*(_lgamR[2]+y*(_lgamR[3]+y*(_lgamR[4]+y*(_lgamR[5]+y*_lgamR[6])))))
|
||||
lgamma = 0.5*y + p/q
|
||||
z := 1.0 // Lgamma(1+s) = Log(s) + Lgamma(s)
|
||||
switch i {
|
||||
@ -294,7 +311,7 @@ func Lgamma(x float64) (lgamma float64, sign int) {
|
||||
t := Log(x)
|
||||
z := 1 / x
|
||||
y := z * z
|
||||
w := W0 + z*(W1+y*(W2+y*(W3+y*(W4+y*(W5+y*W6)))))
|
||||
w := _lgamW[0] + z*(_lgamW[1]+y*(_lgamW[2]+y*(_lgamW[3]+y*(_lgamW[4]+y*(_lgamW[5]+y*_lgamW[6])))))
|
||||
lgamma = (x-0.5)*(t-1) + w
|
||||
default: // 2**58 <= x <= Inf
|
||||
lgamma = x * (Log(x) - 1)
|
||||
|
Loading…
Reference in New Issue
Block a user