Just another update
This commit is contained in:
parent
a25938f123
commit
00a190b0a6
6591 changed files with 94363 additions and 23227 deletions
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
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
import std.stdio, std.math, std.mathspecial;
|
||||
|
||||
real taylorGamma(in real x) pure nothrow {
|
||||
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,
|
||||
|
|
@ -25,7 +25,7 @@ real taylorGamma(in real x) pure nothrow {
|
|||
return 1.0L / sm;
|
||||
}
|
||||
|
||||
real lanczosGamma(real z) pure nothrow {
|
||||
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;
|
||||
|
|
|
|||
14
Task/Gamma-function/F-Sharp/gamma-function.fs
Normal file
14
Task/Gamma-function/F-Sharp/gamma-function.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 ) )
|
||||
7
Task/Gamma-function/Haskell/gamma-function-2.hs
Normal file
7
Task/Gamma-function/Haskell/gamma-function-2.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
|
||||
19
Task/Gamma-function/JavaScript/gamma-function.js
Normal file
19
Task/Gamma-function/JavaScript/gamma-function.js
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
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;
|
||||
}
|
||||
|
|
@ -1,51 +1,22 @@
|
|||
sub Γ(\z, :$method = 'lanczos') {
|
||||
given $method {
|
||||
when 'lanczos' {
|
||||
constant g = 9;
|
||||
z < .5 ?? pi/ sin(pi * z) / Γ(1 - z, :$method) !!
|
||||
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..*
|
||||
}
|
||||
when 'stirling' {
|
||||
z < 10 ?? Γ(z + 1, :$method) / z !!
|
||||
sqrt(2*pi*z)*(z/e + 1/(12*z*e))**z / z
|
||||
}
|
||||
when 'taylor' {
|
||||
z > 3/2 ?? (z - 1)*Γ(z - 1, :$method) !!
|
||||
z < 1/2 ?? Γ(z + 1, :$method)/z !!
|
||||
1 / <
|
||||
0.00000_00000_00000_00002 -0.00000_00000_00000_00023 0.00000_00000_00000_00141
|
||||
0.00000_00000_00000_00119 -0.00000_00000_00000_11813 0.00000_00000_00001_22678
|
||||
-0.00000_00000_00005_34812 -0.00000_00000_00020_58326 0.00000_00000_00510_03703
|
||||
-0.00000_00000_03696_80562 0.00000_00000_07782_26344 0.00000_00001_04342_67117
|
||||
-0.00000_00011_81274_57049 0.00000_00050_02007_64447 0.00000_00061_16095_10448
|
||||
-0.00000_02056_33841_69776 0.00000_11330_27231_98170 -0.00000_12504_93482_14267
|
||||
-0.00002_01348_54780_78824 0.00012_80502_82388_11619 -0.00021_52416_74114_95097
|
||||
-0.00116_51675_91859_06511 0.00721_89432_46663_09954 -0.00962_19715_27876_97356
|
||||
-0.04219_77345_55544_33675 0.16653_86113_82291_48950 -0.04200_26350_34095_23553
|
||||
-0.65587_80715_20253_88108 0.57721_56649_01532_86061 1.00000_00000_00000_00000
|
||||
>.reduce: * *(z-1) + *
|
||||
}
|
||||
default { die "unknow method '$method'" }
|
||||
}
|
||||
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..*
|
||||
}
|
||||
|
||||
for <lanczos taylor stirling> -> $method {
|
||||
# a bug in fmt forces us to round numbers before displaying them
|
||||
say "{$method.fmt("%10s")}: ",
|
||||
map { Γ($_, :$method).round(1e-12).fmt("%.12f") }, (1/3, 2/3 ... 10/3);
|
||||
}
|
||||
say Γ($_) for 1/3, 2/3 ... 10/3;
|
||||
|
|
|
|||
|
|
@ -3,6 +3,10 @@ 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';
|
||||
|
|
@ -51,10 +55,15 @@ sub Gamma {
|
|||
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(lanczos taylor stirling)) {
|
||||
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";
|
||||
|
|
|
|||
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)}
|
||||
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
|
||||
6
Task/Gamma-function/TI-83-BASIC/gamma-function-1.ti-83
Normal file
6
Task/Gamma-function/TI-83-BASIC/gamma-function-1.ti-83
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.ti-83
Normal file
14
Task/Gamma-function/TI-83-BASIC/gamma-function-2.ti-83
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue