Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
3
Task/Gamma-function/00-META.yaml
Normal file
3
Task/Gamma-function/00-META.yaml
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
from: http://rosettacode.org/wiki/Gamma_function
|
||||
note: Mathematical operations
|
||||
17
Task/Gamma-function/00-TASK.txt
Normal file
17
Task/Gamma-function/00-TASK.txt
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
;Task:
|
||||
Implement one algorithm (or more) to compute the [[wp:Gamma function|Gamma]] (<math>\Gamma</math>) function (in the real field only).
|
||||
|
||||
If your language has the function as built-in or you know a library which has it, compare your implementation's results with the results of the built-in/library function.
|
||||
|
||||
The Gamma function can be defined as:
|
||||
|
||||
:::::: <big><big> <math>\Gamma(x) = \displaystyle\int_0^\infty t^{x-1}e^{-t} dt</math></big></big>
|
||||
|
||||
This suggests a straightforward (but inefficient) way of computing the <math>\Gamma</math> through numerical integration.
|
||||
|
||||
|
||||
Better suggested methods:
|
||||
* [[wp:Lanczos approximation|Lanczos approximation]]
|
||||
* [[wp:Stirling's approximation|Stirling's approximation]]
|
||||
<br><br>
|
||||
|
||||
20
Task/Gamma-function/11l/gamma-function.11l
Normal file
20
Task/Gamma-function/11l/gamma-function.11l
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
V _a = [ 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
|
||||
]
|
||||
F gamma(x)
|
||||
V y = x - 1.0
|
||||
V sm = :_a.last
|
||||
L(n) (:_a.len-2 .. 0).step(-1)
|
||||
sm = sm * y + :_a[n]
|
||||
R 1.0 / sm
|
||||
|
||||
L(i) 1..10
|
||||
print(‘#.14’.format(gamma(i / 3.0)))
|
||||
159
Task/Gamma-function/360-Assembly/gamma-function.360
Normal file
159
Task/Gamma-function/360-Assembly/gamma-function.360
Normal file
|
|
@ -0,0 +1,159 @@
|
|||
GAMMAT CSECT
|
||||
USING GAMMAT,R13
|
||||
SAVEAR B STM-SAVEAR(R15)
|
||||
DC 17F'0'
|
||||
DC CL8'GAMMAT'
|
||||
STM STM R14,R12,12(R13)
|
||||
ST R13,4(R15)
|
||||
ST R15,8(R13)
|
||||
LR R13,R15
|
||||
* ---- CODE
|
||||
LE F4,=E'0'
|
||||
LH R2,NI
|
||||
LOOPI EQU *
|
||||
AE F4,=E'1' xi=xi+1
|
||||
LER F0,F4
|
||||
DE F0,=E'10' x=xi/10
|
||||
STE F0,X
|
||||
LE F6,X
|
||||
SE F6,=E'1' xx=x-1.0
|
||||
LH R4,NT
|
||||
BCTR R4,0
|
||||
SLA R4,2
|
||||
LE F0,T(R4)
|
||||
STE F0,SUM sum=t(nt)
|
||||
LH R3,NT
|
||||
BCTR R3,0
|
||||
SH R4,=H'4'
|
||||
LOOPJ CH R3,=H'1' for j=nt-1 downto 1
|
||||
BL ENDLOOPJ
|
||||
LE F0,SUM
|
||||
MER F0,F6 sum*xx
|
||||
LE F2,T(R4) t(j)
|
||||
AER F0,F2
|
||||
STE F0,SUM sum=sum*xx+t(j)
|
||||
BCTR R3,0
|
||||
SH R4,=H'4'
|
||||
B LOOPJ
|
||||
ENDLOOPJ EQU *
|
||||
LE F0,=E'1'
|
||||
DE F0,SUM
|
||||
STE F0,GAMMA gamma=1/sum
|
||||
LE F0,X
|
||||
BAL R14,CONVERT
|
||||
MVC BUF(8),CONVERTM
|
||||
LE F0,GAMMA
|
||||
BAL R14,CONVERT
|
||||
MVC BUF+9(13),CONVERTM
|
||||
WTO MF=(E,WTOMSG)
|
||||
BCT R2,LOOPI
|
||||
* ---- END CODE
|
||||
CNOP 0,4
|
||||
L R13,4(0,R13)
|
||||
LM R14,R12,12(R13)
|
||||
XR R15,R15
|
||||
BR R14
|
||||
* ---- DATA
|
||||
NI DC H'30'
|
||||
NT DC AL2((TEND-T)/4)
|
||||
T DC E'1.00000000000000000000'
|
||||
DC E'0.57721566490153286061'
|
||||
DC E'-0.65587807152025388108'
|
||||
DC E'-0.04200263503409523553'
|
||||
DC E'0.16653861138229148950'
|
||||
DC E'-0.04219773455554433675'
|
||||
DC E'-0.00962197152787697356'
|
||||
DC E'0.00721894324666309954'
|
||||
DC E'-0.00116516759185906511'
|
||||
DC E'-0.00021524167411495097'
|
||||
DC E'0.00012805028238811619'
|
||||
DC E'-0.00002013485478078824'
|
||||
DC E'-0.00000125049348214267'
|
||||
DC E'0.00000113302723198170'
|
||||
DC E'-0.00000020563384169776'
|
||||
DC E'0.00000000611609510448'
|
||||
DC E'0.00000000500200764447'
|
||||
DC E'-0.00000000118127457049'
|
||||
DC E'0.00000000010434267117'
|
||||
DC E'0.00000000000778226344'
|
||||
DC E'-0.00000000000369680562'
|
||||
DC E'0.00000000000051003703'
|
||||
DC E'-0.00000000000002058326'
|
||||
DC E'-0.00000000000000534812'
|
||||
DC E'0.00000000000000122678'
|
||||
DC E'-0.00000000000000011813'
|
||||
DC E'0.00000000000000000119'
|
||||
DC E'0.00000000000000000141'
|
||||
DC E'-0.00000000000000000023'
|
||||
DC E'0.00000000000000000002'
|
||||
TEND DS 0E
|
||||
X DS E
|
||||
SUM DS E
|
||||
GAMMA DS E
|
||||
WTOMSG DS 0F
|
||||
DC AL2(L'BUF),XL2'0000'
|
||||
BUF DC CL80' '
|
||||
* Subroutine Convertion Float->Display
|
||||
CONVERT CNOP 0,4
|
||||
ME F0,CONVERTC
|
||||
STE F0,CONVERTF
|
||||
MVI CONVERTS,X'00'
|
||||
L R9,CONVERTF
|
||||
CH R9,=H'0'
|
||||
BZ CONVERT7
|
||||
BNL CONVERT1 is negative?
|
||||
MVI CONVERTS,X'80'
|
||||
N R9,=X'7FFFFFFF' sign bit
|
||||
CONVERT1 LR R8,R9
|
||||
N R8,=X'00FFFFFF'
|
||||
BNZ CONVERT2
|
||||
SR R9,R9
|
||||
B CONVERT7
|
||||
CONVERT2 LR R8,R9
|
||||
N R8,=X'FF000000' characteristic
|
||||
SRL R8,24
|
||||
CH R8,=H'64'
|
||||
BH CONVERT3
|
||||
SR R9,R9
|
||||
B CONVERT7
|
||||
CONVERT3 CH R8,=H'72' 2**32
|
||||
BNH CONVERT4
|
||||
L R9,=X'7FFFFFFF' biggest
|
||||
B CONVERT6
|
||||
CONVERT4 SR R8,R8
|
||||
SLDL R8,8
|
||||
CH R8,=H'72'
|
||||
BL CONVERT5
|
||||
CH R9,=H'0'
|
||||
BP CONVERT5
|
||||
L R9,=X'7FFFFFFF'
|
||||
B CONVERT6
|
||||
CONVERT5 SH R8,=H'72'
|
||||
LCR R8,R8
|
||||
SLL R8,2
|
||||
SRL R9,0(R8)
|
||||
CONVERT6 SR R8,R8
|
||||
IC R8,CONVERTS
|
||||
CH R8,=H'0' sign bit set?
|
||||
BZ CONVERT7
|
||||
LCR R9,R9
|
||||
CONVERT7 ST R9,CONVERTB
|
||||
CVD R9,CONVERTP
|
||||
MVC CONVERTD,=X'402020202120202020202020'
|
||||
ED CONVERTD,CONVERTP+2
|
||||
MVC CONVERTM(6),CONVERTD
|
||||
MVI CONVERTM+6,C'.'
|
||||
MVC CONVERTM+7(6),CONVERTD+6
|
||||
BR R14
|
||||
*
|
||||
CONVERTC DC E'1E6' X'45F42400'
|
||||
CONVERTF DS F
|
||||
CONVERTB DS F
|
||||
CONVERTS DS X
|
||||
CONVERTM DS CL13
|
||||
CONVERTD DS CL12
|
||||
CONVERTP DS PL8
|
||||
*
|
||||
EQUREGS
|
||||
EQUREGS REGS=FPR
|
||||
END GAMMAT
|
||||
131
Task/Gamma-function/ALGOL-68/gamma-function.alg
Normal file
131
Task/Gamma-function/ALGOL-68/gamma-function.alg
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
# Coefficients used by the GNU Scientific Library #
|
||||
[]LONG REAL p = ( LONG 0.99999 99999 99809 93,
|
||||
LONG 676.52036 81218 851,
|
||||
-LONG 1259.13921 67224 028,
|
||||
LONG 771.32342 87776 5313,
|
||||
-LONG 176.61502 91621 4059,
|
||||
LONG 12.50734 32786 86905,
|
||||
-LONG 0.13857 10952 65720 12,
|
||||
LONG 9.98436 95780 19571 6e-6,
|
||||
LONG 1.50563 27351 49311 6e-7);
|
||||
|
||||
PROC lanczos gamma = (LONG REAL in z)LONG REAL: (
|
||||
# Reflection formula #
|
||||
LONG REAL z := in z;
|
||||
IF z < LONG 0.5 THEN
|
||||
long pi / (long sin(long pi*z)*lanczos gamma(1-z))
|
||||
ELSE
|
||||
z -:= 1;
|
||||
LONG REAL x := p[1];
|
||||
FOR i TO UPB p - 1 DO x +:= p[i+1]/(z+i) OD;
|
||||
LONG REAL t = z + UPB p - LONG 1.5;
|
||||
long sqrt(2*long pi) * t**(z+LONG 0.5) * long exp(-t) * x
|
||||
FI
|
||||
);
|
||||
|
||||
PROC taylor gamma = (LONG REAL x)LONG REAL:
|
||||
BEGIN # good for values between 0 and 1 #
|
||||
[]LONG REAL a =
|
||||
( LONG 1.00000 00000 00000 00000,
|
||||
LONG 0.57721 56649 01532 86061,
|
||||
-LONG 0.65587 80715 20253 88108,
|
||||
-LONG 0.04200 26350 34095 23553,
|
||||
LONG 0.16653 86113 82291 48950,
|
||||
-LONG 0.04219 77345 55544 33675,
|
||||
-LONG 0.00962 19715 27876 97356,
|
||||
LONG 0.00721 89432 46663 09954,
|
||||
-LONG 0.00116 51675 91859 06511,
|
||||
-LONG 0.00021 52416 74114 95097,
|
||||
LONG 0.00012 80502 82388 11619,
|
||||
-LONG 0.00002 01348 54780 78824,
|
||||
-LONG 0.00000 12504 93482 14267,
|
||||
LONG 0.00000 11330 27231 98170,
|
||||
-LONG 0.00000 02056 33841 69776,
|
||||
LONG 0.00000 00061 16095 10448,
|
||||
LONG 0.00000 00050 02007 64447,
|
||||
-LONG 0.00000 00011 81274 57049,
|
||||
LONG 0.00000 00001 04342 67117,
|
||||
LONG 0.00000 00000 07782 26344,
|
||||
-LONG 0.00000 00000 03696 80562,
|
||||
LONG 0.00000 00000 00510 03703,
|
||||
-LONG 0.00000 00000 00020 58326,
|
||||
-LONG 0.00000 00000 00005 34812,
|
||||
LONG 0.00000 00000 00001 22678,
|
||||
-LONG 0.00000 00000 00000 11813,
|
||||
LONG 0.00000 00000 00000 00119,
|
||||
LONG 0.00000 00000 00000 00141,
|
||||
-LONG 0.00000 00000 00000 00023,
|
||||
LONG 0.00000 00000 00000 00002
|
||||
);
|
||||
LONG REAL y = x - 1;
|
||||
LONG REAL sum := a [UPB a];
|
||||
FOR n FROM UPB a - 1 DOWNTO LWB a DO
|
||||
sum := sum * y + a [n]
|
||||
OD;
|
||||
1/sum
|
||||
END # taylor gamma #;
|
||||
|
||||
LONG REAL long e = long exp(1);
|
||||
|
||||
PROC sterling gamma = (LONG REAL n)LONG REAL:
|
||||
( # improves for values much greater then 1 #
|
||||
long sqrt(2*long pi/n)*(n/long e)**n
|
||||
);
|
||||
|
||||
PROC factorial = (LONG INT n)LONG REAL:
|
||||
(
|
||||
IF n=0 OR n=1 THEN 1
|
||||
ELIF n=2 THEN 2
|
||||
ELSE n*factorial(n-1) FI
|
||||
);
|
||||
|
||||
REF[]LONG REAL fm := NIL;
|
||||
|
||||
PROC sponge gamma = (LONG REAL x)LONG REAL:
|
||||
(
|
||||
INT a = 12; # alter to get required precision #
|
||||
REF []LONG REAL fm := NIL;
|
||||
LONG REAL res;
|
||||
|
||||
IF fm :=: REF[]LONG REAL(NIL) THEN
|
||||
fm := HEAP[0:a-1]LONG REAL;
|
||||
fm[0] := long sqrt(LONG 2*long pi);
|
||||
FOR k TO a-1 DO
|
||||
fm[k] := (((k-1) MOD 2=0) | 1 | -1) * long exp(a-k) *
|
||||
(a-k) **(k-LONG 0.5) / factorial(k-1)
|
||||
OD
|
||||
FI;
|
||||
res := fm[0];
|
||||
FOR k TO a-1 DO
|
||||
res +:= fm[k] / ( x + k )
|
||||
OD;
|
||||
res *:= long exp(-(x+a)) * (x+a)**(x + LONG 0.5);
|
||||
res/x
|
||||
);
|
||||
|
||||
FORMAT real fmt = $g(-real width, real width - 2)$;
|
||||
FORMAT long real fmt16 = $g(-17, 17 - 2)$; # accurate to about 16 decimal places #
|
||||
|
||||
[]STRING methods = ("Genie", "Lanczos", "Sponge", "Taylor","Stirling");
|
||||
|
||||
printf(($11xg12xg12xg13xg13xgl$,methods));
|
||||
|
||||
FORMAT sample fmt = $"gamma("g(-3,1)")="f(real fmt)n(UPB methods-1)(", "f(long real fmt16))l$;
|
||||
FORMAT sqr sample fmt = $"gamma("g(-3,1)")**2="f(real fmt)n(UPB methods-1)(", "f(long real fmt16))l$;
|
||||
FORMAT sample exp fmt = $"gamma("g(-3)")="g(-15,11,0)n(UPB methods-1)(","g(-18,14,0))l$;
|
||||
|
||||
PROC sample = (LONG REAL x)[]LONG REAL:
|
||||
(gamma(SHORTEN x), lanczos gamma(x), sponge gamma(x), taylor gamma(x), sterling gamma(x));
|
||||
|
||||
FOR i FROM 1 TO 20 DO
|
||||
LONG REAL x = i / LONG 10;
|
||||
printf((sample fmt, x, sample(x)));
|
||||
IF i = 5 THEN # insert special case of a half #
|
||||
printf((sqr sample fmt,
|
||||
x, gamma(SHORTEN x)**2, lanczos gamma(x)**2, sponge gamma(x)**2,
|
||||
taylor gamma(x)**2, sterling gamma(x)**2))
|
||||
FI
|
||||
OD;
|
||||
FOR x FROM 10 BY 10 TO 70 DO
|
||||
printf((sample exp fmt, x, sample(x)))
|
||||
OD
|
||||
26
Task/Gamma-function/ANSI-BASIC/gamma-function.basic
Normal file
26
Task/Gamma-function/ANSI-BASIC/gamma-function.basic
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
100 DECLARE EXTERNAL FUNCTION FNlngamma
|
||||
110
|
||||
120 DEF FNgamma(z) = EXP(FNlngamma(z))
|
||||
130
|
||||
140 FOR x = 0.1 TO 2.05 STEP 0.1
|
||||
150 PRINT USING$("#.#",x), USING$("##.############", FNgamma(x))
|
||||
160 NEXT x
|
||||
170 END
|
||||
180
|
||||
190 EXTERNAL FUNCTION FNlngamma(z)
|
||||
200 DIM lz(0 TO 6)
|
||||
210 RESTORE
|
||||
220 MAT READ lz
|
||||
230 DATA 1.000000000190015, 76.18009172947146, -86.50532032941677, 24.01409824083091, -1.231739572450155, 0.0012086509738662, -0.000005395239385
|
||||
240 IF z < 0.5 THEN
|
||||
250 LET FNlngamma = LOG(PI / SIN(PI * z)) - FNlngamma(1.0 - z)
|
||||
260 EXIT FUNCTION
|
||||
270 END IF
|
||||
280 LET z = z - 1.0
|
||||
290 LET b = z + 5.5
|
||||
300 LET a = lz(0)
|
||||
310 FOR i = 1 TO 6
|
||||
320 LET a = a + lz(i) / (z + i)
|
||||
330 NEXT i
|
||||
340 LET FNlngamma = (LOG(SQR(2*PI)) + LOG(a) - b) + LOG(b) * (z+0.5)
|
||||
350 END FUNCTION
|
||||
18
Task/Gamma-function/AWK/gamma-function.awk
Normal file
18
Task/Gamma-function/AWK/gamma-function.awk
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# syntax: GAWK -f GAMMA_FUNCTION.AWK
|
||||
BEGIN {
|
||||
e = (1+1/100000)^100000
|
||||
pi = atan2(0,-1)
|
||||
|
||||
print("X Stirling")
|
||||
for (i=1; i<=20; i++) {
|
||||
d = i / 10
|
||||
printf("%4.2f %9.5f\n",d,gamma_stirling(d))
|
||||
}
|
||||
exit(0)
|
||||
}
|
||||
function gamma_stirling(x) {
|
||||
return sqrt(2*pi/x) * pow(x/e,x)
|
||||
}
|
||||
function pow(a,b) {
|
||||
return exp(b*log(a))
|
||||
}
|
||||
41
Task/Gamma-function/Ada/gamma-function-1.ada
Normal file
41
Task/Gamma-function/Ada/gamma-function-1.ada
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
function Gamma (X : Long_Float) return Long_Float is
|
||||
A : constant array (0..29) of Long_Float :=
|
||||
( 1.00000_00000_00000_00000,
|
||||
0.57721_56649_01532_86061,
|
||||
-0.65587_80715_20253_88108,
|
||||
-0.04200_26350_34095_23553,
|
||||
0.16653_86113_82291_48950,
|
||||
-0.04219_77345_55544_33675,
|
||||
-0.00962_19715_27876_97356,
|
||||
0.00721_89432_46663_09954,
|
||||
-0.00116_51675_91859_06511,
|
||||
-0.00021_52416_74114_95097,
|
||||
0.00012_80502_82388_11619,
|
||||
-0.00002_01348_54780_78824,
|
||||
-0.00000_12504_93482_14267,
|
||||
0.00000_11330_27231_98170,
|
||||
-0.00000_02056_33841_69776,
|
||||
0.00000_00061_16095_10448,
|
||||
0.00000_00050_02007_64447,
|
||||
-0.00000_00011_81274_57049,
|
||||
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
|
||||
);
|
||||
Y : constant Long_Float := X - 1.0;
|
||||
Sum : Long_Float := A (A'Last);
|
||||
begin
|
||||
for N in reverse A'First..A'Last - 1 loop
|
||||
Sum := Sum * Y + A (N);
|
||||
end loop;
|
||||
return 1.0 / Sum;
|
||||
end Gamma;
|
||||
9
Task/Gamma-function/Ada/gamma-function-2.ada
Normal file
9
Task/Gamma-function/Ada/gamma-function-2.ada
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
with Ada.Text_IO; use Ada.Text_IO;
|
||||
with Gamma;
|
||||
|
||||
procedure Test_Gamma is
|
||||
begin
|
||||
for I in 1..10 loop
|
||||
Put_Line (Long_Float'Image (Gamma (Long_Float (I) / 3.0)));
|
||||
end loop;
|
||||
end Test_Gamma;
|
||||
32
Task/Gamma-function/Arturo/gamma-function.arturo
Normal file
32
Task/Gamma-function/Arturo/gamma-function.arturo
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
A: @[
|
||||
1.00000000000000000000 0.57721566490153286061 neg 0.65587807152025388108
|
||||
neg 0.04200263503409523553 0.16653861138229148950 neg 0.04219773455554433675
|
||||
neg 0.00962197152787697356 0.00721894324666309954 neg 0.00116516759185906511
|
||||
neg 0.00021524167411495097 0.00012805028238811619 neg 0.00002013485478078824
|
||||
neg 0.00000125049348214267 0.00000113302723198170 neg 0.00000020563384169776
|
||||
0.00000000611609510448 0.00000000500200764447 neg 0.00000000118127457049
|
||||
0.00000000010434267117 0.00000000000778226344 neg 0.00000000000369680562
|
||||
0.00000000000051003703 neg 0.00000000000002058326 neg 0.00000000000000534812
|
||||
0.00000000000000122678 neg 0.00000000000000011813 0.00000000000000000119
|
||||
0.00000000000000000141 neg 0.00000000000000000023 0.00000000000000000002
|
||||
]
|
||||
|
||||
ourGamma: function [x][
|
||||
y: x - 1
|
||||
result: last A
|
||||
loop ((size A)-1)..0 'n ->
|
||||
result: (result*y) + get A n
|
||||
result: 1 // result
|
||||
return result
|
||||
]
|
||||
|
||||
loop 1..10 'z [
|
||||
v1: ourGamma z // 3
|
||||
v2: gamma z // 3
|
||||
print [
|
||||
pad (to :string z)++" =>" 10
|
||||
pad (to :string v1)++" ~" 30
|
||||
pad (to :string v2)++" :" 30
|
||||
pad (to :string v1-v2) 30
|
||||
]
|
||||
]
|
||||
82
Task/Gamma-function/AutoHotkey/gamma-function.ahk
Normal file
82
Task/Gamma-function/AutoHotkey/gamma-function.ahk
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
/*
|
||||
Here is the upper incomplete Gamma function. Omitting or setting
|
||||
the second parameter to 0 we get the (complete) Gamma function.
|
||||
The code is based on: "Computation of Special Functions" Zhang and Jin,
|
||||
John Wiley and Sons, 1996
|
||||
*/
|
||||
|
||||
SetFormat FloatFast, 0.9e
|
||||
|
||||
Loop 10
|
||||
MsgBox % GAMMA(A_Index/3) "`n" GAMMA(A_Index*10)
|
||||
|
||||
GAMMA(a,x=0) { ; upper incomplete gamma: Integral(t**(a-1)*e**-t, t = x..inf)
|
||||
If (a > 171 || x < 0)
|
||||
Return 2.e308 ; overflow
|
||||
|
||||
xam := x > 0 ? -x+a*ln(x) : 0
|
||||
If (xam > 700)
|
||||
Return 2.e308 ; overflow
|
||||
|
||||
If (x > 1+a) { ; no need for gamma(a)
|
||||
t0 := 0, k := 60
|
||||
Loop 60
|
||||
t0 := (k-a)/(1+k/(x+t0)), --k
|
||||
Return exp(xam) / (x+t0)
|
||||
}
|
||||
|
||||
r := 1, ga := 1.0 ; compute ga = gamma(a) ...
|
||||
If (a = round(a)) ; if integer: factorial
|
||||
If (a > 0)
|
||||
Loop % a-1
|
||||
ga *= A_Index
|
||||
Else ; negative integer
|
||||
ga := 1.7976931348623157e+308 ; Dmax
|
||||
Else { ; not integer
|
||||
If (abs(a) > 1) {
|
||||
z := abs(a)
|
||||
m := floor(z)
|
||||
Loop %m%
|
||||
r *= (z-A_Index)
|
||||
z -= m
|
||||
}
|
||||
Else
|
||||
z := a
|
||||
|
||||
gr := ((((((((((((((((((((((( 0.14e-14
|
||||
*z - 0.54e-14) *z - 0.206e-13) *z + 0.51e-12)
|
||||
*z - 0.36968e-11) *z + 0.77823e-11) *z + 0.1043427e-9)
|
||||
*z - 0.11812746e-8) *z + 0.50020075e-8) *z + 0.6116095e-8)
|
||||
*z - 0.2056338417e-6) *z + 0.1133027232e-5) *z - 0.12504934821e-5)
|
||||
*z - 0.201348547807e-4) *z + 0.1280502823882e-3) *z - 0.2152416741149e-3)
|
||||
*z - 0.11651675918591e-2) *z + 0.7218943246663e-2) *z - 0.9621971527877e-2)
|
||||
*z - 0.421977345555443e-1) *z + 0.1665386113822915) *z - 0.420026350340952e-1)
|
||||
*z - 0.6558780715202538) *z + 0.5772156649015329) *z + 1
|
||||
|
||||
ga := 1.0/(gr*z) * r
|
||||
If (a < -1)
|
||||
ga := -3.1415926535897931/(a*ga*sin(3.1415926535897931*a))
|
||||
}
|
||||
|
||||
If (x = 0) ; complete gamma requested
|
||||
Return ga
|
||||
|
||||
s := 1/a ; here x <= 1+a
|
||||
r := s
|
||||
Loop 60 {
|
||||
r *= x/(a+A_Index)
|
||||
s += r
|
||||
If (abs(r/s) < 1.e-15)
|
||||
break
|
||||
}
|
||||
Return ga - exp(xam)*s
|
||||
}
|
||||
|
||||
/*
|
||||
The 10 results shown:
|
||||
2.678938535e+000 1.354117939e+000 1.0 8.929795115e-001 9.027452930e-001
|
||||
3.628800000e+005 1.216451004e+017 8.841761994e+030 2.039788208e+046 6.082818640e+062
|
||||
|
||||
1.000000000e+000 1.190639348e+000 1.504575489e+000 2.000000000e+000 2.778158479e+000
|
||||
1.386831185e+080 1.711224524e+098 8.946182131e+116 1.650795516e+136 9.332621544e+155
|
||||
*/
|
||||
29
Task/Gamma-function/BASIC256/gamma-function.basic
Normal file
29
Task/Gamma-function/BASIC256/gamma-function.basic
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
print " x Stirling Lanczos"
|
||||
print
|
||||
for i = 1 to 20
|
||||
d = i / 10.0
|
||||
print d;
|
||||
print chr(9); ljust(string(gammaStirling(d)), 13, "0");
|
||||
print chr(9); ljust(string(gammaLanczos(d)), 13, "0")
|
||||
next i
|
||||
end
|
||||
|
||||
function gammaStirling (x)
|
||||
e = exp(1) # e is not predefined in BASIC256
|
||||
return sqr(2.0 * pi / x) * ((x / e) ^ x)
|
||||
end function
|
||||
|
||||
function gammaLanczos (x)
|
||||
dim p = {0.99999999999980993, 676.5203681218851, -1259.1392167224028, 771.32342877765313, -176.61502916214059, 12.507343278686905, -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7}
|
||||
|
||||
g = 7
|
||||
if x < 0.5 then return pi / (sin(pi * x) * gammaLanczos(1-x))
|
||||
x -= 1
|
||||
a = p[0]
|
||||
t = x + g + 0.5
|
||||
|
||||
for i = 1 to 8
|
||||
a += p[i] / (x + i)
|
||||
next i
|
||||
return sqr(2.0 * pi) * (t ^ (x + 0.5)) * exp(-t) * a
|
||||
end function
|
||||
23
Task/Gamma-function/BBC-BASIC/gamma-function.basic
Normal file
23
Task/Gamma-function/BBC-BASIC/gamma-function.basic
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
*FLOAT64
|
||||
INSTALL @lib$+"FNUSING"
|
||||
|
||||
FOR x = 0.1 TO 2.05 STEP 0.1
|
||||
PRINT FNusing("#.#",x), FNusing("##.############", FNgamma(x))
|
||||
NEXT
|
||||
END
|
||||
|
||||
DEF FNgamma(z) = EXP(FNlngamma(z))
|
||||
|
||||
DEF FNlngamma(z)
|
||||
LOCAL a, b, i%, lz()
|
||||
DIM lz(6)
|
||||
lz() = 1.000000000190015, 76.18009172947146, -86.50532032941677, \
|
||||
\ 24.01409824083091, -1.231739572450155, 0.0012086509738662, -0.000005395239385
|
||||
IF z < 0.5 THEN = LN(PI / SIN(PI * z)) - FNlngamma(1.0 - z)
|
||||
z -= 1.0
|
||||
b = z + 5.5
|
||||
a = lz(0)
|
||||
FOR i% = 1 TO 6
|
||||
a += lz(i%) / (z + i%)
|
||||
NEXT
|
||||
= (LNSQR(2*PI) + LN(a) - b) + LN(b) * (z+0.5)
|
||||
58
Task/Gamma-function/C++/gamma-function.cpp
Normal file
58
Task/Gamma-function/C++/gamma-function.cpp
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
#include <math.h>
|
||||
#include <numbers>
|
||||
#include <stdio.h>
|
||||
#include <vector>
|
||||
|
||||
// Calculate the coefficients used by Spouge's approximation (based on the C
|
||||
// implemetation)
|
||||
std::vector<double> CalculateCoefficients(int numCoeff)
|
||||
{
|
||||
std::vector<double> c(numCoeff);
|
||||
double k1_factrl = 1.0;
|
||||
c[0] = sqrt(2.0 * std::numbers::pi);
|
||||
for(size_t k=1; k < numCoeff; k++)
|
||||
{
|
||||
c[k] = exp(numCoeff-k) * pow(numCoeff-k, k-0.5) / k1_factrl;
|
||||
k1_factrl *= -(double)k;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
// The Spouge approximation
|
||||
double Gamma(const std::vector<double>& coeffs, double x)
|
||||
{
|
||||
const size_t numCoeff = coeffs.size();
|
||||
double accm = coeffs[0];
|
||||
for(size_t k=1; k < numCoeff; k++)
|
||||
{
|
||||
accm += coeffs[k] / ( x + k );
|
||||
}
|
||||
accm *= exp(-(x+numCoeff)) * pow(x+numCoeff, x+0.5);
|
||||
return accm/x;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
// estimate the gamma function with 1, 4, and 10 coefficients
|
||||
const auto coeff1 = CalculateCoefficients(1);
|
||||
const auto coeff4 = CalculateCoefficients(4);
|
||||
const auto coeff10 = CalculateCoefficients(10);
|
||||
|
||||
const auto inputs = std::vector<double>{
|
||||
0.001, 0.01, 0.1, 0.5, 1.0,
|
||||
1.461632145, // minimum of the gamma function
|
||||
2, 2.5, 3, 4, 5, 6, 7, 8, 9, 10, 50, 100,
|
||||
150 // causes overflow for this implemetation
|
||||
};
|
||||
|
||||
printf("%16s%16s%16s%16s%16s\n", "gamma( x ) =", "Spouge 1", "Spouge 4", "Spouge 10", "built-in");
|
||||
for(auto x : inputs)
|
||||
{
|
||||
printf("gamma(%7.3f) = %16.10g %16.10g %16.10g %16.10g\n",
|
||||
x,
|
||||
Gamma(coeff1, x),
|
||||
Gamma(coeff4, x),
|
||||
Gamma(coeff10, x),
|
||||
std::tgamma(x)); // built-in gamma function
|
||||
}
|
||||
}
|
||||
27
Task/Gamma-function/C-sharp/gamma-function.cs
Normal file
27
Task/Gamma-function/C-sharp/gamma-function.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
using System;
|
||||
using System.Numerics;
|
||||
|
||||
static int g = 7;
|
||||
static double[] p = {0.99999999999980993, 676.5203681218851, -1259.1392167224028,
|
||||
771.32342877765313, -176.61502916214059, 12.507343278686905,
|
||||
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7};
|
||||
|
||||
Complex Gamma(Complex z)
|
||||
{
|
||||
// Reflection formula
|
||||
if (z.Real < 0.5)
|
||||
{
|
||||
return Math.PI / (Complex.Sin( Math.PI * z) * Gamma(1 - z));
|
||||
}
|
||||
else
|
||||
{
|
||||
z -= 1;
|
||||
Complex x = p[0];
|
||||
for (var i = 1; i < g + 2; i++)
|
||||
{
|
||||
x += p[i]/(z+i);
|
||||
}
|
||||
Complex t = z + g + 0.5;
|
||||
return Complex.Sqrt(2 * Math.PI) * (Complex.Pow(t, z + 0.5)) * Complex.Exp(-t) * x;
|
||||
}
|
||||
}
|
||||
52
Task/Gamma-function/C/gamma-function.c
Normal file
52
Task/Gamma-function/C/gamma-function.c
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <math.h>
|
||||
#include <gsl/gsl_sf_gamma.h>
|
||||
#ifndef M_PI
|
||||
#define M_PI 3.14159265358979323846
|
||||
#endif
|
||||
|
||||
/* very simple approximation */
|
||||
double st_gamma(double x)
|
||||
{
|
||||
return sqrt(2.0*M_PI/x)*pow(x/M_E, x);
|
||||
}
|
||||
|
||||
#define A 12
|
||||
double sp_gamma(double z)
|
||||
{
|
||||
const int a = A;
|
||||
static double c_space[A];
|
||||
static double *c = NULL;
|
||||
int k;
|
||||
double accm;
|
||||
|
||||
if ( c == NULL ) {
|
||||
double k1_factrl = 1.0; /* (k - 1)!*(-1)^k with 0!==1*/
|
||||
c = c_space;
|
||||
c[0] = sqrt(2.0*M_PI);
|
||||
for(k=1; k < a; k++) {
|
||||
c[k] = exp(a-k) * pow(a-k, k-0.5) / k1_factrl;
|
||||
k1_factrl *= -k;
|
||||
}
|
||||
}
|
||||
accm = c[0];
|
||||
for(k=1; k < a; k++) {
|
||||
accm += c[k] / ( z + k );
|
||||
}
|
||||
accm *= exp(-(z+a)) * pow(z+a, z+0.5); /* Gamma(z+1) */
|
||||
return accm/z;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
double x;
|
||||
|
||||
|
||||
printf("%15s%15s%15s%15s\n", "Stirling", "Spouge", "GSL", "libm");
|
||||
for(x=1.0; x <= 10.0; x+=1.0) {
|
||||
printf("%15.8lf%15.8lf%15.8lf%15.8lf\n", st_gamma(x/3.0), sp_gamma(x/3.0),
|
||||
gsl_sf_gamma(x/3.0), tgamma(x/3.0));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
15
Task/Gamma-function/Clojure/gamma-function-1.clj
Normal file
15
Task/Gamma-function/Clojure/gamma-function-1.clj
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
(defn gamma
|
||||
"Returns Gamma(z + 1 = number) using Lanczos approximation."
|
||||
[number]
|
||||
(if (< number 0.5)
|
||||
(/ Math/PI (* (Math/sin (* Math/PI number))
|
||||
(gamma (- 1 number))))
|
||||
(let [n (dec number)
|
||||
c [0.99999999999980993 676.5203681218851 -1259.1392167224028
|
||||
771.32342877765313 -176.61502916214059 12.507343278686905
|
||||
-0.13857109526572012 9.9843695780195716e-6 1.5056327351493116e-7]]
|
||||
(* (Math/sqrt (* 2 Math/PI))
|
||||
(Math/pow (+ n 7 0.5) (+ n 0.5))
|
||||
(Math/exp (- (+ n 7 0.5)))
|
||||
(+ (first c)
|
||||
(apply + (map-indexed #(/ %2 (+ n %1 1)) (next c))))))))
|
||||
1
Task/Gamma-function/Clojure/gamma-function-2.clj
Normal file
1
Task/Gamma-function/Clojure/gamma-function-2.clj
Normal file
|
|
@ -0,0 +1 @@
|
|||
(map #(printf "%.1f %.4f\n" % (gamma %)) (map #(float (/ % 10)) (range 1 31)))
|
||||
26
Task/Gamma-function/Common-Lisp/gamma-function.lisp
Normal file
26
Task/Gamma-function/Common-Lisp/gamma-function.lisp
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
; Taylor series coefficients
|
||||
(defconstant tcoeff
|
||||
'( 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))
|
||||
|
||||
; number of coefficients
|
||||
(defconstant numcoeff (length tcoeff))
|
||||
|
||||
(defun gamma (x)
|
||||
(let ((y (- x 1.0))
|
||||
(sum (nth (- numcoeff 1) tcoeff)))
|
||||
(loop for i from (- numcoeff 2) downto 0 do
|
||||
(setf sum (+ (* sum y) (nth i tcoeff))))
|
||||
(/ 1.0 sum)))
|
||||
|
||||
(loop for i from 1 to 10
|
||||
do (
|
||||
format t "~12,10f~%" (gamma (/ i 3.0))))
|
||||
42
Task/Gamma-function/Crystal/gamma-function.crystal
Normal file
42
Task/Gamma-function/Crystal/gamma-function.crystal
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
# Taylor Series
|
||||
def a
|
||||
[ 1.00000_00000_00000_00000, 0.57721_56649_01532_86061, -0.65587_80715_20253_88108,
|
||||
-0.04200_26350_34095_23553, 0.16653_86113_82291_48950, -0.04219_77345_55544_33675,
|
||||
-0.00962_19715_27876_97356, 0.00721_89432_46663_09954, -0.00116_51675_91859_06511,
|
||||
-0.00021_52416_74114_95097, 0.00012_80502_82388_11619, -0.00002_01348_54780_78824,
|
||||
-0.00000_12504_93482_14267, 0.00000_11330_27231_98170, -0.00000_02056_33841_69776,
|
||||
0.00000_00061_16095_10448, 0.00000_00050_02007_64447, -0.00000_00011_81274_57049,
|
||||
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 ]
|
||||
end
|
||||
|
||||
def taylor_gamma(x)
|
||||
y = x.to_f - 1
|
||||
1.0 / a.reverse.reduce(0) { |sum, an| sum * y + an }
|
||||
end
|
||||
|
||||
# Lanczos Method
|
||||
def p
|
||||
[ 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 ]
|
||||
end
|
||||
|
||||
def lanczos_gamma(z)
|
||||
# Reflection formula
|
||||
z = z.to_f
|
||||
if z < 0.5
|
||||
Math::PI / (Math.sin(Math::PI * z) * lanczos_gamma(1 - z))
|
||||
else
|
||||
z -= 1
|
||||
x = p[0]
|
||||
(1..p.size - 1).each { |i| x += p[i] / (z + i) }
|
||||
t = z + p.size - 1.5
|
||||
Math.sqrt(2 * Math::PI) * t**(z + 0.5) * Math.exp(-t) * x
|
||||
end
|
||||
end
|
||||
|
||||
puts " Taylor Series Lanczos Method Builtin Function"
|
||||
(1..27).each { |i| n = i/3.0; puts "gamma(%.2f) = %.14e %.14e %.14e" % [n, taylor_gamma(n), lanczos_gamma(n), Math.gamma(n)] }
|
||||
62
Task/Gamma-function/D/gamma-function.d
Normal file
62
Task/Gamma-function/D/gamma-function.d
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
import std.stdio, std.math, std.mathspecial;
|
||||
|
||||
real taylorGamma(in real x) pure nothrow @safe @nogc {
|
||||
static immutable real[30] table = [
|
||||
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];
|
||||
|
||||
immutable real y = x - 1.0L;
|
||||
real sm = table[$ - 1];
|
||||
foreach_reverse (immutable an; table[0 .. $ - 1])
|
||||
sm = sm * y + an;
|
||||
return 1.0L / sm;
|
||||
}
|
||||
|
||||
real lanczosGamma(real z) pure nothrow @safe @nogc {
|
||||
// Coefficients used by the GNU Scientific Library.
|
||||
// http://en.wikipedia.org/wiki/Lanczos_approximation
|
||||
enum g = 7;
|
||||
static immutable real[9] table =
|
||||
[ 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.5L) {
|
||||
return PI / (sin(PI * z) * lanczosGamma(1 - z));
|
||||
} else {
|
||||
z -= 1;
|
||||
real x = table[0];
|
||||
foreach (immutable i; 1 .. g + 2)
|
||||
x += table[i] / (z + i);
|
||||
immutable real t = z + g + 0.5L;
|
||||
return sqrt(2 * PI) * t ^^ (z + 0.5L) * exp(-t) * x;
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
foreach (immutable i; 1 .. 11) {
|
||||
immutable real x = i / 3.0L;
|
||||
writefln("%f: %20.19e %20.19e %20.19e", x,
|
||||
x.taylorGamma, x.lanczosGamma, x.gamma);
|
||||
}
|
||||
}
|
||||
26
Task/Gamma-function/Delphi/gamma-function.delphi
Normal file
26
Task/Gamma-function/Delphi/gamma-function.delphi
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
program Gamma_function;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
System.SysUtils,
|
||||
System.Math;
|
||||
|
||||
function lanczos7(z: double): Double;
|
||||
begin
|
||||
var t := z + 6.5;
|
||||
var x := 0.99999999999980993 + 676.5203681218851 / z - 1259.1392167224028 / (z
|
||||
+ 1) + 771.32342877765313 / (z + 2) - 176.61502916214059 / (z + 3) +
|
||||
12.507343278686905 / (z + 4) - 0.13857109526572012 / (z + 5) +
|
||||
9.9843695780195716e-6 / (z + 6) + 1.5056327351493116e-7 / (z + 7);
|
||||
|
||||
Result := Sqrt(2) * Sqrt(pi) * Power(t, z - 0.5) * exp(-t) * x;
|
||||
end;
|
||||
|
||||
begin
|
||||
var xs: TArray<double> := [-0.5, 0.1, 0.5, 1, 1.5, 2, 3, 10, 140, 170];
|
||||
writeln(' x Lanczos7');
|
||||
for var x in xs do
|
||||
writeln(format('%5.1f %24.16g', [x, lanczos7(x)]));
|
||||
readln;
|
||||
end.
|
||||
21
Task/Gamma-function/Elixir/gamma-function.elixir
Normal file
21
Task/Gamma-function/Elixir/gamma-function.elixir
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
defmodule Gamma do
|
||||
@a [ 1.00000_00000_00000_00000, 0.57721_56649_01532_86061, -0.65587_80715_20253_88108,
|
||||
-0.04200_26350_34095_23553, 0.16653_86113_82291_48950, -0.04219_77345_55544_33675,
|
||||
-0.00962_19715_27876_97356, 0.00721_89432_46663_09954, -0.00116_51675_91859_06511,
|
||||
-0.00021_52416_74114_95097, 0.00012_80502_82388_11619, -0.00002_01348_54780_78824,
|
||||
-0.00000_12504_93482_14267, 0.00000_11330_27231_98170, -0.00000_02056_33841_69776,
|
||||
0.00000_00061_16095_10448, 0.00000_00050_02007_64447, -0.00000_00011_81274_57049,
|
||||
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 ]
|
||||
|> Enum.reverse
|
||||
def taylor(x) do
|
||||
y = x - 1
|
||||
1 / Enum.reduce(@a, 0, fn a,sum -> sum * y + a end)
|
||||
end
|
||||
end
|
||||
|
||||
Enum.each(Enum.map(1..10, &(&1/3)), fn x ->
|
||||
:io.format "~f ~18.15f~n", [x, Gamma.taylor(x)]
|
||||
end)
|
||||
14
Task/Gamma-function/F-Sharp/gamma-function-1.fs
Normal file
14
Task/Gamma-function/F-Sharp/gamma-function-1.fs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
open System
|
||||
|
||||
let gamma z =
|
||||
let lanczosCoefficients = [76.18009172947146;-86.50532032941677;24.01409824083091;-1.231739572450155;0.1208650973866179e-2;-0.5395239384953e-5]
|
||||
let rec sumCoefficients acc i coefficients =
|
||||
match coefficients with
|
||||
| [] -> acc
|
||||
| h::t -> sumCoefficients (acc + (h/i)) (i+1.0) t
|
||||
let gamma = 5.0
|
||||
let x = z - 1.0
|
||||
Math.Pow(x + gamma + 0.5, x + 0.5) * Math.Exp( -(x + gamma + 0.5) ) * Math.Sqrt( 2.0 * Math.PI ) * sumCoefficients 1.000000000190015 (x + 1.0) lanczosCoefficients
|
||||
|
||||
seq { for i in 1 .. 20 do yield ((double)i/10.0) } |> Seq.iter ( fun v -> System.Console.WriteLine("{0} : {1}", v, gamma v ) )
|
||||
seq { for i in 1 .. 10 do yield ((double)i*10.0) } |> Seq.iter ( fun v -> System.Console.WriteLine("{0} : {1}", v, gamma v ) )
|
||||
21
Task/Gamma-function/F-Sharp/gamma-function-2.fs
Normal file
21
Task/Gamma-function/F-Sharp/gamma-function-2.fs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
open System.Numerics
|
||||
open System
|
||||
|
||||
let rec gamma (z: Complex) =
|
||||
let mutable z = z
|
||||
let lanczosCoefficients = [| 676.520368121885; -1259.1392167224; 771.323428777653; -176.615029162141; 12.5073432786869; -0.13857109526572; 9.98436957801957E-06; 1.50563273514931E-07 |]
|
||||
|
||||
if z.Real < 0.5 then
|
||||
Math.PI / (sin (Math.PI * z) * gamma (1.0 - z))
|
||||
else
|
||||
let mutable x = Complex.One
|
||||
z <- z - 1.0
|
||||
|
||||
for i = 0 to lanczosCoefficients.Length - 1 do
|
||||
x <- x + lanczosCoefficients.[i] / (z + Complex(i, 0) + 1.0)
|
||||
|
||||
let t = z + float lanczosCoefficients.Length - 0.5
|
||||
sqrt (2.0 * Math.PI) * (t ** (z + 0.5)) * exp (-t) * x
|
||||
|
||||
Seq.iter (fun i -> printfn "Gamma(%f) = %A" i (gamma (Complex(i, 0)))) [ 0 .. 100 ]
|
||||
Seq.iter2 (fun i j -> printfn "Gamma(%f + i%f) = %A" i j (gamma (Complex(i, j)))) [ 0 .. 100 ] [ 0 .. 100 ]
|
||||
5
Task/Gamma-function/Factor/gamma-function.factor
Normal file
5
Task/Gamma-function/Factor/gamma-function.factor
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
! built in
|
||||
USING: picomath prettyprint ;
|
||||
0.1 gamma . ! 9.513507698668723
|
||||
2.0 gamma . ! 1.0
|
||||
10. gamma . ! 362880.0
|
||||
15
Task/Gamma-function/Forth/gamma-function-1.fth
Normal file
15
Task/Gamma-function/Forth/gamma-function-1.fth
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
8 constant (gamma-shift)
|
||||
|
||||
: (mortici) ( f1 -- f2)
|
||||
-1 s>f f+ 1 s>f
|
||||
fover 271828183e-8 f* 12 s>f f* f/
|
||||
fover 271828183e-8 f/ f+
|
||||
fover f** fswap
|
||||
628318530e-8 f* fsqrt f* \ 2*pi
|
||||
;
|
||||
|
||||
: gamma ( f1 -- f2)
|
||||
fdup f0< >r fdup f0= r> or abort" Gamma less or equal to zero"
|
||||
fdup (gamma-shift) s>f f+ (mortici) fswap
|
||||
1 s>f (gamma-shift) 0 do fover i s>f f+ f* loop fswap fdrop f/
|
||||
;
|
||||
25
Task/Gamma-function/Forth/gamma-function-2.fth
Normal file
25
Task/Gamma-function/Forth/gamma-function-2.fth
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
2 constant (gamma-shift) \ don't change this
|
||||
\ an approximation of the d(x) function
|
||||
: ~d(x) ( f1 -- f2)
|
||||
fdup 10 s>f f< \ use first symmetrical sigmoidal
|
||||
if \ for range 1-10
|
||||
-2705443e-8 fswap 2280802e-6 f/ 1428045e-6 f** 1 s>f f+ f/ 3187831e-8 f+
|
||||
else \ use second symmetrical sigmoidal
|
||||
-29372563e-9 fswap 1841693e-6 f/ 1052779e-6 f** 1 s>f f+ f/ 3330828e-8 f+
|
||||
then 333333333e-10 fover f< if fdrop 1 s>f 30 s>f f/ then
|
||||
; \ perform some sane clipping to infinity
|
||||
|
||||
: (ramanujan) ( f1 -- f2)
|
||||
fdup fdup f* 4 s>f f* ( n 4n2)
|
||||
fover fover f* fdup f+ f+ fover f+ ( n 8n3+4n2+n)
|
||||
fover ~d(x) f+ ( n 8n3+4n2+n+d[x])
|
||||
1 s>f 6 s>f f/ f** ( n 8n3+4n2+n+d[x]^1/6)
|
||||
fswap fdup 2.7182818284590452353e f/ ( 8n3+4n2+n+d[x]^1/6 n n/e)
|
||||
fswap f** f* pi fsqrt f* ( f)
|
||||
;
|
||||
|
||||
: gamma ( f1 -- f2)
|
||||
fdup f0< >r fdup f0= r> or abort" Gamma less or equal to zero"
|
||||
fdup (gamma-shift) 1- s>f f+ (ramanujan) fswap
|
||||
1 s>f (gamma-shift) 0 do fover i s>f f+ f* loop fswap fdrop f/
|
||||
;
|
||||
98
Task/Gamma-function/Fortran/gamma-function.f
Normal file
98
Task/Gamma-function/Fortran/gamma-function.f
Normal file
|
|
@ -0,0 +1,98 @@
|
|||
program ComputeGammaInt
|
||||
|
||||
implicit none
|
||||
|
||||
integer :: i
|
||||
|
||||
write(*, "(3A15)") "Simpson", "Lanczos", "Builtin"
|
||||
do i=1, 10
|
||||
write(*, "(3F15.8)") my_gamma(i/3.0), lacz_gamma(i/3.0), gamma(i/3.0)
|
||||
end do
|
||||
|
||||
contains
|
||||
|
||||
pure function intfuncgamma(x, y) result(z)
|
||||
real :: z
|
||||
real, intent(in) :: x, y
|
||||
|
||||
z = x**(y-1.0) * exp(-x)
|
||||
end function intfuncgamma
|
||||
|
||||
|
||||
function my_gamma(a) result(g)
|
||||
real :: g
|
||||
real, intent(in) :: a
|
||||
|
||||
real, parameter :: small = 1.0e-4
|
||||
integer, parameter :: points = 100000
|
||||
|
||||
real :: infty, dx, p, sp(2, points), x
|
||||
integer :: i
|
||||
logical :: correction
|
||||
|
||||
x = a
|
||||
|
||||
correction = .false.
|
||||
! value with x<1 gives \infty, so we use
|
||||
! \Gamma(x+1) = x\Gamma(x)
|
||||
! to avoid the problem
|
||||
if ( x < 1.0 ) then
|
||||
correction = .true.
|
||||
x = x + 1
|
||||
end if
|
||||
|
||||
! find a "reasonable" infinity...
|
||||
! we compute this integral indeed
|
||||
! \int_0^M dt t^{x-1} e^{-t}
|
||||
! where M is such that M^{x-1} e^{-M} ≤ \epsilon
|
||||
infty = 1.0e4
|
||||
do while ( intfuncgamma(infty, x) > small )
|
||||
infty = infty * 10.0
|
||||
end do
|
||||
|
||||
! using simpson
|
||||
dx = infty/real(points)
|
||||
sp = 0.0
|
||||
forall(i=1:points/2-1) sp(1, 2*i) = intfuncgamma(2.0*(i)*dx, x)
|
||||
forall(i=1:points/2) sp(2, 2*i - 1) = intfuncgamma((2.0*(i)-1.0)*dx, x)
|
||||
g = (intfuncgamma(0.0, x) + 2.0*sum(sp(1,:)) + 4.0*sum(sp(2,:)) + &
|
||||
intfuncgamma(infty, x))*dx/3.0
|
||||
|
||||
if ( correction ) g = g/a
|
||||
|
||||
end function my_gamma
|
||||
|
||||
|
||||
recursive function lacz_gamma(a) result(g)
|
||||
real, intent(in) :: a
|
||||
real :: g
|
||||
|
||||
real, parameter :: pi = 3.14159265358979324
|
||||
integer, parameter :: cg = 7
|
||||
|
||||
! these precomputed values are taken by the sample code in Wikipedia,
|
||||
! and the sample itself takes them from the GNU Scientific Library
|
||||
real, dimension(0:8), parameter :: p = &
|
||||
(/ 0.99999999999980993, 676.5203681218851, -1259.1392167224028, &
|
||||
771.32342877765313, -176.61502916214059, 12.507343278686905, &
|
||||
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7 /)
|
||||
|
||||
real :: t, w, x
|
||||
integer :: i
|
||||
|
||||
x = a
|
||||
|
||||
if ( x < 0.5 ) then
|
||||
g = pi / ( sin(pi*x) * lacz_gamma(1.0-x) )
|
||||
else
|
||||
x = x - 1.0
|
||||
t = p(0)
|
||||
do i=1, cg+2
|
||||
t = t + p(i)/(x+real(i))
|
||||
end do
|
||||
w = x + real(cg) + 0.5
|
||||
g = sqrt(2.0*pi) * w**(x+0.5) * exp(-w) * t
|
||||
end if
|
||||
end function lacz_gamma
|
||||
|
||||
end program ComputeGammaInt
|
||||
47
Task/Gamma-function/FreeBASIC/gamma-function.basic
Normal file
47
Task/Gamma-function/FreeBASIC/gamma-function.basic
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Const pi = 3.1415926535897932
|
||||
Const e = 2.7182818284590452
|
||||
|
||||
Function gammaStirling (x As Double) As Double
|
||||
Return Sqr(2.0 * pi / x) * ((x / e) ^ x)
|
||||
End Function
|
||||
|
||||
Function gammaLanczos (x As Double) As Double
|
||||
Dim p(0 To 8) As Double = _
|
||||
{ _
|
||||
0.99999999999980993, _
|
||||
676.5203681218851, _
|
||||
-1259.1392167224028, _
|
||||
771.32342877765313, _
|
||||
-176.61502916214059, _
|
||||
12.507343278686905, _
|
||||
-0.13857109526572012, _
|
||||
9.9843695780195716e-6, _
|
||||
1.5056327351493116e-7 _
|
||||
}
|
||||
|
||||
Dim As Integer g = 7
|
||||
If x < 0.5 Then Return pi / (Sin(pi * x) * gammaLanczos(1-x))
|
||||
x -= 1
|
||||
Dim a As Double = p(0)
|
||||
Dim t As Double = x + g + 0.5
|
||||
|
||||
For i As Integer = 1 To 8
|
||||
a += p(i) / (x + i)
|
||||
Next
|
||||
|
||||
Return Sqr(2.0 * pi) * (t ^ (x + 0.5)) * Exp(-t) * a
|
||||
End Function
|
||||
|
||||
Print " x", " Stirling",, " Lanczos"
|
||||
Print
|
||||
For i As Integer = 1 To 20
|
||||
Dim As Double d = i / 10.0
|
||||
Print Using "#.##"; d;
|
||||
Print , Using "#.###############"; gammaStirling(d);
|
||||
Print , Using "#.###############"; gammaLanczos(d)
|
||||
Next
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
27
Task/Gamma-function/Go/gamma-function.go
Normal file
27
Task/Gamma-function/Go/gamma-function.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(" x math.Gamma Lanczos7")
|
||||
for _, x := range []float64{-.5, .1, .5, 1, 1.5, 2, 3, 10, 140, 170} {
|
||||
fmt.Printf("%5.1f %24.16g %24.16g\n", x, math.Gamma(x), lanczos7(x))
|
||||
}
|
||||
}
|
||||
|
||||
func lanczos7(z float64) float64 {
|
||||
t := z + 6.5
|
||||
x := .99999999999980993 +
|
||||
676.5203681218851/z -
|
||||
1259.1392167224028/(z+1) +
|
||||
771.32342877765313/(z+2) -
|
||||
176.61502916214059/(z+3) +
|
||||
12.507343278686905/(z+4) -
|
||||
.13857109526572012/(z+5) +
|
||||
9.9843695780195716e-6/(z+6) +
|
||||
1.5056327351493116e-7/(z+7)
|
||||
return math.Sqrt2 * math.SqrtPi * math.Pow(t, z-.5) * math.Exp(-t) * x
|
||||
}
|
||||
14
Task/Gamma-function/Groovy/gamma-function.groovy
Normal file
14
Task/Gamma-function/Groovy/gamma-function.groovy
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
a = [ 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].reverse()
|
||||
|
||||
def gamma = { 1.0 / a.inject(0) { sm, a_i -> sm * (it - 1) + a_i } }
|
||||
|
||||
(1..10).each{ printf("% 1.9e\n", gamma(it / 3.0)) }
|
||||
21
Task/Gamma-function/Haskell/gamma-function-1.hs
Normal file
21
Task/Gamma-function/Haskell/gamma-function-1.hs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
cof :: [Double]
|
||||
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)
|
||||
|
||||
main :: IO ()
|
||||
main = mapM_ print $ gammaln <$> [0.1,0.2 .. 1.0]
|
||||
22
Task/Gamma-function/Haskell/gamma-function-2.hs
Normal file
22
Task/Gamma-function/Haskell/gamma-function-2.hs
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
import Control.Applicative
|
||||
|
||||
cof :: [Double]
|
||||
cof =
|
||||
[ 76.18009172947146
|
||||
, -86.50532032941677
|
||||
, 24.01409824083091
|
||||
, -1.231739572450155
|
||||
, 0.001208650973866179
|
||||
, -0.000005395239384953
|
||||
]
|
||||
|
||||
gammaln :: Double -> Double
|
||||
gammaln =
|
||||
((+) . negate . (((-) . (5.5 +)) <*> (((*) . (0.5 +)) <*> (log . (5.5 +))))) <*>
|
||||
(log .
|
||||
((/) =<<
|
||||
(2.5066282746310007 *) .
|
||||
(1.000000000190015 +) . sum . zipWith (/) cof . enumFrom . (1 +)))
|
||||
|
||||
main :: IO ()
|
||||
main = mapM_ print $ gammaln <$> [0.1,0.2 .. 1.0]
|
||||
7
Task/Gamma-function/Haskell/gamma-function-3.hs
Normal file
7
Task/Gamma-function/Haskell/gamma-function-3.hs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
procedure main()
|
||||
every write(left(i := !10/10.0,5),gamma(.i))
|
||||
end
|
||||
|
||||
procedure gamma(z) # Stirling's approximation
|
||||
return (2*&pi/z)^0.5 * (z/&e)^z
|
||||
end
|
||||
1
Task/Gamma-function/J/gamma-function-1.j
Normal file
1
Task/Gamma-function/J/gamma-function-1.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
gamma=: !@<:
|
||||
3
Task/Gamma-function/J/gamma-function-2.j
Normal file
3
Task/Gamma-function/J/gamma-function-2.j
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
sbase =: %:@(2p1&%) * %&1x1 ^ ]
|
||||
scorr =: 1 1r12 1r288 _139r51840 _571r2488320&p.@%
|
||||
stirlg=: sbase * scorr
|
||||
6
Task/Gamma-function/J/gamma-function-3.j
Normal file
6
Task/Gamma-function/J/gamma-function-3.j
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
(,. stirlg ,. gamma) 10 1p1 1x1 1.5 1
|
||||
10 362880 362880
|
||||
3.14159 2.28803 2.28804
|
||||
2.71828 1.56746 1.56747
|
||||
1.5 0.886155 0.886227
|
||||
1 0.999499 1
|
||||
31
Task/Gamma-function/Java/gamma-function.java
Normal file
31
Task/Gamma-function/Java/gamma-function.java
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
public class GammaFunction {
|
||||
|
||||
public double st_gamma(double x){
|
||||
return Math.sqrt(2*Math.PI/x)*Math.pow((x/Math.E), x);
|
||||
}
|
||||
|
||||
public double la_gamma(double x){
|
||||
double[] p = {0.99999999999980993, 676.5203681218851, -1259.1392167224028,
|
||||
771.32342877765313, -176.61502916214059, 12.507343278686905,
|
||||
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7};
|
||||
int g = 7;
|
||||
if(x < 0.5) return Math.PI / (Math.sin(Math.PI * x)*la_gamma(1-x));
|
||||
|
||||
x -= 1;
|
||||
double a = p[0];
|
||||
double t = x+g+0.5;
|
||||
for(int 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;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
GammaFunction test = new GammaFunction();
|
||||
System.out.println("Gamma \t\tStirling \t\tLanczos");
|
||||
for(double i = 1; i <= 20; i += 1){
|
||||
System.out.println("" + i/10.0 + "\t\t" + test.st_gamma(i/10.0) + "\t" + test.la_gamma(i/10.0));
|
||||
}
|
||||
}
|
||||
}
|
||||
20
Task/Gamma-function/JavaScript/gamma-function.js
Normal file
20
Task/Gamma-function/JavaScript/gamma-function.js
Normal file
|
|
@ -0,0 +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));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
16
Task/Gamma-function/Jq/gamma-function-1.jq
Normal file
16
Task/Gamma-function/Jq/gamma-function-1.jq
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
def gamma:
|
||||
[
|
||||
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
|
||||
] as $a
|
||||
| (. - 1) as $y
|
||||
| ($a|length) as $n
|
||||
| reduce range(2; 1+$n) as $an
|
||||
($a[$n-1]; (. * $y) + $a[$n - $an])
|
||||
| 1 / . ;
|
||||
16
Task/Gamma-function/Jq/gamma-function-2.jq
Normal file
16
Task/Gamma-function/Jq/gamma-function-2.jq
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
# for reals, but easily extended to complex values
|
||||
def gamma_by_lanczos:
|
||||
def pow(x): if x == 0 then 1 elif x == 1 then . else x * log | exp end;
|
||||
. as $x
|
||||
| ((1|atan) * 4) as $pi
|
||||
| if $x < 0.5 then $pi / ((($pi * $x) | sin) * ((1-$x)|gamma_by_lanczos ))
|
||||
else
|
||||
[ 0.99999999999980993, 676.5203681218851, -1259.1392167224028,
|
||||
771.32342877765313, -176.61502916214059, 12.507343278686905,
|
||||
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7] as $p
|
||||
| ($x - 1) as $x
|
||||
| ($x + 7.5) as $t
|
||||
| reduce range(1; $p|length) as $i
|
||||
($p[0]; . + ($p[$i]/($x + $i) ))
|
||||
* ((2*$pi) | sqrt) * ($t | pow($x+0.5)) * ((-$t)|exp)
|
||||
end;
|
||||
5
Task/Gamma-function/Jq/gamma-function-3.jq
Normal file
5
Task/Gamma-function/Jq/gamma-function-3.jq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
def gamma_by_stirling:
|
||||
def pow(x): if x == 0 then 1 elif x == 1 then . else x * log | exp end;
|
||||
((1|atan) * 8) as $twopi
|
||||
| . as $x
|
||||
| (($twopi/$x) | sqrt) * ( ($x / (1|exp)) | pow($x));
|
||||
6
Task/Gamma-function/Jq/gamma-function-4.jq
Normal file
6
Task/Gamma-function/Jq/gamma-function-4.jq
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
def pad(n): tostring | . + (n - length) * " ";
|
||||
|
||||
" i: gamma lanczos tgamma",
|
||||
(range(1;11)
|
||||
| . / 3.0
|
||||
| "\(pad(18)): \(gamma|pad(18)) : \(gamma_by_lanczos|pad(18)) : \(tgamma)")
|
||||
12
Task/Gamma-function/Jq/gamma-function-5.jq
Normal file
12
Task/Gamma-function/Jq/gamma-function-5.jq
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
$ jq -M -r -n -f Gamma_function_Stirling.jq
|
||||
i: gamma lanczos tgamma
|
||||
0.3333333333333333: 2.6789385347077483 : 2.6789385347077483 : 2.678938534707748
|
||||
0.6666666666666666: 1.3541179394264005 : 1.3541179394263998 : 1.3541179394264005
|
||||
1 : 1 : 0.9999999999999998 : 1
|
||||
1.3333333333333333: 0.8929795115692493 : 0.8929795115692494 : 0.8929795115692493
|
||||
1.6666666666666667: 0.9027452929509336 : 0.9027452929509342 : 0.9027452929509336
|
||||
2 : 1 : 1.0000000000000002 : 1
|
||||
2.3333333333333335: 1.190639348758999 : 1.1906393487589995 : 1.190639348758999
|
||||
2.6666666666666665: 1.5045754882515399 : 1.5045754882515576 : 1.5045754882515558
|
||||
3 : 1.9999999999939684 : 2.0000000000000013 : 2
|
||||
3.3333333333333335: 2.778158479338573 : 2.778158480437665 : 2.7781584804376647
|
||||
56
Task/Gamma-function/Jsish/gamma-function.jsish
Normal file
56
Task/Gamma-function/Jsish/gamma-function.jsish
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
#!/usr/bin/env jsish
|
||||
/* Gamma function, in Jsish, using the Lanczos approximation */
|
||||
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));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (Interp.conf('unitTest')) {
|
||||
for (var i=-5.5; i <= 5.5; i += 0.5) {
|
||||
printf('%2.1f %+e\n', i, gamma(i));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
=!EXPECTSTART!=
|
||||
-5.5 +1.091265e-02
|
||||
-5.0 -4.275508e+13
|
||||
-4.5 -6.001960e-02
|
||||
-4.0 +2.672193e+14
|
||||
-3.5 +2.700882e-01
|
||||
-3.0 -1.425169e+15
|
||||
-2.5 -9.453087e-01
|
||||
-2.0 +6.413263e+15
|
||||
-1.5 +2.363272e+00
|
||||
-1.0 -2.565305e+16
|
||||
-0.5 -3.544908e+00
|
||||
0.0 +inf
|
||||
0.5 +1.772454e+00
|
||||
1.0 +1.000000e+00
|
||||
1.5 +8.862269e-01
|
||||
2.0 +1.000000e+00
|
||||
2.5 +1.329340e+00
|
||||
3.0 +2.000000e+00
|
||||
3.5 +3.323351e+00
|
||||
4.0 +6.000000e+00
|
||||
4.5 +1.163173e+01
|
||||
5.0 +2.400000e+01
|
||||
5.5 +5.234278e+01
|
||||
=!EXPECTEND!=
|
||||
*/
|
||||
1
Task/Gamma-function/Julia/gamma-function-1.julia
Normal file
1
Task/Gamma-function/Julia/gamma-function-1.julia
Normal file
|
|
@ -0,0 +1 @@
|
|||
@show gamma(1)
|
||||
3
Task/Gamma-function/Julia/gamma-function-2.julia
Normal file
3
Task/Gamma-function/Julia/gamma-function-2.julia
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
using QuadGK
|
||||
gammaquad(t::Float64) = first(quadgk(x -> x ^ (t - 1) * exp(-x), zero(t), Inf, reltol = 100eps(t)))
|
||||
@show gammaquad(1.0)
|
||||
2
Task/Gamma-function/Julia/gamma-function-3.julia
Normal file
2
Task/Gamma-function/Julia/gamma-function-3.julia
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
using SpecialFunctions
|
||||
gamma(1/2) - sqrt(pi)
|
||||
35
Task/Gamma-function/Kotlin/gamma-function.kotlin
Normal file
35
Task/Gamma-function/Kotlin/gamma-function.kotlin
Normal 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)))
|
||||
}
|
||||
}
|
||||
65
Task/Gamma-function/Lambdatalk/gamma-function.lambdatalk
Normal file
65
Task/Gamma-function/Lambdatalk/gamma-function.lambdatalk
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
{def gamma.p
|
||||
{A.new 0.99999999999980993
|
||||
676.5203681218851
|
||||
-1259.1392167224028
|
||||
771.32342877765313
|
||||
-176.61502916214059
|
||||
12.507343278686905
|
||||
-0.13857109526572012
|
||||
9.9843695780195716e-6
|
||||
1.5056327351493116e-7
|
||||
}}
|
||||
-> gamma.p
|
||||
|
||||
{def gamma.rec
|
||||
{lambda {:x :a :i}
|
||||
{if {< :i {A.length {gamma.p}}}
|
||||
then {gamma.rec :x
|
||||
{+ :a {/ {A.get :i {gamma.p}} {+ :x :i}} }
|
||||
{+ :i 1}}
|
||||
else :a
|
||||
}}}
|
||||
-> gamma.rec
|
||||
|
||||
{def gamma
|
||||
{lambda {:x}
|
||||
{if {< :x 0.5}
|
||||
then {/ {PI}
|
||||
{* {sin {* {PI} :x}}
|
||||
{gamma {- 1 :x}}}}
|
||||
else {let { {:x {- :x 1}}
|
||||
{:t {+ {- :x 1} 7 0.5}}
|
||||
} {* {sqrt {* 2 {PI}}}
|
||||
{pow :t {+ :x 0.5}}
|
||||
{exp -:t}
|
||||
{gamma.rec :x {A.first {gamma.p}} 1}}
|
||||
}}}}
|
||||
-> gamma
|
||||
|
||||
{S.map {lambda {:i}
|
||||
{div} Γ(:i) = {gamma :i}}
|
||||
{S.serie -5.5 5.5 0.5}}
|
||||
|
||||
Γ(-5.5) = 0.010912654781909836
|
||||
Γ(-5) = -42755084646679.17
|
||||
Γ(-4.5) = -0.06001960130050417
|
||||
Γ(-4) = 267219279041745.34
|
||||
Γ(-3.5) = 0.27008820585226917
|
||||
Γ(-3) = -1425169488222640
|
||||
Γ(-2.5) = -0.9453087204829418
|
||||
Γ(-2) = 6413262697001885
|
||||
Γ(-1.5) = 2.363271801207352
|
||||
Γ(-1) = -25653050788007544
|
||||
Γ(-0.5) = -3.5449077018110295
|
||||
Γ(0) = Infinity
|
||||
Γ(0.5) = 1.7724538509055159
|
||||
Γ(1) = 0.9999999999999998
|
||||
Γ(1.5) = 0.8862269254527586
|
||||
Γ(2) = 1.0000000000000002
|
||||
Γ(2.5) = 1.3293403881791384
|
||||
Γ(3) = 2.000000000000001
|
||||
Γ(3.5) = 3.3233509704478426
|
||||
Γ(4) = 6.000000000000007
|
||||
Γ(4.5) = 11.631728396567446
|
||||
Γ(5) = 23.999999999999996
|
||||
Γ(5.5) = 52.34277778455358
|
||||
44
Task/Gamma-function/Limbo/gamma-function.limbo
Normal file
44
Task/Gamma-function/Limbo/gamma-function.limbo
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
implement Lanczos7;
|
||||
|
||||
include "sys.m"; sys: Sys;
|
||||
include "draw.m";
|
||||
include "math.m"; math: Math;
|
||||
lgamma, exp, pow, sqrt: import math;
|
||||
|
||||
Lanczos7: module {
|
||||
init: fn(nil: ref Draw->Context, nil: list of string);
|
||||
};
|
||||
|
||||
init(nil: ref Draw->Context, nil: list of string)
|
||||
{
|
||||
sys = load Sys Sys->PATH;
|
||||
math = load Math Math->PATH;
|
||||
# We ignore some floating point exceptions:
|
||||
math->FPcontrol(0, Math->OVFL|Math->UNFL);
|
||||
ns : list of real = -0.5 :: 0.1 :: 0.5 :: 1.0 :: 1.5 :: 2.0 :: 3.0 :: 10.0 :: 140.0 :: 170.0 :: nil;
|
||||
|
||||
sys->print("%5s %24s %24s\n", "x", "math->lgamma", "lanczos7");
|
||||
while(ns != nil) {
|
||||
x := hd ns;
|
||||
ns = tl ns;
|
||||
# math->lgamma returns a tuple.
|
||||
(i, r) := lgamma(x);
|
||||
g := real i * exp(r);
|
||||
sys->print("%5.1f %24.16g %24.16g\n", x, g, lanczos7(x));
|
||||
}
|
||||
}
|
||||
|
||||
lanczos7(z: real): real
|
||||
{
|
||||
t := z + 6.5;
|
||||
x := 0.99999999999980993 +
|
||||
676.5203681218851/z -
|
||||
1259.1392167224028/(z+1.0) +
|
||||
771.32342877765313/(z+2.0) -
|
||||
176.61502916214059/(z+3.0) +
|
||||
12.507343278686905/(z+4.0) -
|
||||
0.13857109526572012/(z+5.0) +
|
||||
9.9843695780195716e-6/(z+6.0) +
|
||||
1.5056327351493116e-7/(z+7.0);
|
||||
return sqrt(2.0) * sqrt(Math->Pi) * pow(t, z - 0.5) * exp(-t) * x;
|
||||
}
|
||||
11
Task/Gamma-function/Lua/gamma-function.lua
Normal file
11
Task/Gamma-function/Lua/gamma-function.lua
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
gamma, coeff, quad, qui, set = 0.577215664901, -0.65587807152056, -0.042002635033944, 0.16653861138228, -0.042197734555571
|
||||
function recigamma(z)
|
||||
return z + gamma * z^2 + coeff * z^3 + quad * z^4 + qui * z^5 + set * z^6
|
||||
end
|
||||
|
||||
function gammafunc(z)
|
||||
if z == 1 then return 1
|
||||
elseif math.abs(z) <= 0.5 then return 1 / recigamma(z)
|
||||
else return (z - 1) * gammafunc(z-1)
|
||||
end
|
||||
end
|
||||
39
Task/Gamma-function/M2000-Interpreter/gamma-function.m2000
Normal file
39
Task/Gamma-function/M2000-Interpreter/gamma-function.m2000
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
Module PrepareLambdaFunctions {
|
||||
Const e = 2.7182818284590452@
|
||||
Exp= Lambda e (x) -> e^x
|
||||
gammaStirling=lambda e (x As decimal)->Sqrt(2.0 * pi / x) * ((x / e) ^ x)
|
||||
Rad2Deg =Lambda pidivby180=pi/180 (RadAngle)->RadAngle / pidivby180
|
||||
Dim p(9)
|
||||
p(0)=0.99999999999980993@, 676.5203681218851@, -1259.1392167224028@, 771.32342877765313@
|
||||
p(4)=-176.61502916214059@, 12.507343278686905@, -0.13857109526572012@, 0.0000099843695780195716@
|
||||
p(8)=0.00000015056327351493116@
|
||||
gammaLanczos =Lambda p(), Rad2Deg, Exp (x As decimal) -> {
|
||||
Def Decimal a, t
|
||||
If x < 0.5 Then =pi / (Sin(Rad2Deg(pi * x)) *Lambda(1-x)) : Exit
|
||||
x -= 1@
|
||||
a=p(0)
|
||||
t = x + 7.5@
|
||||
For i= 1@ To 8@ {
|
||||
a += p(i) / (x + i)
|
||||
}
|
||||
= Sqrt(2.0 * pi) * (t ^ (x + 0.5)) * Exp(-t) * a
|
||||
}
|
||||
Push gammaStirling, gammaLanczos
|
||||
}
|
||||
Call PrepareLambdaFunctions
|
||||
Read gammaLanczos, gammaStirling
|
||||
Font "Courier New"
|
||||
Form 120, 40
|
||||
document doc$=" χ Stirling Lanczos"+{
|
||||
}
|
||||
Print $(2,20),"x", "Stirling",@(55),"Lanczos", $(0)
|
||||
Print
|
||||
For d = 0.1 To 2 step 0.1
|
||||
Print $("0.00"), d,
|
||||
Print $("0.000000000000000"), gammaStirling(d),
|
||||
Print $("0.0000000000000000000000000000"), gammaLanczos(d)
|
||||
doc$=format$("{0:-10} {1:-30} {2:-34}",str$(d,"0.00"), str$(gammaStirling(d),"0.000000000000000"), str$(gammaLanczos(d),"0.0000000000000000000000000000"))+{
|
||||
}
|
||||
Next d
|
||||
Print $("")
|
||||
clipboard doc$
|
||||
4
Task/Gamma-function/Maple/gamma-function.maple
Normal file
4
Task/Gamma-function/Maple/gamma-function.maple
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
GAMMA(17/2);
|
||||
GAMMA(7*I);
|
||||
M := Matrix(2, 3, 'fill' = -3.6);
|
||||
MTM:-gamma(M);
|
||||
1
Task/Gamma-function/Mathematica/gamma-function.math
Normal file
1
Task/Gamma-function/Mathematica/gamma-function.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
Gamma[x]
|
||||
21
Task/Gamma-function/Maxima/gamma-function.maxima
Normal file
21
Task/Gamma-function/Maxima/gamma-function.maxima
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
fpprec: 30$
|
||||
|
||||
gamma_coeff(n) := block([a: makelist(1, n)],
|
||||
a[2]: bfloat(%gamma),
|
||||
for k from 3 thru n do
|
||||
a[k]: bfloat((sum((-1)^j * zeta(j) * a[k - j], j, 2, k - 1) - a[2] * a[k - 1]) / (1 - k * a[1])),
|
||||
a)$
|
||||
|
||||
poleval(a, x) := block([y: 0],
|
||||
for k from length(a) thru 1 step -1 do
|
||||
y: y * x + a[k],
|
||||
y)$
|
||||
|
||||
gc: gamma_coeff(20)$
|
||||
|
||||
gamma_approx(x) := block([y: 1],
|
||||
while x > 2 do (x: x - 1, y: y * x),
|
||||
y / (poleval(gc, x - 1)))$
|
||||
|
||||
gamma_approx(12.3b0) - gamma(12.3b0);
|
||||
/* -9.25224705314470500985141176997b-15 */
|
||||
37
Task/Gamma-function/Modula-3/gamma-function.mod3
Normal file
37
Task/Gamma-function/Modula-3/gamma-function.mod3
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
MODULE Gamma EXPORTS Main;
|
||||
|
||||
FROM IO IMPORT Put;
|
||||
FROM Fmt IMPORT Extended, Style;
|
||||
|
||||
PROCEDURE Taylor(x: EXTENDED): EXTENDED =
|
||||
CONST a = ARRAY [0..29] OF EXTENDED {
|
||||
1.00000000000000000000X0, 0.57721566490153286061X0,
|
||||
-0.65587807152025388108X0, -0.04200263503409523553X0,
|
||||
0.16653861138229148950X0, -0.04219773455554433675X0,
|
||||
-0.00962197152787697356X0, 0.00721894324666309954X0,
|
||||
-0.00116516759185906511X0, -0.00021524167411495097X0,
|
||||
0.00012805028238811619X0, -0.00002013485478078824X0,
|
||||
-0.00000125049348214267X0, 0.00000113302723198170X0,
|
||||
-0.00000020563384169776X0, 0.00000000611609510448X0,
|
||||
0.00000000500200764447X0, -0.00000000118127457049X0,
|
||||
0.00000000010434267117X0, 0.00000000000778226344X0,
|
||||
-0.00000000000369680562X0, 0.00000000000051003703X0,
|
||||
-0.00000000000002058326X0, -0.00000000000000534812X0,
|
||||
0.00000000000000122678X0, -0.00000000000000011813X0,
|
||||
0.00000000000000000119X0, 0.00000000000000000141X0,
|
||||
-0.00000000000000000023X0, 0.00000000000000000002X0 };
|
||||
VAR y := x - 1.0X0;
|
||||
sum := a[LAST(a)];
|
||||
|
||||
BEGIN
|
||||
FOR i := LAST(a) - 1 TO FIRST(a) BY -1 DO
|
||||
sum := sum * y + a[i];
|
||||
END;
|
||||
RETURN 1.0X0 / sum;
|
||||
END Taylor;
|
||||
|
||||
BEGIN
|
||||
FOR i := 1 TO 10 DO
|
||||
Put(Extended(Taylor(FLOAT(i, EXTENDED) / 3.0X0), style := Style.Sci) & "\n");
|
||||
END;
|
||||
END Gamma.
|
||||
27
Task/Gamma-function/Nim/gamma-function.nim
Normal file
27
Task/Gamma-function/Nim/gamma-function.nim
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import math, strformat
|
||||
|
||||
const A = [
|
||||
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 ]
|
||||
|
||||
proc gamma(x: float): float =
|
||||
let y = x - 1
|
||||
result = A[^1]
|
||||
for n in countdown(A.high - 1, A.low):
|
||||
result = result * y + A[n]
|
||||
result = 1 / result
|
||||
|
||||
echo "Our gamma function Nim gamma function Difference"
|
||||
echo "------------------ ------------------ ----------"
|
||||
for i in 1..10:
|
||||
let val1 = gamma(i.toFloat / 3)
|
||||
let val2 = math.gamma(i.toFloat / 3)
|
||||
echo &"{val1:18.16f} {val2:18.16f} {val1 - val2:11.4e}"
|
||||
63
Task/Gamma-function/OCaml/gamma-function.ocaml
Normal file
63
Task/Gamma-function/OCaml/gamma-function.ocaml
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
let e = exp 1.
|
||||
let pi = 4. *. atan 1.
|
||||
let sqrttwopi = sqrt (2. *. pi)
|
||||
|
||||
module Lanczos = struct
|
||||
(* Lanczos method *)
|
||||
(* Coefficients used by the GNU Scientific Library *)
|
||||
let g = 7.
|
||||
let c = [|0.99999999999980993; 676.5203681218851; -1259.1392167224028;
|
||||
771.32342877765313; -176.61502916214059; 12.507343278686905;
|
||||
-0.13857109526572012; 9.9843695780195716e-6; 1.5056327351493116e-7|]
|
||||
|
||||
let rec ag z d =
|
||||
if d = 0 then c.(0) +. ag z 1
|
||||
else if d < 8 then c.(d) /. (z +. float d) +. ag z (succ d)
|
||||
else c.(d) /. (z +. float d)
|
||||
|
||||
let gamma z =
|
||||
let z = z -. 1. in
|
||||
let p = z +. g +. 0.5 in
|
||||
sqrttwopi *. p ** (z +. 0.5) *. exp (-. p) *. ag z 0
|
||||
end
|
||||
|
||||
module Stirling = struct
|
||||
(* Stirling method *)
|
||||
let gamma z =
|
||||
sqrttwopi /. sqrt z *. (z /. e) ** z
|
||||
|
||||
end
|
||||
|
||||
module Stirling2 = struct
|
||||
(* Extended Stirling method seen in Abramowitz and Stegun *)
|
||||
let d = [|1./.12.; 1./.288.; -139./.51840.; -571./.2488320.|]
|
||||
|
||||
let rec corr z x n =
|
||||
if n < Array.length d - 1 then d.(n) /. x +. corr z (x *. z) (succ n)
|
||||
else d.(n) /. x
|
||||
|
||||
let gamma z = Stirling.gamma z *. (1. +. corr z z 0)
|
||||
end
|
||||
|
||||
let mirror gma z =
|
||||
if z > 0.5 then gma z
|
||||
else pi /. sin (pi *. z) /. gma (1. -. z)
|
||||
|
||||
let _ =
|
||||
Printf.printf "z\t\tLanczos\t\tStirling\tStirling2\n";
|
||||
for i = 1 to 20 do
|
||||
let z = float i /. 10. in
|
||||
Printf.printf "%-10.8g\t%10.8e\t%10.8e\t%10.8e\n"
|
||||
z
|
||||
(mirror Lanczos.gamma z)
|
||||
(mirror Stirling.gamma z)
|
||||
(mirror Stirling2.gamma z)
|
||||
done;
|
||||
for i = 1 to 7 do
|
||||
let z = 10. *. float i in
|
||||
Printf.printf "%-10.8g\t%10.8e\t%10.8e\t%10.8e\n"
|
||||
z
|
||||
(Lanczos.gamma z)
|
||||
(Stirling.gamma z)
|
||||
(Stirling2.gamma z)
|
||||
done
|
||||
22
Task/Gamma-function/Octave/gamma-function.octave
Normal file
22
Task/Gamma-function/Octave/gamma-function.octave
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
function g = lacz_gamma(a, cg=7)
|
||||
p = [ 0.99999999999980993, 676.5203681218851, -1259.1392167224028, \
|
||||
771.32342877765313, -176.61502916214059, 12.507343278686905, \
|
||||
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7 ];
|
||||
x=a;
|
||||
if ( x < 0.5 )
|
||||
g = pi / ( sin(pi*x) * lacz_gamma(1.0-x) );
|
||||
else
|
||||
x = x - 1.0;
|
||||
t = p(1);
|
||||
for i=1:(cg+1)
|
||||
t = t + p(i+1)/(x+double(i));
|
||||
endfor
|
||||
w = x + double(cg) + 0.5;
|
||||
g = sqrt(2.0*pi) * w**(x+0.5) * exp(-w) * t;
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
for i = 1:10
|
||||
printf("%f %f\n", gamma(i/3.0), lacz_gamma(i/3.0));
|
||||
endfor
|
||||
17
Task/Gamma-function/Oforth/gamma-function.fth
Normal file
17
Task/Gamma-function/Oforth/gamma-function.fth
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import: math
|
||||
|
||||
[
|
||||
676.5203681218851, -1259.1392167224028, 771.32342877765313,
|
||||
-176.61502916214059, 12.507343278686905, -0.13857109526572012,
|
||||
9.9843695780195716e-6, 1.5056327351493116e-7
|
||||
] const: Gamma.Lanczos
|
||||
|
||||
: gamma(z)
|
||||
| i t |
|
||||
z 0.5 < ifTrue: [ Pi dup z * sin 1.0 z - gamma * / return ]
|
||||
z 1.0 - ->z
|
||||
0.99999999999980993 Gamma.Lanczos size loop: i [ i Gamma.Lanczos at z i + / + ]
|
||||
z Gamma.Lanczos size + 0.5 - ->t
|
||||
2 Pi * sqrt *
|
||||
t z 0.5 + powf *
|
||||
t neg exp * ;
|
||||
1
Task/Gamma-function/PARI-GP/gamma-function-1.parigp
Normal file
1
Task/Gamma-function/PARI-GP/gamma-function-1.parigp
Normal file
|
|
@ -0,0 +1 @@
|
|||
gamma(x)
|
||||
1
Task/Gamma-function/PARI-GP/gamma-function-2.parigp
Normal file
1
Task/Gamma-function/PARI-GP/gamma-function-2.parigp
Normal file
|
|
@ -0,0 +1 @@
|
|||
Gamma(x)=intnum(t=0,[+oo,1],t^(x-1)/exp(t))
|
||||
1
Task/Gamma-function/PARI-GP/gamma-function-3.parigp
Normal file
1
Task/Gamma-function/PARI-GP/gamma-function-3.parigp
Normal file
|
|
@ -0,0 +1 @@
|
|||
Gamma(x)=intnumromb(t=0,9,t^(x-1)/exp(t),0)+intnumromb(t=9,max(x,99)^9,t^(x-1)/exp(t),2)
|
||||
1
Task/Gamma-function/PARI-GP/gamma-function-4.parigp
Normal file
1
Task/Gamma-function/PARI-GP/gamma-function-4.parigp
Normal file
|
|
@ -0,0 +1 @@
|
|||
Stirling(x)=x--;sqrt(2*Pi*x)*(x/exp(1))^x
|
||||
45
Task/Gamma-function/PL-I/gamma-function.pli
Normal file
45
Task/Gamma-function/PL-I/gamma-function.pli
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
/* From Rosetta Fortran */
|
||||
test: procedure options (main);
|
||||
|
||||
declare i fixed binary;
|
||||
|
||||
on underflow ;
|
||||
|
||||
put skip list ('Lanczos', 'Builtin' );
|
||||
do i = 1 to 10;
|
||||
put skip list (lanczos_gamma(i/3.0q0), gamma(i/3.0q0) );
|
||||
end;
|
||||
|
||||
|
||||
lanczos_gamma: procedure (a) returns (float (18)) recursive;
|
||||
declare a float (18);
|
||||
declare pi float (18) value (3.14159265358979324E0);
|
||||
declare cg fixed binary initial ( 7 );
|
||||
|
||||
/* these precomputed values are taken by the sample code in Wikipedia, */
|
||||
/* and the sample itself takes them from the GNU Scientific Library */
|
||||
declare p(0:8) float (18) static initial
|
||||
( 0.99999999999980993e0, 676.5203681218851e0, -1259.1392167224028e0,
|
||||
771.32342877765313e0, -176.61502916214059e0, 12.507343278686905e0,
|
||||
-0.13857109526572012e0, 9.9843695780195716e-6, 1.5056327351493116e-7 );
|
||||
|
||||
declare ( t, w, x ) float (18);
|
||||
declare i fixed binary;
|
||||
|
||||
x = a;
|
||||
|
||||
if x < 0.5 then
|
||||
return ( pi / ( sin(pi*x) * lanczos_gamma(1.0-x) ) );
|
||||
else
|
||||
do;
|
||||
x = x - 1.0;
|
||||
t = p(0);
|
||||
do i = 1 to cg+2;
|
||||
t = t + p(i)/(x+i);
|
||||
end;
|
||||
w = x + float(cg) + 0.5;
|
||||
return ( sqrt(2*pi) * w**(x+0.5) * exp(-w) * t );
|
||||
end;
|
||||
end lanczos_gamma;
|
||||
|
||||
end test;
|
||||
76
Task/Gamma-function/Pascal/gamma-function.pas
Normal file
76
Task/Gamma-function/Pascal/gamma-function.pas
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
program GammaTest;
|
||||
{$mode objfpc}{$H+}
|
||||
uses SysUtils;
|
||||
|
||||
function Gamma( x : extended) : extended;
|
||||
const COF : array [0..14] of extended =
|
||||
( 0.999999999999997092, // may as well include this in the array
|
||||
57.1562356658629235,
|
||||
-59.5979603554754912,
|
||||
14.1360979747417471,
|
||||
-0.491913816097620199,
|
||||
0.339946499848118887e-4,
|
||||
0.465236289270485756e-4,
|
||||
-0.983744753048795646e-4,
|
||||
0.158088703224912494e-3,
|
||||
-0.210264441724104883e-3,
|
||||
0.217439618115212643e-3,
|
||||
-0.164318106536763890e-3,
|
||||
0.844182239838527433e-4,
|
||||
-0.261908384015814087e-4,
|
||||
0.368991826595316234e-5);
|
||||
const
|
||||
K = 2.5066282746310005;
|
||||
PI_OVER_K = PI / K;
|
||||
var
|
||||
j : integer;
|
||||
tmp, w, ser : extended;
|
||||
reflect : boolean;
|
||||
begin
|
||||
reflect := (x < 0.5);
|
||||
if reflect then w := 1.0 - x else w := x;
|
||||
tmp := w + 5.2421875;
|
||||
tmp := (w + 0.5)*Ln(tmp) - tmp;
|
||||
ser := COF[0];
|
||||
for j := 1 to 14 do ser := ser + COF[j]/(w + j);
|
||||
try
|
||||
if reflect then
|
||||
result := PI_OVER_K * w * Exp(-tmp) / (Sin(PI*x) * ser)
|
||||
else
|
||||
result := K * Exp(tmp) * ser / w;
|
||||
except
|
||||
raise SysUtils.Exception.CreateFmt(
|
||||
'Gamma(%g) is undefined or out of floating-point range', [x]);
|
||||
end;
|
||||
end;
|
||||
|
||||
// Main routine for testing the Gamma function
|
||||
var
|
||||
x, k : extended;
|
||||
begin
|
||||
WriteLn( 'Is it seamless at x = 1/2 ?');
|
||||
x := 0.49999999999999;
|
||||
WriteLn( SysUtils.Format( 'Gamma(%g) = %g', [x, Gamma(x)]));
|
||||
x := 0.50000000000001;
|
||||
WriteLn( SysUtils.Format( 'Gamma(%g) = %g', [x, Gamma(x)]));
|
||||
WriteLn( 'Test a few values:');
|
||||
WriteLn( SysUtils.Format( 'Gamma(1) = %g', [Gamma(1)]));
|
||||
WriteLn( SysUtils.Format( 'Gamma(2) = %g', [Gamma(2)]));
|
||||
WriteLn( SysUtils.Format( 'Gamma(3) = %g', [Gamma(3)]));
|
||||
WriteLn( SysUtils.Format( 'Gamma(10) = %g', [Gamma(10)]));
|
||||
WriteLn( SysUtils.Format( 'Gamma(101) = %g', [Gamma(101)]));
|
||||
WriteLn( ' 100! = 9.33262154439442E157');
|
||||
WriteLn( SysUtils.Format( 'Gamma(1/2) = %g', [Gamma(0.5)]));
|
||||
WriteLn( SysUtils.Format( 'Sqrt(pi) = %g', [Sqrt(PI)]));
|
||||
WriteLn( SysUtils.Format( 'Gamma(-7/2) = %g', [Gamma(-3.5)]));
|
||||
(*
|
||||
Note here a bug or misfeature in Lazarus (doesn't occur in Delphi):
|
||||
Putting (16.0/105.0)*Sqrt(PI) does not give the required precision.
|
||||
We have to explicitly define the integers as extended floating-point.
|
||||
*)
|
||||
k := extended(16.0)/extended(105.0);
|
||||
WriteLn( SysUtils.Format( ' (16/105)Sqrt(pi) = %g', [k*Sqrt(PI)]));
|
||||
WriteLn( SysUtils.Format( 'Gamma(-9/2) = %g', [Gamma(-4.5)]));
|
||||
k := extended(32.0)/extended(945.0);
|
||||
WriteLn( SysUtils.Format( '-(32/945)Sqrt(pi) = %g', [-k*Sqrt(PI)]));
|
||||
end.
|
||||
70
Task/Gamma-function/Perl/gamma-function.pl
Normal file
70
Task/Gamma-function/Perl/gamma-function.pl
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
use strict;
|
||||
use warnings;
|
||||
use constant pi => 4*atan2(1, 1);
|
||||
use constant e => exp(1);
|
||||
|
||||
# Normally would be: use Math::MPFR
|
||||
# but this will use it if it's installed and ignore otherwise
|
||||
my $have_MPFR = eval { require Math::MPFR; Math::MPFR->import(); 1; };
|
||||
|
||||
sub Gamma {
|
||||
my $z = shift;
|
||||
my $method = shift // 'lanczos';
|
||||
if ($method eq 'lanczos') {
|
||||
use constant g => 9;
|
||||
$z < .5 ? pi / sin(pi * $z) / Gamma(1 - $z, $method) :
|
||||
sqrt(2* pi) *
|
||||
($z + g - .5)**($z - .5) *
|
||||
exp(-($z + g - .5)) *
|
||||
do {
|
||||
my @coeff = qw{
|
||||
1.000000000000000174663
|
||||
5716.400188274341379136
|
||||
-14815.30426768413909044
|
||||
14291.49277657478554025
|
||||
-6348.160217641458813289
|
||||
1301.608286058321874105
|
||||
-108.1767053514369634679
|
||||
2.605696505611755827729
|
||||
-0.7423452510201416151527e-2
|
||||
0.5384136432509564062961e-7
|
||||
-0.4023533141268236372067e-8
|
||||
};
|
||||
my ($sum, $i) = (shift(@coeff), 0);
|
||||
$sum += $_ / ($z + $i++) for @coeff;
|
||||
$sum;
|
||||
}
|
||||
} elsif ($method eq 'taylor') {
|
||||
$z < .5 ? Gamma($z+1, $method)/$z :
|
||||
$z > 1.5 ? ($z-1)*Gamma($z-1, $method) :
|
||||
do {
|
||||
my $s = 0; ($s *= $z-1) += $_ for qw{
|
||||
0.00000000000000000002 -0.00000000000000000023 0.00000000000000000141
|
||||
0.00000000000000000119 -0.00000000000000011813 0.00000000000000122678
|
||||
-0.00000000000000534812 -0.00000000000002058326 0.00000000000051003703
|
||||
-0.00000000000369680562 0.00000000000778226344 0.00000000010434267117
|
||||
-0.00000000118127457049 0.00000000500200764447 0.00000000611609510448
|
||||
-0.00000020563384169776 0.00000113302723198170 -0.00000125049348214267
|
||||
-0.00002013485478078824 0.00012805028238811619 -0.00021524167411495097
|
||||
-0.00116516759185906511 0.00721894324666309954 -0.00962197152787697356
|
||||
-0.04219773455554433675 0.16653861138229148950 -0.04200263503409523553
|
||||
-0.65587807152025388108 0.57721566490153286061 1.00000000000000000000
|
||||
}; 1/$s;
|
||||
}
|
||||
} elsif ($method eq 'stirling') {
|
||||
no warnings qw(recursion);
|
||||
$z < 100 ? Gamma($z + 1, $method)/$z :
|
||||
sqrt(2*pi*$z)*($z/e + 1/(12*e*$z))**$z / $z;
|
||||
} elsif ($method eq 'MPFR') {
|
||||
my $result = Math::MPFR->new();
|
||||
Math::MPFR::Rmpfr_gamma($result, Math::MPFR->new($z), 0);
|
||||
$result;
|
||||
} else { die "unknown method '$method'" }
|
||||
}
|
||||
|
||||
for my $method (qw(MPFR lanczos taylor stirling)) {
|
||||
next if $method eq 'MPFR' && !$have_MPFR;
|
||||
printf "%10s: ", $method;
|
||||
print join(' ', map { sprintf "%.12f", Gamma($_/3, $method) } 1 .. 10);
|
||||
print "\n";
|
||||
}
|
||||
116
Task/Gamma-function/Phix/gamma-function-1.phix
Normal file
116
Task/Gamma-function/Phix/gamma-function-1.phix
Normal file
|
|
@ -0,0 +1,116 @@
|
|||
(phixonline)-->
|
||||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">c</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">12</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">spouge_gamma</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">accm</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">accm</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">accm</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">c</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">accm</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">k1_factrl</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span> <span style="color: #000080;font-style:italic;">-- (k - 1)!*(-1)^k with 0!==1</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">c</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">13</span><span style="color: #0000FF;">-</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)*</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">13</span><span style="color: #0000FF;">-</span><span style="color: #000000;">k</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1.5</span><span style="color: #0000FF;">)/</span><span style="color: #000000;">k1_factrl</span>
|
||||
<span style="color: #000000;">k1_factrl</span> <span style="color: #0000FF;">*=</span> <span style="color: #0000FF;">-(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #000000;">12</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">accm</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">c</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]/(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">+</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000000;">accm</span> <span style="color: #0000FF;">*=</span> <span style="color: #7060A8;">exp</span><span style="color: #0000FF;">(-(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">+</span><span style="color: #000000;">12</span><span style="color: #0000FF;">))*</span><span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">+</span><span style="color: #000000;">12</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">+</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- Gamma(z+1)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">accm</span><span style="color: #0000FF;">/</span><span style="color: #000000;">z</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">taylor_gamma</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">-- (good for values between 0 and 1, apparently)</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">1.00000_00000_00000_00000</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">0.57721_56649_01532_86061</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.65587_80715_20253_88108</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.04200_26350_34095_23553</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">0.16653_86113_82291_48950</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.04219_77345_55544_33675</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.00962_19715_27876_97356</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">0.00721_89432_46663_09954</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.00116_51675_91859_06511</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.00021_52416_74114_95097</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">0.00012_80502_82388_11619</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.00002_01348_54780_78824</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.00000_12504_93482_14267</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">0.00000_11330_27231_98170</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.00000_02056_33841_69776</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">0.00000_00061_16095_10448</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">0.00000_00050_02007_64447</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.00000_00011_81274_57049</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">0.00000_00001_04342_67117</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">0.00000_00000_07782_26344</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.00000_00000_03696_80562</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">0.00000_00000_00510_03703</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.00000_00000_00020_58326</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.00000_00000_00005_34812</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">0.00000_00000_00001_22678</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.00000_00000_00000_11813</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">0.00000_00000_00000_00119</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">0.00000_00000_00000_00141</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.00000_00000_00000_00023</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">0.00000_00000_00000_00002</span> <span style="color: #0000FF;">}</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">[$]</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1</span> <span style="color: #008080;">by</span> <span style="color: #0000FF;">-</span><span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">*</span><span style="color: #000000;">y</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">n</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">s</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">lanczos_gamma</span><span style="color: #0000FF;">(</span><span style="color: #004080;">atom</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">z</span><span style="color: #0000FF;"><</span><span style="color: #000000;">0.5</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #004600;">PI</span> <span style="color: #0000FF;">/</span> <span style="color: #0000FF;">(</span><span style="color: #7060A8;">sin</span><span style="color: #0000FF;">(</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">*</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)*</span><span style="color: #000000;">lanczos_gamma</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">-</span><span style="color: #000000;">z</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000080;font-style:italic;">-- use a lanczos approximation:</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0.99999999999980993</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">z</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">6.5</span><span style="color: #0000FF;">;</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span> <span style="color: #000000;">676.5203681218851</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">1259.1392167224028</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">771.32342877765313</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">176.61502916214059</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">12.507343278686905</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #0000FF;">-</span><span style="color: #000000;">0.13857109526572012</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">9.9843695780195716e-6</span><span style="color: #0000FF;">,</span>
|
||||
<span style="color: #000000;">1.5056327351493116e-7</span> <span style="color: #0000FF;">}</span>
|
||||
<span style="color: #000000;">z</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000000;">x</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">p</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">/</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">z</span> <span style="color: #0000FF;">+</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">2</span><span style="color: #0000FF;">*</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">*</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">+</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">*</span> <span style="color: #7060A8;">exp</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">x</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">sqPI</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sqrt</span><span style="color: #0000FF;">(</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">sq</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">zm</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span><span style="color: #0000FF;">=</span><span style="color: #008000;">"%19.16f"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">mul</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">zm</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sqPI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">mul</span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">spouge_gamma</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">taylor_gamma</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">lanczos_gamma</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)},</span>
|
||||
<span style="color: #000000;">error</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_abs</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_sub</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">", "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">:=</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">", "</span>
|
||||
<span style="color: #004080;">integer</span> <span style="color: #000000;">bdx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">smallest</span><span style="color: #0000FF;">(</span><span style="color: #000000;">error</span><span style="color: #0000FF;">,</span><span style="color: #000000;">return_index</span><span style="color: #0000FF;">:=</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">atom</span> <span style="color: #000000;">best</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">bdx</span><span style="color: #0000FF;">],</span>
|
||||
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">bdx</span><span style="color: #0000FF;">]*</span><span style="color: #000000;">mul</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span> <span style="color: #000080;font-style:italic;">-- (potentially mark >1)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]=</span><span style="color: #000000;">best</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">t</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">*</span><span style="color: #000000;">22</span><span style="color: #0000FF;">-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..</span><span style="color: #000000;">i</span><span style="color: #0000FF;">*</span><span style="color: #000000;">22</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"*,"</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">es</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #000000;">e</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%5g: %s %s, %19.16f\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">es</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">*</span><span style="color: #000000;">p</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" z ------ spouge ----- ----- taylor ------ ----- lanczos ----- ---- expected ----- %19.16f\n"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">PI</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">({{-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">/</span><span style="color: #000000;">4</span><span style="color: #0000FF;">},{-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sqPI</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sqPI</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">5</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">/</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sqPI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">7</span><span style="color: #0000FF;">/</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">8</span><span style="color: #0000FF;">/</span><span style="color: #000000;">15</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sqPI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">6</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">sq</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">sq</span><span style="color: #0000FF;">({</span><span style="color: #000000;">0.001</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sqPI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">999.4237725</span><span style="color: #0000FF;">},</span><span style="color: #008000;">"%19.15f"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">sq</span><span style="color: #0000FF;">({</span><span style="color: #000000;">0.01</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sqPI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">99.43258512</span><span style="color: #0000FF;">},</span><span style="color: #008000;">"%19.16f"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">sq</span><span style="color: #0000FF;">({</span><span style="color: #000000;">0.1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sqPI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">9.513507699</span><span style="color: #0000FF;">},</span><span style="color: #008000;">"%19.16f"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">sq</span><span style="color: #0000FF;">({</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sqPI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">362880</span><span style="color: #0000FF;">},</span><span style="color: #008000;">"%19.12f"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">sq</span><span style="color: #0000FF;">({</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sqPI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">9.332621544e155</span><span style="color: #0000FF;">},</span><span style="color: #008000;">"%19.13g"</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">64</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">sq</span><span style="color: #0000FF;">({</span><span style="color: #000000;">150</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sqPI</span><span style="color: #0000FF;">/</span><span style="color: #000000;">3.808922638e260</span><span style="color: #0000FF;">},</span><span style="color: #008000;">"%19.13g"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (fatal power overflow error on 32 bits)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<!--
|
||||
109
Task/Gamma-function/Phix/gamma-function-2.phix
Normal file
109
Task/Gamma-function/Phix/gamma-function-2.phix
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
-->
|
||||
<span style="color: #008080;">without</span> <span style="color: #008080;">javascript_semantics</span> <span style="color: #000080;font-style:italic;">-- (no mpfr_exp(), mpfr_gamma() in pwa/p2js)</span>
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">dp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">30</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"%5s: %33s, %33s, %32s\n"</span>
|
||||
<span style="color: #7060A8;">requires</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"1.0.2"</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (mpfr_get_fixed(maxlen), mpfr_gamma)</span>
|
||||
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
|
||||
<span style="color: #7060A8;">mpfr_set_default_precision</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">87</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 87 decimal places. </span>
|
||||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">mc</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_inits</span><span style="color: #0000FF;">(</span><span style="color: #000000;">40</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">mpfr_spouge_gamma</span><span style="color: #0000FF;">(</span><span style="color: #004080;">mpfr</span> <span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">accm</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mc</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #7060A8;">mpfr_cmp_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000080;font-style:italic;">-- mc[1] := sqrt(2*PI)</span>
|
||||
<span style="color: #7060A8;">mpfr_const_pi</span><span style="color: #0000FF;">(</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">-- k1_factrl = (k - 1)!*(-1)^k with 0!==1</span>
|
||||
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">k1_factrl</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">tmk</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(),</span>
|
||||
<span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mc</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000080;font-style:italic;">-- mc[k] = exp(13-k)*power(13-k,k-1.5)/k1_factrl</span>
|
||||
<span style="color: #7060A8;">mpfr_set_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tmk</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mc</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">-</span><span style="color: #000000;">k</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">mpfr_exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mc</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">],</span><span style="color: #000000;">tmk</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_set_d</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1.5</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">mpfr_pow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">tmk</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k1_factrl</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mc</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">],</span><span style="color: #000000;">mc</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">],</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">-- k1_factrl *= -(k-1)</span>
|
||||
<span style="color: #7060A8;">mpfr_mul_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">k1_factrl</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k1_factrl</span><span style="color: #0000FF;">,-(</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #000000;">accm</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">for</span> <span style="color: #000000;">k</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mc</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
|
||||
<span style="color: #000080;font-style:italic;">-- accm += mc[k]/(z+k-1)</span>
|
||||
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">ck</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mc</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">]),</span>
|
||||
<span style="color: #000000;">zk</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_add_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">zk</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zk</span><span style="color: #0000FF;">,</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ck</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ck</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zk</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ck</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
|
||||
<span style="color: #000080;font-style:italic;">-- atom zc = z+length(mc)
|
||||
-- accm *= exp(-zc)*power(zc,z+0.5) -- Gamma(z+1)</span>
|
||||
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">ez</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(),</span>
|
||||
<span style="color: #000000;">zh</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_add_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mc</span><span style="color: #0000FF;">))</span>
|
||||
<span style="color: #7060A8;">mpfr_neg</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ez</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">mpfr_exp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ez</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ez</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">zh</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zh</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">mpfr_pow</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">zh</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ez</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000080;font-style:italic;">-- return accm/z</span>
|
||||
<span style="color: #7060A8;">mpfr_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">accm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">accm</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">constant</span> <span style="color: #000000;">mPI</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(),</span>
|
||||
<span style="color: #000000;">mqPI</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #7060A8;">mpfr_const_pi</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mPI</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">pistr</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mPI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_sqrt</span><span style="color: #0000FF;">(</span><span style="color: #000000;">mqPI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mPI</span><span style="color: #0000FF;">)</span>
|
||||
|
||||
<span style="color: #008080;">function</span> <span style="color: #000000;">makempfr</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">res</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #004080;">string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'/'</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">])</span>
|
||||
<span style="color: #7060A8;">mpfr_div_si</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">to_integer</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]))</span>
|
||||
<span style="color: #008080;">elsif</span> <span style="color: #004080;">sequence</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #0000FF;">{</span><span style="color: #004080;">mpfr</span> <span style="color: #000000;">x1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">x2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">x</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x1</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">if</span> <span style="color: #004080;">string</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
|
||||
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_div</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #7060A8;">mpfr_div_d</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">else</span>
|
||||
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
|
||||
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
|
||||
|
||||
<span style="color: #008080;">procedure</span> <span style="color: #000000;">mq</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">zm</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">mpfr</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">mul</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">zm</span><span style="color: #0000FF;">,</span><span style="color: #000000;">makempfr</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">mpfr_spouge_gamma</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">maxlen</span><span style="color: #0000FF;">:=</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">e</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init</span><span style="color: #0000FF;">()</span>
|
||||
<span style="color: #7060A8;">mpfr_gamma</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">z</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">mpfr</span> <span style="color: #000000;">p</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_init_set</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">mul</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">mpfr_mul</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">p</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #004080;">string</span> <span style="color: #000000;">zs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">z</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">es</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">e</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #000000;">maxlen</span><span style="color: #0000FF;">:=</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">),</span>
|
||||
<span style="color: #000000;">ps</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">mpfr_get_fixed</span><span style="color: #0000FF;">(</span><span style="color: #000000;">p</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">zs</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">es</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ps</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
|
||||
|
||||
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" z %s %s %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">pad</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" spouge "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"BOTH"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'-'</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">pad</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" expected "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dp</span><span style="color: #0000FF;">+</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"BOTH"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'-'</span><span style="color: #0000FF;">),</span><span style="color: #000000;">pistr</span><span style="color: #0000FF;">})</span>
|
||||
<span style="color: #7060A8;">papply</span><span style="color: #0000FF;">({{-</span><span style="color: #000000;">1.5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0.75</span><span style="color: #0000FF;">},{-</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">0.5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mqPI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}},{</span><span style="color: #000000;">1.5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mqPI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">}},{</span><span style="color: #000000;">2.5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"4/3"</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mqPI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}},{</span><span style="color: #000000;">3.5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"8/15"</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mqPI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">6</span><span style="color: #0000FF;">}}},</span><span style="color: #000000;">mq</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">mq</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"1/1000"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mqPI</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"999.4237724845954661149822012996"</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">28</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">mq</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"1/100"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mqPI</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"99.43258511915060371353298887051"</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">29</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">mq</span><span style="color: #0000FF;">({</span><span style="color: #008000;">"1/10"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mqPI</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"9.513507698668731836292487177265"</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">30</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">mq</span><span style="color: #0000FF;">({</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mqPI</span><span style="color: #0000FF;">,</span><span style="color: #000000;">362880</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">25</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">mq</span><span style="color: #0000FF;">({</span><span style="color: #000000;">100</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mqPI</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"9.332621544394415268169923885627e155"</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
|
||||
<span style="color: #000000;">mq</span><span style="color: #0000FF;">({</span><span style="color: #000000;">150</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">mqPI</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"3.80892263763056972698595524350735e260"</span><span style="color: #0000FF;">}},</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
|
||||
<!--
|
||||
41
Task/Gamma-function/Phixmonti/gamma-function.phixmonti
Normal file
41
Task/Gamma-function/Phixmonti/gamma-function.phixmonti
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
0.577215664901 var gamma
|
||||
-0.65587807152056 var coeff
|
||||
-0.042002635033944 var quad
|
||||
0.16653861138228 var qui
|
||||
-0.042197734555571 var theSet
|
||||
|
||||
def recigamma var z /# n -- n #/
|
||||
z 6 power theSet *
|
||||
z 5 power qui *
|
||||
z 4 power quad *
|
||||
z 3 power coeff *
|
||||
z 2 power gamma *
|
||||
z + + + + +
|
||||
enddef
|
||||
|
||||
/# without var
|
||||
def recigamma
|
||||
dup 6 power theSet * swap
|
||||
dup 5 power qui * swap
|
||||
dup 4 power quad * swap
|
||||
dup 3 power coeff * swap
|
||||
dup 2 power gamma * swap
|
||||
+ + + + +
|
||||
enddef
|
||||
#/
|
||||
|
||||
def gammafunc /# n -- n #/
|
||||
dup 1 == if
|
||||
else
|
||||
dup abs 0.5 <= if
|
||||
recigamma 1 swap /
|
||||
else
|
||||
dup 1 - gammafunc swap 1 - *
|
||||
endif
|
||||
endif
|
||||
enddef
|
||||
|
||||
0.1 2.1 .1 3 tolist
|
||||
for
|
||||
dup print " = " print gammafunc print nl
|
||||
endfor
|
||||
20
Task/Gamma-function/PicoLisp/gamma-function.l
Normal file
20
Task/Gamma-function/PicoLisp/gamma-function.l
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
(scl 28)
|
||||
|
||||
(de *A
|
||||
~(flip
|
||||
(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 ) ) )
|
||||
|
||||
(de gamma (X)
|
||||
(let (Y (- X 1.0) Sum (car *A))
|
||||
(for A (cdr *A)
|
||||
(setq Sum (+ A (*/ Sum Y 1.0))) )
|
||||
(*/ 1.0 1.0 Sum) ) )
|
||||
3
Task/Gamma-function/PowerShell/gamma-function.psh
Normal file
3
Task/Gamma-function/PowerShell/gamma-function.psh
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Add-Type -Path "C:\Program Files (x86)\Math\MathNet.Numerics.3.12.0\lib\net40\MathNet.Numerics.dll"
|
||||
|
||||
1..20 | ForEach-Object {[MathNet.Numerics.SpecialFunctions]::Gamma($_ / 10)}
|
||||
36
Task/Gamma-function/Prolog/gamma-function.pro
Normal file
36
Task/Gamma-function/Prolog/gamma-function.pro
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
gamma_coefficients(
|
||||
[ 1.00000000000000000000000, 0.57721566490153286060651, -0.65587807152025388107701,
|
||||
-0.04200263503409523552900, 0.16653861138229148950170, -0.04219773455554433674820,
|
||||
-0.00962197152787697356211, 0.00721894324666309954239, -0.00116516759185906511211,
|
||||
-0.00021524167411495097281, 0.00012805028238811618615, -0.00002013485478078823865,
|
||||
-0.00000125049348214267065, 0.00000113302723198169588, -0.00000020563384169776071,
|
||||
0.00000000611609510448141, 0.00000000500200764446922, -0.00000000118127457048702,
|
||||
0.00000000010434267116911, 0.00000000000778226343990, -0.00000000000369680561864,
|
||||
0.00000000000051003702874, -0.00000000000002058326053, -0.00000000000000534812253,
|
||||
0.00000000000000122677862, -0.00000000000000011812593, 0.00000000000000000118669,
|
||||
0.00000000000000000141238, -0.00000000000000000022987, 0.00000000000000000001714
|
||||
]).
|
||||
|
||||
tolerance(1e-17).
|
||||
|
||||
gamma(X, _) :- X =< 0.0, !, fail.
|
||||
gamma(X, Y) :-
|
||||
X < 1.0, small_gamma(X, Y), !.
|
||||
gamma(1, 1) :- !.
|
||||
gamma(1.0, 1) :- !.
|
||||
gamma(X, Y) :-
|
||||
X1 is X - 1,
|
||||
gamma(X1, Y1),
|
||||
Y is X1 * Y1.
|
||||
|
||||
small_gamma(X, Y) :-
|
||||
gamma_coefficients(Cs),
|
||||
recip_gamma(X, 1.0, Cs, 1.0, 0.0, Y0),
|
||||
Y is 1 / Y0.
|
||||
|
||||
recip_gamma(_, _, [], _, Y, Y) :- !.
|
||||
recip_gamma(_, _, [], X0, X1, Y) :- tolerance(Tol), abs(X1 - X0) < Tol, Y = X1, !. % early exit
|
||||
recip_gamma(X, PrevPow, [C|Cs], _, X1, Y) :-
|
||||
Power is PrevPow * X,
|
||||
X2 is X1 + C*Power,
|
||||
recip_gamma(X, Power, Cs, X1, X2, Y).
|
||||
82
Task/Gamma-function/PureBasic/gamma-function-1.basic
Normal file
82
Task/Gamma-function/PureBasic/gamma-function-1.basic
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
Procedure.d Gamma(x.d) ; AKJ 01-May-10
|
||||
; Complete Gamma function for x>0 and x<2 (approx)
|
||||
; Extended outside this range via: Gamma(x+1) = x*Gamma(x)
|
||||
; Based on http://rosettacode.org/wiki/Gamma_function [Ada]
|
||||
Protected Dim A.d(28)
|
||||
A(0) = 1.0
|
||||
A(1) = 0.5772156649015328606
|
||||
A(2) =-0.6558780715202538811
|
||||
A(3) =-0.0420026350340952355
|
||||
A(4) = 0.1665386113822914895
|
||||
A(5) =-0.0421977345555443368 ; was ...33675
|
||||
A(6) =-0.0096219715278769736
|
||||
A(7) = 0.0072189432466630995
|
||||
A(8) =-0.0011651675918590651
|
||||
A(9) =-0.0002152416741149510
|
||||
A(10) = 0.0001280502823881162
|
||||
A(11) =-0.0000201348547807882
|
||||
A(12) =-0.0000012504934821427
|
||||
A(13) = 0.0000011330272319817
|
||||
A(14) =-0.0000002056338416978
|
||||
A(15) = 0.0000000061160951045
|
||||
A(16) = 0.0000000050020076445
|
||||
A(17) =-0.0000000011812745705
|
||||
A(18) = 0.0000000001043426712
|
||||
A(19) = 0.0000000000077822634
|
||||
A(20) =-0.0000000000036968056
|
||||
A(21) = 0.0000000000005100370
|
||||
A(22) =-0.0000000000000205833
|
||||
A(23) =-0.0000000000000053481
|
||||
A(24) = 0.0000000000000012268
|
||||
A(25) =-0.0000000000000001181
|
||||
A(26) = 0.0000000000000000012
|
||||
A(27) = 0.0000000000000000014
|
||||
A(28) =-0.0000000000000000002
|
||||
;A(29) = 0.00000000000000000002
|
||||
Protected y.d, Prod.d, Sum.d, N
|
||||
If x<=0.0: ProcedureReturn 0.0: EndIf ; Error
|
||||
y = x-1.0: Prod = 1.0
|
||||
While y>=1.0
|
||||
Prod*y: y-1.0 ; Recurse using Gamma(x+1) = x*Gamma(x)
|
||||
Wend
|
||||
Sum= A(28)
|
||||
For N = 27 To 0 Step -1
|
||||
Sum*y+A(N)
|
||||
Next N
|
||||
If Sum=0.0: ProcedureReturn Infinity(): EndIf
|
||||
ProcedureReturn Prod / Sum
|
||||
EndProcedure
|
||||
|
||||
Procedure.d GammLn(x.d) ; AKJ 01-May-10
|
||||
; Returns Ln(Gamma()) or 0 on error
|
||||
; Based on Numerical Recipes gamma.h
|
||||
Protected j, tmp.d, y.d, ser.d
|
||||
Protected Dim cof.d(13)
|
||||
cof(0) = 57.1562356658629235
|
||||
cof(1) = -59.5979603554754912
|
||||
cof(2) = 14.1360979747417471
|
||||
cof(3) = -0.491913816097620199
|
||||
cof(4) = 0.339946499848118887e-4
|
||||
cof(5) = 0.465236289270485756e-4
|
||||
cof(6) = -0.983744753048795646e-4
|
||||
cof(7) = 0.158088703224912494e-3
|
||||
cof(8) = -0.210264441724104883e-3
|
||||
cof(9) = 0.217439618115212643e-3
|
||||
cof(10) = -0.164318106536763890e-3
|
||||
cof(11) = 0.844182239838527433e-4
|
||||
cof(12) = -0.261908384015814087e-4
|
||||
cof(13) = 0.368991826595316234e-5
|
||||
If x<=0: ProcedureReturn 0: EndIf ; Bad argument
|
||||
y = x
|
||||
tmp = x+5.2421875
|
||||
tmp = (x+0.5)*Log(tmp)-tmp
|
||||
ser = 0.999999999999997092
|
||||
For j=0 To 13
|
||||
y + 1: ser + cof(j)/y
|
||||
Next j
|
||||
ProcedureReturn tmp+Log(2.5066282746310005*ser/x)
|
||||
EndProcedure
|
||||
|
||||
Procedure Factorial(x) ; AKJ 01-May-10
|
||||
ProcedureReturn Gamma(x+1)
|
||||
EndProcedure
|
||||
8
Task/Gamma-function/PureBasic/gamma-function-2.basic
Normal file
8
Task/Gamma-function/PureBasic/gamma-function-2.basic
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
Debug "Gamma()"
|
||||
For i = 12 To 15
|
||||
Debug StrD(i/3.0, 3)+" "+StrD(Gamma(i/3.0))
|
||||
Next i
|
||||
Debug ""
|
||||
Debug "Ln(Gamma(5.0)) = "+StrD(GammLn(5.0), 16) ; Ln(24)
|
||||
Debug ""
|
||||
Debug "Factorial 6 = "+StrD(Factorial(6), 0) ; 72
|
||||
22
Task/Gamma-function/Python/gamma-function-1.py
Normal file
22
Task/Gamma-function/Python/gamma-function-1.py
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
_a = ( 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
|
||||
)
|
||||
def gamma (x):
|
||||
y = float(x) - 1.0;
|
||||
sm = _a[-1];
|
||||
for an in _a[-2::-1]:
|
||||
sm = sm * y + an;
|
||||
return 1.0 / sm;
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
for i in range(1,11):
|
||||
print " %20.14e" % gamma(i/3.0)
|
||||
82
Task/Gamma-function/Python/gamma-function-2.py
Normal file
82
Task/Gamma-function/Python/gamma-function-2.py
Normal file
|
|
@ -0,0 +1,82 @@
|
|||
'''Gamma function'''
|
||||
|
||||
from functools import reduce
|
||||
|
||||
|
||||
# gamma_ :: [Float] -> Float -> Float
|
||||
def gamma_(tbl):
|
||||
'''Gamma function.'''
|
||||
def go(x):
|
||||
y = float(x) - 1.0
|
||||
return 1.0 / reduce(
|
||||
lambda a, x: a * y + x,
|
||||
tbl[-2::-1],
|
||||
tbl[-1]
|
||||
)
|
||||
return lambda x: go(x)
|
||||
|
||||
|
||||
# TBL :: [Float]
|
||||
TBL = [
|
||||
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
|
||||
]
|
||||
|
||||
|
||||
# TEST ----------------------------------------------------
|
||||
# main :: IO()
|
||||
def main():
|
||||
'''Gamma function over a range of values.'''
|
||||
|
||||
gamma = gamma_(TBL)
|
||||
print(
|
||||
fTable(' i -> gamma(i/3):\n')(repr)(lambda x: "%0.7e" % x)(
|
||||
lambda x: gamma(x / 3.0)
|
||||
)(enumFromTo(1)(10))
|
||||
)
|
||||
|
||||
|
||||
# GENERIC -------------------------------------------------
|
||||
|
||||
# enumFromTo :: (Int, Int) -> [Int]
|
||||
def enumFromTo(m):
|
||||
'''Integer enumeration from m to n.'''
|
||||
return lambda n: list(range(m, 1 + n))
|
||||
|
||||
|
||||
# FORMATTING -------------------------------------------------
|
||||
|
||||
# fTable :: String -> (a -> String) ->
|
||||
# (b -> String) -> (a -> b) -> [a] -> String
|
||||
def fTable(s):
|
||||
'''Heading -> x display function -> fx display function ->
|
||||
f -> xs -> tabular string.
|
||||
'''
|
||||
def go(xShow, fxShow, f, xs):
|
||||
ys = [xShow(x) for x in xs]
|
||||
w = max(map(len, ys))
|
||||
return s + '\n' + '\n'.join(map(
|
||||
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
|
||||
xs, ys
|
||||
))
|
||||
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
|
||||
xShow, fxShow, f, xs
|
||||
)
|
||||
|
||||
|
||||
# MAIN ---
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
53
Task/Gamma-function/R/gamma-function.r
Normal file
53
Task/Gamma-function/R/gamma-function.r
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
stirling <- function(z) sqrt(2*pi/z) * (exp(-1)*z)^z
|
||||
|
||||
nemes <- function(z) sqrt(2*pi/z) * (exp(-1)*(z + (12*z - (10*z)^-1)^-1))^z
|
||||
|
||||
lanczos <- function(z)
|
||||
{
|
||||
if(length(z) > 1)
|
||||
{
|
||||
sapply(z, lanczos)
|
||||
} else
|
||||
{
|
||||
g <- 7
|
||||
p <- c(0.99999999999980993, 676.5203681218851, -1259.1392167224028,
|
||||
771.32342877765313, -176.61502916214059, 12.507343278686905,
|
||||
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7)
|
||||
z <- as.complex(z)
|
||||
if(Re(z) < 0.5)
|
||||
{
|
||||
pi / (sin(pi*z) * lanczos(1-z))
|
||||
} else
|
||||
{
|
||||
z <- z - 1
|
||||
x <- p[1]
|
||||
for (i in 1:8) {
|
||||
x <- x+p[i+1]/(z+i)
|
||||
}
|
||||
tt <- z + g + 0.5
|
||||
sqrt(2*pi) * tt^(z+0.5) * exp(-tt) * x
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
spouge <- function(z, a=49)
|
||||
{
|
||||
if(length(z) > 1)
|
||||
{
|
||||
sapply(z, spouge)
|
||||
} else
|
||||
{
|
||||
z <- z-1
|
||||
k <- seq.int(1, a-1)
|
||||
ck <- rep(c(1, -1), len=a-1) / factorial(k-1) * (a-k)^(k-0.5) * exp(a-k)
|
||||
(z + a)^(z+0.5) * exp(-z - a) * (sqrt(2*pi) + sum(ck/(z+k)))
|
||||
}
|
||||
}
|
||||
|
||||
# Checks
|
||||
z <- (1:10)/3
|
||||
all.equal(gamma(z), stirling(z)) # Mean relative difference: 0.07181942
|
||||
all.equal(gamma(z), nemes(z)) # Mean relative difference: 0.003460549
|
||||
all.equal(as.complex(gamma(z)), lanczos(z)) # TRUE
|
||||
all.equal(gamma(z), spouge(z)) # TRUE
|
||||
data.frame(z=z, stirling=stirling(z), nemes=nemes(z), lanczos=lanczos(z), spouge=spouge(z), builtin=gamma(z))
|
||||
68
Task/Gamma-function/REXX/gamma-function-1.rexx
Normal file
68
Task/Gamma-function/REXX/gamma-function-1.rexx
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
/*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 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.*/
|
||||
end /*j*/ /* [↑] default LO is one; HI is nine.*/
|
||||
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. */
|
||||
#.2 = 0.57721566490153286060651209008240243104215933593992359880576723488486772677766467
|
||||
#.3 = -0.65587807152025388107701951514539048127976638047858434729236244568387083835372210
|
||||
#.4 = -0.04200263503409523552900393487542981871139450040110609352206581297618009687597599
|
||||
#.5 = 0.16653861138229148950170079510210523571778150224717434057046890317899386605647425
|
||||
#.6 = -0.04219773455554433674820830128918739130165268418982248637691887327545901118558900
|
||||
#.7 = -0.00962197152787697356211492167234819897536294225211300210513886262731167351446074
|
||||
#.8 = 0.00721894324666309954239501034044657270990480088023831800109478117362259497415854
|
||||
#.9 = -0.00116516759185906511211397108401838866680933379538405744340750527562002584816653
|
||||
#.10 = -0.00021524167411495097281572996305364780647824192337833875035026748908563946371678
|
||||
#.11 = 0.00012805028238811618615319862632816432339489209969367721490054583804120355204347
|
||||
#.12 = -0.00002013485478078823865568939142102181838229483329797911526116267090822918618897
|
||||
#.13 = -0.00000125049348214267065734535947383309224232265562115395981534992315749121245561
|
||||
#.14 = 0.00000113302723198169588237412962033074494332400483862107565429550539546040842730
|
||||
#.15 = -0.00000020563384169776071034501541300205728365125790262933794534683172533245680371
|
||||
#.16 = 0.00000000611609510448141581786249868285534286727586571971232086732402927723507435
|
||||
#.17 = 0.00000000500200764446922293005566504805999130304461274249448171895337887737472132
|
||||
#.18 = -0.00000000118127457048702014458812656543650557773875950493258759096189263169643391
|
||||
#.19 = 0.00000000010434267116911005104915403323122501914007098231258121210871073927347588
|
||||
#.20 = 0.00000000000778226343990507125404993731136077722606808618139293881943550732692987
|
||||
#.21 = -0.00000000000369680561864220570818781587808576623657096345136099513648454655443000
|
||||
#.22 = 0.00000000000051003702874544759790154813228632318027268860697076321173501048565735
|
||||
#.23 = -0.00000000000002058326053566506783222429544855237419746091080810147188058196444349
|
||||
#.24 = -0.00000000000000534812253942301798237001731872793994898971547812068211168095493211
|
||||
#.25 = 0.00000000000000122677862823826079015889384662242242816545575045632136601135999606
|
||||
#.26 = -0.00000000000000011812593016974587695137645868422978312115572918048478798375081233
|
||||
#.27 = 0.00000000000000000118669225475160033257977724292867407108849407966482711074006109
|
||||
#.28 = 0.00000000000000000141238065531803178155580394756670903708635075033452562564122263
|
||||
#.29 = -0.00000000000000000022987456844353702065924785806336992602845059314190367014889830
|
||||
#.30 = 0.00000000000000000001714406321927337433383963370267257066812656062517433174649858
|
||||
#.31 = 0.00000000000000000000013373517304936931148647813951222680228750594717618947898583
|
||||
#.32 = -0.00000000000000000000020542335517666727893250253513557337960820379352387364127301
|
||||
#.33 = 0.00000000000000000000002736030048607999844831509904330982014865311695836363370165
|
||||
#.34 = -0.00000000000000000000000173235644591051663905742845156477979906974910879499841377
|
||||
#.35 = -0.00000000000000000000000002360619024499287287343450735427531007926413552145370486
|
||||
#.36 = 0.00000000000000000000000001864982941717294430718413161878666898945868429073668232
|
||||
#.37 = -0.00000000000000000000000000221809562420719720439971691362686037973177950067567580
|
||||
#.38 = 0.00000000000000000000000000012977819749479936688244144863305941656194998646391332
|
||||
#.39 = 0.00000000000000000000000000000118069747496652840622274541550997151855968463784158
|
||||
#.40 = -0.00000000000000000000000000000112458434927708809029365467426143951211941179558301
|
||||
#.41 = 0.00000000000000000000000000000012770851751408662039902066777511246477487720656005
|
||||
#.42 = -0.00000000000000000000000000000000739145116961514082346128933010855282371056899245
|
||||
#.43 = 0.00000000000000000000000000000000001134750257554215760954165259469306393008612196
|
||||
#.44 = 0.00000000000000000000000000000000004639134641058722029944804907952228463057968680
|
||||
#.45 = -0.00000000000000000000000000000000000534733681843919887507741819670989332090488591
|
||||
#.46 = 0.00000000000000000000000000000000000032079959236133526228612372790827943910901464
|
||||
#.47 = -0.00000000000000000000000000000000000000444582973655075688210159035212464363740144
|
||||
#.48 = -0.00000000000000000000000000000000000000131117451888198871290105849438992219023663
|
||||
#.49 = 0.00000000000000000000000000000000000000016470333525438138868182593279063941453996
|
||||
#.50 = -0.00000000000000000000000000000000000000001056233178503581218600561071538285049997
|
||||
#.51 = 0.00000000000000000000000000000000000000000026784429826430494783549630718908519485
|
||||
#.52 = 0.00000000000000000000000000000000000000000002424715494851782689673032938370921241
|
||||
#=52; do k=# by -1 for #
|
||||
sum=sum*xm + #.k
|
||||
end /*k*/
|
||||
return 1/sum
|
||||
69
Task/Gamma-function/REXX/gamma-function-2.rexx
Normal file
69
Task/Gamma-function/REXX/gamma-function-2.rexx
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/*REXX program calculates the gamma function using Spouge's approximation with 87 digits*/
|
||||
e=2.71828182845904523536028747135266249775724709369995957496696762772407663035354759457138
|
||||
numeric digits length(e) - length(.) /*use the number of decimal digits in E*/
|
||||
c.= 0
|
||||
# = 40 /*#: the number of steps in GAMMA func*/
|
||||
call sq gamma(-3/2), 3/4
|
||||
call sq gamma(-1/2), -1/2
|
||||
call sq gamma( 1/2), 1
|
||||
call si gamma( 1 )
|
||||
call sq gamma( 3/2), 2
|
||||
call si gamma( 2 )
|
||||
call sq gamma( 5/2), 4/3
|
||||
call si gamma( 3 )
|
||||
call sq gamma( 7/2), 8/15
|
||||
call si gamma( 4 )
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
gamma: procedure expose c. e #; parse arg z; #p= # + 1
|
||||
accm = c.1
|
||||
if accm==0 then do; accm= sqrt( 2*pi() )
|
||||
c.1 = accm
|
||||
kfact = 1
|
||||
do k=2 to #
|
||||
c.k= exp(#p-k) * pow(#p-k, k-1.5) / kfact
|
||||
kfact = kfact * -(k-1)
|
||||
end /*k*/
|
||||
end
|
||||
|
||||
do j=2 to #; accm = accm + c.j / (z+j-1)
|
||||
end /*k*/
|
||||
|
||||
return (accm * exp(-(z+#)) * pow(z+#, z+0.5) ) / z
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
pi: return 3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348
|
||||
fmt: parse arg n,p,a; _= format(n,p,a); L= length(_); return left( strip0(_), L)
|
||||
isInt: return datatype(arg(1), 'W') /*is the argument an integer? */
|
||||
sq: procedure expose #; parse arg x,mu; say fmt(x,9,#) fmt((x*mu)**2,9,#); return
|
||||
si: procedure expose #; parse arg x; say fmt(x,9,#); return
|
||||
strip0: procedure; arg _; if pos(., _)\==0 then _= strip(strip(_,'T',0),'T',.); return _
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
exp: procedure expose e; arg x; ix= x%1; if abs(x-ix)>.5 then ix=ix+sign(x); x= x-ix; z=1
|
||||
_=1; w=1; do j=1; _= _*x/j; z= (z+_)/1; if z==w then leave; w=z
|
||||
end /*j*/; if z\==0 then z= e**ix * z; return z
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
ln: procedure; parse arg x; call e; ig= x>1.5; is= 1-2*(ig\==1); ii= 0; xx= x
|
||||
do while ig & xx>1.5 | \ig & xx<.5; _=e
|
||||
do k=-1; iz=xx*_**-is; if k>=0&(ig&iz<1|\ig&iz>.5) then leave; _=_*_; izz=iz; end
|
||||
xx= izz; ii= ii+is*2**k; end /*while*/; x= x*e**-ii-1; z=0; _= -1; p=z
|
||||
do k=1; _=-_*x; z=z+_/k; if z=p then leave; p=z; end; /*k*/; return z+ii
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
pow: procedure; parse arg x,y; if y=0 then return 1; if x=0 then return 0
|
||||
if isInt(y) then return x**y; if isInt(1/y) then return root(x, 1/y)
|
||||
if abs(y//1)=.5 then return sqrt(x)**sign(y)*x**(y%1); return exp( y*ln(x) )
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
root: procedure; parse arg x 1 ox,y 1 oy; if x=0 | y=1 then return x/1
|
||||
if \isInt(y) then return $pow(x, 1/y)
|
||||
if y==2 then return sqrt(x); if y==-2 then return 1/sqrt(x); return rooti(x,y)/1
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
rooti: x=abs(x); y=abs(y); a= digits() + 5; m= y-1; d= 5
|
||||
parse value format(x,2,1,,0) 'E0' with ? 'E' _ .; g= (?/y'E'_ % y) + (x>1)
|
||||
do until d==a; d=min(d+d, a); numeric digits d; o=0
|
||||
do until o=g; o=g; g= format((m*g**y+x)/y/g**m,,d-2); end; end
|
||||
_= g*sign(ox); if oy<0 then _= 1/_; return _
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
sqrt: procedure; parse arg x; if x=0 then return 0; d=digits(); numeric digits; h=d+6
|
||||
numeric form; m.=9; parse value format(x,2,1,,0) 'E0' with g "E" _ .; g=g *.5'e'_ %2
|
||||
do j=0 while h>9; m.j=h; h=h%2+1; end /*j*/
|
||||
do k=j+5 to 0 by -1; numeric digits m.k; g=(g+x/g)*.5; end /*k*/
|
||||
numeric digits d; return g/1
|
||||
29
Task/Gamma-function/Racket/gamma-function.rkt
Normal file
29
Task/Gamma-function/Racket/gamma-function.rkt
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#lang racket
|
||||
(define (gamma number)
|
||||
(if (> 1/2 number)
|
||||
(/ pi (* (sin (* pi number))
|
||||
(gamma (- 1.0 number))))
|
||||
(let ((n (sub1 number))
|
||||
(c '(0.99999999999980993 676.5203681218851 -1259.1392167224028
|
||||
771.32342877765313 -176.61502916214059 12.507343278686905
|
||||
-0.13857109526572012 9.9843695780195716e-6 1.5056327351493116e-7)))
|
||||
(* (sqrt (* pi 2))
|
||||
(expt (+ n 7 0.5) (+ n 0.5))
|
||||
(exp (- (+ n 7 0.5)))
|
||||
(+ (car c)
|
||||
(apply +
|
||||
(for/list ((i (in-range (length (cdr c)))) (x (in-list (cdr c))))
|
||||
(/ x (+ 1 n i)))))))))
|
||||
|
||||
(map gamma '(0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0))
|
||||
;->
|
||||
;'(9.513507698668736
|
||||
; 4.590843711998802
|
||||
; 2.9915689876875904
|
||||
; 2.218159543757687
|
||||
; 1.7724538509055159
|
||||
; 1.489192248812818
|
||||
; 1.2980553326475577
|
||||
; 1.1642297137253037
|
||||
; 1.068628702119319
|
||||
; 1.0)
|
||||
22
Task/Gamma-function/Raku/gamma-function.raku
Normal file
22
Task/Gamma-function/Raku/gamma-function.raku
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
sub Γ(\z) {
|
||||
constant g = 9;
|
||||
z < .5 ?? pi/ sin(pi * z) / Γ(1 - z) !!
|
||||
sqrt(2*pi) *
|
||||
(z + g - 1/2)**(z - 1/2) *
|
||||
exp(-(z + g - 1/2)) *
|
||||
[+] <
|
||||
1.000000000000000174663
|
||||
5716.400188274341379136
|
||||
-14815.30426768413909044
|
||||
14291.49277657478554025
|
||||
-6348.160217641458813289
|
||||
1301.608286058321874105
|
||||
-108.1767053514369634679
|
||||
2.605696505611755827729
|
||||
-0.7423452510201416151527e-2
|
||||
0.5384136432509564062961e-7
|
||||
-0.4023533141268236372067e-8
|
||||
> Z* 1, |map 1/(z + *), 0..*
|
||||
}
|
||||
|
||||
say Γ($_) for 1/3, 2/3 ... 10/3;
|
||||
18
Task/Gamma-function/Ring/gamma-function.ring
Normal file
18
Task/Gamma-function/Ring/gamma-function.ring
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
decimals(3)
|
||||
gamma = 0.577
|
||||
coeff = -0.655
|
||||
quad = -0.042
|
||||
qui = 0.166
|
||||
set = -0.042
|
||||
|
||||
for i=1 to 10
|
||||
see gammafunc(i / 3.0) + nl
|
||||
next
|
||||
|
||||
func recigamma z
|
||||
return z + gamma * pow(z,2) + coeff * pow(z,3) + quad * pow(z,4) + qui * pow(z,5) + set * pow(z,6)
|
||||
|
||||
func gammafunc z
|
||||
if z = 1 return 1
|
||||
but fabs(z) <= 0.5 return 1 / recigamma(z)
|
||||
else return (z - 1) * gammafunc(z-1) ok
|
||||
17
Task/Gamma-function/Ruby/gamma-function-1.rb
Normal file
17
Task/Gamma-function/Ruby/gamma-function-1.rb
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
$a = [ 1.00000_00000_00000_00000, 0.57721_56649_01532_86061, -0.65587_80715_20253_88108,
|
||||
-0.04200_26350_34095_23553, 0.16653_86113_82291_48950, -0.04219_77345_55544_33675,
|
||||
-0.00962_19715_27876_97356, 0.00721_89432_46663_09954, -0.00116_51675_91859_06511,
|
||||
-0.00021_52416_74114_95097, 0.00012_80502_82388_11619, -0.00002_01348_54780_78824,
|
||||
-0.00000_12504_93482_14267, 0.00000_11330_27231_98170, -0.00000_02056_33841_69776,
|
||||
0.00000_00061_16095_10448, 0.00000_00050_02007_64447, -0.00000_00011_81274_57049,
|
||||
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 ]
|
||||
|
||||
def gamma(x)
|
||||
y = Float(x) - 1
|
||||
1.0 / $a.reverse.inject {|sum, an| sum * y + an}
|
||||
end
|
||||
|
||||
(1..10).each {|i| puts format("%.14e", gamma(i/3.0))}
|
||||
1
Task/Gamma-function/Ruby/gamma-function-2.rb
Normal file
1
Task/Gamma-function/Ruby/gamma-function-2.rb
Normal file
|
|
@ -0,0 +1 @@
|
|||
(1..10).each{|i| puts Math.gamma(i/3.0)}
|
||||
26
Task/Gamma-function/Scala/gamma-function.scala
Normal file
26
Task/Gamma-function/Scala/gamma-function.scala
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import java.util.Locale._
|
||||
|
||||
object Gamma {
|
||||
def stGamma(x:Double):Double=math.sqrt(2*math.Pi/x)*math.pow((x/math.E), x)
|
||||
|
||||
def laGamma(x:Double):Double={
|
||||
val p=Seq(676.5203681218851, -1259.1392167224028, 771.32342877765313,
|
||||
-176.61502916214059, 12.507343278686905, -0.13857109526572012,
|
||||
9.9843695780195716e-6, 1.5056327351493116e-7)
|
||||
|
||||
if(x < 0.5) {
|
||||
math.Pi/(math.sin(math.Pi*x)*laGamma(1-x))
|
||||
} else {
|
||||
val x2=x-1
|
||||
val t=x2+7+0.5
|
||||
val a=p.zipWithIndex.foldLeft(0.99999999999980993)((r,v) => r+v._1/(x2+v._2+1))
|
||||
math.sqrt(2*math.Pi)*math.pow(t, x2+0.5)*math.exp(-t)*a
|
||||
}
|
||||
}
|
||||
|
||||
def main(args: Array[String]): Unit = {
|
||||
println("Gamma Stirling Lanczos")
|
||||
for(x <- 0.1 to 2.0 by 0.1)
|
||||
println("%.1f -> %.16f %.16f".formatLocal(ENGLISH, x, stGamma(x), laGamma(x)))
|
||||
}
|
||||
}
|
||||
62
Task/Gamma-function/Scheme/gamma-function.ss
Normal file
62
Task/Gamma-function/Scheme/gamma-function.ss
Normal 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")))
|
||||
22
Task/Gamma-function/Scilab/gamma-function.scilab
Normal file
22
Task/Gamma-function/Scilab/gamma-function.scilab
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
function x=gammal(z) // Lanczos'
|
||||
lz=[ 1.000000000190015 ..
|
||||
76.18009172947146 -86.50532032941677 24.01409824083091 ..
|
||||
-1.231739572450155 1.208650973866179e-3 -5.395239384953129e-6 ]
|
||||
if z < 0.5 then
|
||||
x=%pi/sin(%pi*z)-gammal(1-z)
|
||||
else
|
||||
z=z-1.0
|
||||
b=z+5.5
|
||||
a=lz(1)
|
||||
for i=1:6
|
||||
a=a+(lz(i+1)/(z+i))
|
||||
end
|
||||
x=exp((log(sqrt(2*%pi))+log(a)-b)+log(b)*(z+0.5))
|
||||
end
|
||||
endfunction
|
||||
|
||||
printf("%4s %-9s %-9s\n","x","gamma(x)","gammal(x)")
|
||||
for i=1:30
|
||||
x=i/10
|
||||
printf("%4.1f %9f %9f\n",x,gamma(x),gammal(x))
|
||||
end
|
||||
42
Task/Gamma-function/Seed7/gamma-function.seed7
Normal file
42
Task/Gamma-function/Seed7/gamma-function.seed7
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
$ include "seed7_05.s7i";
|
||||
include "float.s7i";
|
||||
|
||||
const func float: gamma (in float: X) is func
|
||||
result
|
||||
var float: result is 0.0;
|
||||
local
|
||||
const array float: A is [] (
|
||||
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);
|
||||
var float: Y is 0.0;
|
||||
var float: Sum is A[maxIdx(A)];
|
||||
var integer: N is 0;
|
||||
begin
|
||||
Y := X - 1.0;
|
||||
for N range pred(maxIdx(A)) downto minIdx(A) do
|
||||
Sum := Sum * Y + A[N];
|
||||
end for;
|
||||
result := 1.0 / Sum;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var integer: I is 0;
|
||||
begin
|
||||
for I range 1 to 10 do
|
||||
writeln((gamma(flt(I) / 3.0)) digits 15);
|
||||
end for;
|
||||
end func;
|
||||
19
Task/Gamma-function/Sidef/gamma-function-1.sidef
Normal file
19
Task/Gamma-function/Sidef/gamma-function-1.sidef
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
var a = [ 1.00000_00000_00000_00000, 0.57721_56649_01532_86061, -0.65587_80715_20253_88108,
|
||||
-0.04200_26350_34095_23553, 0.16653_86113_82291_48950, -0.04219_77345_55544_33675,
|
||||
-0.00962_19715_27876_97356, 0.00721_89432_46663_09954, -0.00116_51675_91859_06511,
|
||||
-0.00021_52416_74114_95097, 0.00012_80502_82388_11619, -0.00002_01348_54780_78824,
|
||||
-0.00000_12504_93482_14267, 0.00000_11330_27231_98170, -0.00000_02056_33841_69776,
|
||||
0.00000_00061_16095_10448, 0.00000_00050_02007_64447, -0.00000_00011_81274_57049,
|
||||
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 ]
|
||||
|
||||
func gamma(x) {
|
||||
var y = (x - 1)
|
||||
1 / a.reverse.reduce {|sum, an| sum*y + an}
|
||||
}
|
||||
|
||||
for i in 1..10 {
|
||||
say ("%.14e" % gamma(i/3))
|
||||
}
|
||||
37
Task/Gamma-function/Sidef/gamma-function-2.sidef
Normal file
37
Task/Gamma-function/Sidef/gamma-function-2.sidef
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
func gamma(z) {
|
||||
var epsilon = 0.0000001
|
||||
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 = 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.re : result
|
||||
}
|
||||
|
||||
for i in 1..10 {
|
||||
say ("%.14e" % gamma(i/3))
|
||||
}
|
||||
11
Task/Gamma-function/Sidef/gamma-function-3.sidef
Normal file
11
Task/Gamma-function/Sidef/gamma-function-3.sidef
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
define ℯ = Num.e
|
||||
define τ = Num.tau
|
||||
|
||||
func Γ(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))
|
||||
}
|
||||
61
Task/Gamma-function/Stata/gamma-function-1.stata
Normal file
61
Task/Gamma-function/Stata/gamma-function-1.stata
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
mata
|
||||
_gamma_coef = 1.0,
|
||||
5.772156649015328606065121e-1,
|
||||
-6.558780715202538810770195e-1,
|
||||
-4.200263503409523552900393e-2,
|
||||
1.665386113822914895017008e-1,
|
||||
-4.219773455554433674820830e-2,
|
||||
-9.621971527876973562114922e-3,
|
||||
7.218943246663099542395010e-3,
|
||||
-1.165167591859065112113971e-3,
|
||||
-2.152416741149509728157300e-4,
|
||||
1.280502823881161861531986e-4,
|
||||
-2.013485478078823865568939e-5,
|
||||
-1.250493482142670657345359e-6,
|
||||
1.133027231981695882374130e-6,
|
||||
-2.056338416977607103450154e-7,
|
||||
6.116095104481415817862499e-9,
|
||||
5.002007644469222930055665e-9,
|
||||
-1.181274570487020144588127e-9,
|
||||
1.04342671169110051049154e-10,
|
||||
7.782263439905071254049937e-12,
|
||||
-3.696805618642205708187816e-12,
|
||||
5.100370287454475979015481e-13,
|
||||
-2.05832605356650678322243e-14,
|
||||
-5.348122539423017982370017e-15,
|
||||
1.226778628238260790158894e-15,
|
||||
-1.181259301697458769513765e-16,
|
||||
1.186692254751600332579777e-18,
|
||||
1.412380655318031781555804e-18,
|
||||
-2.298745684435370206592479e-19,
|
||||
1.714406321927337433383963e-20
|
||||
|
||||
function gamma_(x_) {
|
||||
external _gamma_coef
|
||||
x = x_
|
||||
y = 1
|
||||
while (x<0.5) y = y/x++
|
||||
while (x>1.5) y = --x*y
|
||||
z = _gamma_coef[30]
|
||||
x--
|
||||
for (i=29; i>=1; i--) z = z*x+_gamma_coef[i]
|
||||
return(y/z)
|
||||
}
|
||||
|
||||
function map(f,a) {
|
||||
n = rows(a)
|
||||
p = cols(a)
|
||||
b = J(n,p,.)
|
||||
for (i=1; i<=n; i++) {
|
||||
for (j=1; j<=p; j++) {
|
||||
b[i,j] = (*f)(a[i,j])
|
||||
}
|
||||
}
|
||||
return(b)
|
||||
}
|
||||
|
||||
x=(1::1000)/100
|
||||
u=map(&gamma(),x)
|
||||
v=map(&gamma_(),x)
|
||||
max(abs((v-u):/u))
|
||||
end
|
||||
31
Task/Gamma-function/Stata/gamma-function-2.stata
Normal file
31
Task/Gamma-function/Stata/gamma-function-2.stata
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
from mpmath import mp
|
||||
|
||||
mp.dps = 50
|
||||
|
||||
def gamma_coef(n):
|
||||
a = [mp.mpf(1), mp.mpf(mp.euler)]
|
||||
for k in range(3, n + 1):
|
||||
s = sum((-1)**j * mp.zeta(j) * a[k - j - 1] for j in range(2, k))
|
||||
a.append((s - a[1] * a[k - 2]) / (1 - k * a[0]))
|
||||
return a
|
||||
|
||||
def horner(a, x):
|
||||
y = 0
|
||||
for s in reversed(a):
|
||||
y = y * x + s
|
||||
return y
|
||||
|
||||
gc = gamma_coef(30)
|
||||
|
||||
def gamma_approx(x):
|
||||
y = mp.mpf(1)
|
||||
while x < 0.5:
|
||||
y /= x
|
||||
x += 1
|
||||
while x > 1.5:
|
||||
x -= 1
|
||||
y *= x
|
||||
return y / horner(gc, x - 1)
|
||||
|
||||
for x in gc:
|
||||
print(mp.nstr(x, 25))
|
||||
6
Task/Gamma-function/TI-83-BASIC/gamma-function-1.basic
Normal file
6
Task/Gamma-function/TI-83-BASIC/gamma-function-1.basic
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
for(I,1,10)
|
||||
I/2→X
|
||||
X^(X-1/2)e^(-X)√(2π)→Y
|
||||
Disp X,(X-1)!,Y
|
||||
Pause
|
||||
End
|
||||
14
Task/Gamma-function/TI-83-BASIC/gamma-function-2.basic
Normal file
14
Task/Gamma-function/TI-83-BASIC/gamma-function-2.basic
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
for(I,1,10)
|
||||
I/2→X
|
||||
e^(ln((1.0
|
||||
+76.18009173/(X+1)
|
||||
-86.50532033/(X+2)
|
||||
+24.01409824/(X+3)
|
||||
-1.231739572/(X+4)
|
||||
+1.208650974E-3/(X+5)
|
||||
-5.395239385E-6/(X+6)
|
||||
)√(2π)/X)
|
||||
+(X+.5)ln(X+5.5)-X-5.5)->Y
|
||||
Disp X,(X-1)!,Y
|
||||
Pause
|
||||
End
|
||||
20
Task/Gamma-function/TXR/gamma-function-1.txr
Normal file
20
Task/Gamma-function/TXR/gamma-function-1.txr
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
(defun gamma (x)
|
||||
(/ (rpoly (- x 1.0)
|
||||
#( 1.00000,00000,00000,00000 0.57721,56649,01532,86061
|
||||
-0.65587,80715,20253,88108 -0.04200,26350,34095,23553
|
||||
0.16653,86113,82291,48950 -0.04219,77345,55544,33675
|
||||
-0.00962,19715,27876,97356 0.00721,89432,46663,09954
|
||||
-0.00116,51675,91859,06511 -0.00021,52416,74114,95097
|
||||
0.00012,80502,82388,11619 -0.00002,01348,54780,78824
|
||||
-0.00000,12504,93482,14267 0.00000,11330,27231,98170
|
||||
-0.00000,02056,33841,69776 0.00000,00061,16095,10448
|
||||
0.00000,00050,02007,64447 -0.00000,00011,81274,57049
|
||||
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))))
|
||||
|
||||
(each ((i 1..11))
|
||||
(put-line (pic "##.######" (gamma (/ i 3.0)))))
|
||||
7
Task/Gamma-function/TXR/gamma-function-2.txr
Normal file
7
Task/Gamma-function/TXR/gamma-function-2.txr
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
(defun gamma (x)
|
||||
(* (sqrt (/ (* 2 %pi%)
|
||||
x))
|
||||
(expt (/ x %e%) x)))
|
||||
|
||||
(each ((i 1..11))
|
||||
(put-line (pic "##.######" (gamma (/ i 3.0)))))
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue