September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -27,7 +27,7 @@ Function gammaLanczos (x As Double) As Double
Dim a As Double = p(0)
Dim t As Double = x + g + 0.5
For i As Integer = 1 To 7
For i As Integer = 1 To 8
a += p(i) / (x + i)
Next

View file

@ -1,10 +1,21 @@
cof :: [Double]
cof = [76.18009172947146,-86.50532032941677,24.01409824083091,-1.231739572450155,0.001208650973866179,-0.000005395239384953]
cof =
[ 76.18009172947146
, -86.50532032941677
, 24.01409824083091
, -1.231739572450155
, 0.001208650973866179
, -0.000005395239384953
]
ser :: Double
ser = 1.000000000190015
gammaln :: Double -> Double
gammaln xx = let tmp' = (xx+5.5) - (xx+0.5)*log(xx+5.5)
ser' = ser + sum (zipWith (/) cof [xx+1..])
in -tmp' + log(2.5066282746310005 * ser' / xx)
gammaln xx =
let tmp_ = (xx + 5.5) - (xx + 0.5) * log (xx + 5.5)
ser_ = ser + sum (zipWith (/) cof [xx + 1 ..])
in -tmp_ + log (2.5066282746310005 * ser_ / xx)
main :: IO ()
main = mapM_ print $ gammaln <$> [0.1,0.2 .. 1.0]

View file

@ -1,19 +1,20 @@
function gamma(x){
var p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028,
771.32342877765313, -176.61502916214059, 12.507343278686905,
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7];
var g = 7;
if(x < 0.5){
return Math.PI / (Math.sin(Math.PI * x)*gamma(1-x));
}
function gamma(x) {
var p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028,
771.32342877765313, -176.61502916214059, 12.507343278686905,
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7
];
x -= 1;
var a = p[0];
var t = x+g+0.5;
for(var i = 1; i < p.length; i++){
a += p[i]/(x+i);
}
var g = 7;
if (x < 0.5) {
return Math.PI / (Math.sin(Math.PI * x) * gamma(1 - x));
}
return Math.sqrt(2*Math.PI)*Math.pow(t, x+0.5)*Math.exp(-t)*a;
x -= 1;
var a = p[0];
var t = x + g + 0.5;
for (var i = 1; i < p.length; i++) {
a += p[i] / (x + i);
}
return Math.sqrt(2 * Math.PI) * Math.pow(t, x + 0.5) * Math.exp(-t) * a;
}

View file

@ -0,0 +1,35 @@
// version 1.0.6
fun gammaStirling(x: Double): Double = Math.sqrt(2.0 * Math.PI / x) * Math.pow(x / Math.E, x)
fun gammaLanczos(x: Double): Double {
var xx = x
val p = doubleArrayOf(
0.99999999999980993,
676.5203681218851,
-1259.1392167224028,
771.32342877765313,
-176.61502916214059,
12.507343278686905,
-0.13857109526572012,
9.9843695780195716e-6,
1.5056327351493116e-7
)
val g = 7
if (xx < 0.5) return Math.PI / (Math.sin(Math.PI * xx) * gammaLanczos(1.0 - xx))
xx--
var a = p[0]
val t = xx + g + 0.5
for (i in 1 until p.size) a += p[i] / (xx + i)
return Math.sqrt(2.0 * Math.PI) * Math.pow(t, xx + 0.5) * Math.exp(-t) * a
}
fun main(args: Array<String>) {
println(" x\tStirling\t\tLanczos\n")
for (i in 1 .. 20) {
val d = i / 10.0
print("%4.2f\t".format(d))
print("%17.15f\t".format(gammaStirling(d)))
println("%17.15f".format(gammaLanczos(d)))
}
}

View file

@ -0,0 +1,39 @@
sequence c = repeat(0,12)
function gamma(atom z)
atom accm = c[1]
if accm=0 then
accm = sqrt(2*PI)
c[1] = accm
atom k1_factrl = 1 -- (k - 1)!*(-1)^k with 0!==1
for k=2 to 12 do
c[k] = exp(13-k)*power(13-k,k-1.5)/k1_factrl
k1_factrl *= -(k-1)
end for
end if
for k=2 to 12 do
accm += c[k]/(z+k-1)
end for
accm *= exp(-(z+12))*power(z+12,z+0.5) -- Gamma(z+1)
return accm/z
end function
procedure sq(atom x, atom mul)
atom p = x*mul
printf(1,"%18.16g,%18.15g\n",{x,p*p})
end procedure
procedure si(atom x)
printf(1,"%18.15f\n",{x})
end procedure
sq(gamma(-3/2),3/4)
sq(gamma(-1/2),-1/2)
sq(gamma(1/2),1)
si(gamma(1))
sq(gamma(3/2),2)
si(gamma(2))
sq(gamma(5/2),4/3)
si(gamma(3))
sq(gamma(7/2),8/15)
si(gamma(4))

View file

@ -1,7 +1,7 @@
/*REXX program calculates GAMMA using the Taylor series coefficients, ≈80 decimal digits*/
/*The GAMMA function symbol is the Greek capital letter: Γ */
/*REXX program calculates GAMMA using the Taylor series coefficients; ≈80 decimal digits*/
/*The GAMMA function symbol is the Greek capital letter: Γ */
numeric digits 90 /*be able to handle extended precision.*/
parse arg LO HI . /*allow specification of gamma argument*/
parse arg LO HI . /*allow specification of gamma arg/args*/
/* [↓] either show a range or a ··· */
do j=word(LO 1, 1) to word(HI LO 9, 1) /* ··· single gamma value.*/
say 'gamma('j") =" gamma(j) /*compute gamma of J and display value.*/
@ -9,8 +9,8 @@ parse arg LO HI . /*allow specification of gamma
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
gamma: procedure; parse arg x; xm=x-1; sum=0
/*coefficients thanks to: Arne Fransén & Staffan Wrigge.*/
#.1 = 1 /* [↓] #.2 is the Euler-Mascheroni constant. */
/*coefficients thanks to: Arne Fransén & Staffan Wrigge.*/
#.1 = 1 /* [↓] #.2 is the Euler-Mascheroni constant. */
#.2 = 0.57721566490153286060651209008240243104215933593992359880576723488486772677766467
#.3 = -0.65587807152025388107701951514539048127976638047858434729236244568387083835372210
#.4 = -0.04200263503409523552900393487542981871139450040110609352206581297618009687597599
@ -62,7 +62,7 @@ gamma: procedure; parse arg x; xm=x-1; sum=0
#.50 = -0.00000000000000000000000000000000000000001056233178503581218600561071538285049997
#.51 = 0.00000000000000000000000000000000000000000026784429826430494783549630718908519485
#.52 = 0.00000000000000000000000000000000000000000002424715494851782689673032938370921241
#=52; do k=# by -1 for #
sum=sum*xm + #.k
end /*k*/
#=52; do k=# by -1 for #
sum=sum*xm + #.k
end /*k*/
return 1/sum

View file

@ -0,0 +1,62 @@
(import (scheme base)
(scheme inexact)
(scheme write))
(define PI 3.14159265358979323846264338327950)
(define e 2.7182818284590452353602875)
(define gamma-lanczos
(let ((p '(676.5203681218851 -1259.1392167224028 771.32342877765313
-176.61502916214059 12.507343278686905 -0.13857109526572012
9.9843695780195716e-6 1.5056327351493116e-7)))
(lambda (x)
(if (< x 0.5)
(/ PI (* (sin (* PI x)) (gamma-lanczos (- 1 x))))
(let* ((x2 (- x 1))
(t (+ x2 7 0.5))
(a (do ((ps p (cdr ps))
(idx 0 (+ 1 idx))
(res 0.99999999999980993 (+ res
(/ (car ps)
(+ x2 idx 1)))))
((null? ps) res))))
(* (sqrt (* 2 PI)) (expt t (+ x2 0.5)) (exp (- t)) a))))))
(define (gamma-stirling x)
(* (sqrt (* 2 (/ PI x))) (expt (/ x e) x)))
(define gamma-taylor
(let ((a (reverse
'(1.00000000000000000000 0.57721566490153286061
-0.65587807152025388108 -0.04200263503409523553
0.16653861138229148950 -0.04219773455554433675
-0.00962197152787697356 0.00721894324666309954
-0.00116516759185906511 -0.00021524167411495097
0.00012805028238811619 -0.00002013485478078824
-0.00000125049348214267 0.00000113302723198170
-0.00000020563384169776 0.00000000611609510448
0.00000000500200764447 -0.00000000118127457049
0.00000000010434267117 0.00000000000778226344
-0.00000000000369680562 0.00000000000051003703
-0.00000000000002058326 -0.00000000000000534812
0.00000000000000122678 -0.00000000000000011813
0.00000000000000000119 0.00000000000000000141
-0.00000000000000000023 0.00000000000000000002))))
(lambda (x)
(let ((y (- x 1)))
(do ((as a (cdr as))
(res 0 (+ (car as) (* res y))))
((null? as) (/ 1 res)))))))
(do ((i 0.1 (+ i 0.1)))
((> i 2.01) )
(display (string-append "Gamma ("
(number->string i)
"): "
"\n --- Lanczos : "
(number->string (gamma-lanczos i))
"\n --- Stirling: "
(number->string (gamma-stirling i))
"\n --- Taylor : "
(number->string (gamma-taylor i))
"\n")))

View file

@ -7,13 +7,13 @@ var a = [ 1.00000_00000_00000_00000, 0.57721_56649_01532_86061, -0.65587_80715_
0.00000_00001_04342_67117, 0.00000_00000_07782_26344, -0.00000_00000_03696_80562,
0.00000_00000_00510_03703, -0.00000_00000_00020_58326, -0.00000_00000_00005_34812,
0.00000_00000_00001_22678, -0.00000_00000_00000_11813, 0.00000_00000_00000_00119,
0.00000_00000_00000_00141, -0.00000_00000_00000_00023, 0.00000_00000_00000_00002 ];
0.00000_00000_00000_00141, -0.00000_00000_00000_00023, 0.00000_00000_00000_00002 ]
func gamma(x) {
var y = (x - 1);
1 / a.reverse.reduce {|sum, an| sum*y + an};
var y = (x - 1)
1 / a.reverse.reduce {|sum, an| sum*y + an}
}
for i in 1..10 {
say ("%.14e" % gamma(i/3));
say ("%.14e" % gamma(i/3))
}

View file

@ -3,36 +3,35 @@ func gamma(z) {
func withinepsilon(x) {
abs(x - abs(x)) <= epsilon
}
 
var p = [
676.5203681218851, -1259.1392167224028,
771.32342877765313, -176.61502916214059,
12.507343278686905, -0.13857109526572012,
9.9843695780195716e-6, 1.5056327351493116e-7,
]
var result;
z = Complex(z)
const pi = Complex.pi
if (z.real < 0.5) {
result = (pi / (sin(pi * z) * gamma(Complex(1) - z)))
 
var result = 0
const pi = Num.pi
 
if (z.re < 0.5) {
result = (pi / (sin(pi * z) * gamma(1 - z)))
}
else {
z -= 1
var x = 0.99999999999980993
 
p.each_kv { |i, v|
x += v/(z + i + 1)
}
 
var t = (z + p.len - 0.5)
result = (sqrt(pi*2) * t**(z+0.5) * exp(-t) * x)
}
withinepsilon(result.im) ? result.real : result
 
withinepsilon(result.im) ? result.re : result
}
 
for i in 1..10 {
say ("%.14e" % gamma(i/3));
say ("%.14e" % gamma(i/3))
}

View file

@ -1,11 +1,11 @@
define e = Math.e;
define pi = Math.pi;
define = Num.e
define τ = Num.tau
 
func Γ(t) {
t < 20 ? (__FUNC__(t + 1) / t)
: (Math.sqrt(2*pi*t) * Math.pow(t/e + 1/(12*e*t), t) / t);
t < 20 ? (__FUNC__(t + 1) / t)
 : (sqrt(τ*t) * pow(t/ + 1/(12**t), t) / t)
}
for i in 1..10 {
say ("%.14e" % Γ(i/3));
 
for i in (1..10) {
say ("%.14e" % Γ(i/3))
}

View file

@ -0,0 +1,23 @@
fcn taylorGamma(x){
var table = T(
0x1p+0, 0x1.2788cfc6fb618f4cp-1,
-0x1.4fcf4026afa2dcecp-1, -0x1.5815e8fa27047c8cp-5,
0x1.5512320b43fbe5dep-3, -0x1.59af103c340927bep-5,
-0x1.3b4af28483e214e4p-7, 0x1.d919c527f60b195ap-8,
-0x1.317112ce3a2a7bd2p-10, -0x1.c364fe6f1563ce9cp-13,
0x1.0c8a78cd9f9d1a78p-13, -0x1.51ce8af47eabdfdcp-16,
-0x1.4fad41fc34fbb2p-20, 0x1.302509dbc0de2c82p-20,
-0x1.b9986666c225d1d4p-23, 0x1.a44b7ba22d628acap-28,
0x1.57bc3fc384333fb2p-28, -0x1.44b4cedca388f7c6p-30,
0x1.cae7675c18606c6p-34, 0x1.11d065bfaf06745ap-37,
-0x1.0423bac8ca3faaa4p-38, 0x1.1f20151323cd0392p-41,
-0x1.72cb88ea5ae6e778p-46, -0x1.815f72a05f16f348p-48,
0x1.6198491a83bccbep-50, -0x1.10613dde57a88bd6p-53,
0x1.5e3fee81de0e9c84p-60, 0x1.a0dc770fb8a499b6p-60,
-0x1.0f635344a29e9f8ep-62, 0x1.43d79a4b90ce8044p-66).reverse();
y := x.toFloat() - 1.0;
sm := table[1,*].reduce('wrap(sm,an){ sm * y + an },table[0]);
return(1.0 / sm);
}

View file

@ -0,0 +1,28 @@
fcn lanczosGamma(z) { z = z.toFloat();
// Coefficients used by the GNU Scientific Library.
// http://en.wikipedia.org/wiki/Lanczos_approximation
const g = 7, PI = (0.0).pi;
exp := (0.0).e.pow;
var table = T(
0.99999_99999_99809_93,
676.52036_81218_851,
-1259.13921_67224_028,
771.32342_87776_5313,
-176.61502_91621_4059,
12.50734_32786_86905,
-0.13857_10952_65720_12,
9.98436_95780_19571_6e-6,
1.50563_27351_49311_6e-7);
// Reflection formula.
if (z < 0.5) {
return(PI / ((PI * z).sin() * lanczosGamma(1.0 - z)));
} else {
z -= 1;
x := table[0];
foreach i in ([1 .. g + 1]){
x += table[i] / (z + i); }
t := z + g + 0.5;
return((2.0 * PI).sqrt() * t.pow(z + 0.5) * exp(-t) * x);
}
}