Gamma function: Difference between revisions

Content added Content deleted
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 492: Line 492:
gamma( 70)= 1.711224524e98, 1.711224524281e98, 1.711224524281e98, 7.57303907062e-29, 1.709188578191e98
gamma( 70)= 1.711224524e98, 1.711224524281e98, 1.711224524281e98, 7.57303907062e-29, 1.709188578191e98
</pre>
</pre>

=={{header|ANSI Standard BASIC}}==
=={{header|ANSI Standard BASIC}}==


Line 522: Line 523:
350 END FUNCTION</lang>
350 END FUNCTION</lang>


=={{Header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{AutoHotkey case}}
{{AutoHotkey case}}
Source: [http://www.autohotkey.com/forum/topic44657.html AutoHotkey forum] by Laszlo
Source: [http://www.autohotkey.com/forum/topic44657.html AutoHotkey forum] by Laszlo
Line 607: Line 608:
1.386831185e+080 1.711224524e+098 8.946182131e+116 1.650795516e+136 9.332621544e+155
1.386831185e+080 1.711224524e+098 8.946182131e+116 1.650795516e+136 9.332621544e+155
*/</lang>
*/</lang>

=={{header|AWK}}==
=={{header|AWK}}==
<lang AWK>
<lang AWK>
Line 773: Line 775:
2.70976382 2.77815848 2.77815848 2.77815848
2.70976382 2.77815848 2.77815848 2.77815848
</pre>
</pre>

=={{header|C sharp}}==
=={{header|C sharp}}==
This is just rewritten from the Wikipedia Lanczos article. Works with complex numbers as well as reals.
This is just rewritten from the Wikipedia Lanczos article. Works with complex numbers as well as reals.
Line 1,085: Line 1,088:
3.000000 1.999999999993968
3.000000 1.999999999993968
3.333333 2.778158479338573
3.333333 2.778158479338573
</pre>

=={{header|F Sharp}}==

Solved using the Lanczos Coefficients described in Numerical Recipes (Press et al)

<lang F Sharp>

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 ) )

</lang>

{{out}}
<pre>
0.1 : 9.51350769855015
0.2 : 4.59084371196153
0.3 : 2.99156898767207
0.4 : 2.21815954375051
0.5 : 1.77245385090205
0.6 : 1.48919224881114
0.7 : 1.29805533264677
0.8 : 1.16422971372497
0.9 : 1.06862870211921
1 : 1
1.1 : 0.951350769866919
1.2 : 0.91816874239982
1.3 : 0.897470696306335
1.4 : 0.887263817503124
1.5 : 0.886226925452797
1.6 : 0.893515349287718
1.7 : 0.908638732853309
1.8 : 0.931383770980253
1.9 : 0.961765831907391
2 : 1
10 : 362880.000000085
20 : 1.21645100409886E+17
30 : 8.84176199395902E+30
40 : 2.03978820820436E+46
50 : 6.08281864068541E+62
60 : 1.38683118555266E+80
70 : 1.71122452441801E+98
80 : 8.94618213157899E+116
90 : 1.65079551625067E+136
100 : 9.33262154536104E+155
</pre>
</pre>


Line 1,339: Line 1,399:
1.90 0.920842721894229 0.961765831907388
1.90 0.920842721894229 0.961765831907388
2.00 0.959502175744492 1.000000000000000
2.00 0.959502175744492 1.000000000000000
</pre>

=={{header|F Sharp}}==

Solved using the Lanczos Coefficients described in Numerical Recipes (Press et al)

<lang F Sharp>

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 ) )

</lang>

{{out}}
<pre>
0.1 : 9.51350769855015
0.2 : 4.59084371196153
0.3 : 2.99156898767207
0.4 : 2.21815954375051
0.5 : 1.77245385090205
0.6 : 1.48919224881114
0.7 : 1.29805533264677
0.8 : 1.16422971372497
0.9 : 1.06862870211921
1 : 1
1.1 : 0.951350769866919
1.2 : 0.91816874239982
1.3 : 0.897470696306335
1.4 : 0.887263817503124
1.5 : 0.886226925452797
1.6 : 0.893515349287718
1.7 : 0.908638732853309
1.8 : 0.931383770980253
1.9 : 0.961765831907391
2 : 1
10 : 362880.000000085
20 : 1.21645100409886E+17
30 : 8.84176199395902E+30
40 : 2.03978820820436E+46
50 : 6.08281864068541E+62
60 : 1.38683118555266E+80
70 : 1.71122452441801E+98
80 : 8.94618213157899E+116
90 : 1.65079551625067E+136
100 : 9.33262154536104E+155
</pre>
</pre>


Line 2,050: Line 2,053:
GAMMA(7*I)
GAMMA(7*I)
Matrix(2, 3, [[.2468571430, .2468571430, .2468571430], [.2468571430, .2468571430, .2468571430]])</pre>
Matrix(2, 3, [[.2468571430, .2468571430, .2468571430], [.2468571430, .2468571430, .2468571430]])</pre>

=={{header|Mathematica}}==
=={{header|Mathematica}}==
This code shows the built-in method, which works for any value (positive, negative and complex numbers).
This code shows the built-in method, which works for any value (positive, negative and complex numbers).
Line 2,120: Line 2,124:
gamma_approx(12.3b0) - gamma(12.3b0);
gamma_approx(12.3b0) - gamma(12.3b0);
/* -9.25224705314470500985141176997b-15 */</lang>
/* -9.25224705314470500985141176997b-15 */</lang>

=={{header|МК-61/52}}==
<pre>
П9 9 П0 ИП9 ИП9 1 + * Вx L0
05 1 + П9 ^ ln 1 - * ИП9
1 2 * 1/x + e^x <-> / 2 пи
* ИП9 / КвКор * ^ ВП 3 + Вx
- С/П
</pre>


=={{header|Modula-3}}==
=={{header|Modula-3}}==
Line 2,172: Line 2,185:
1.9999999999939684e+000
1.9999999999939684e+000
2.7781584793385790e+000
2.7781584793385790e+000
</pre>

=={{header|МК-61/52}}==
<pre>
П9 9 П0 ИП9 ИП9 1 + * Вx L0
05 1 + П9 ^ ln 1 - * ИП9
1 2 * 1/x + e^x <-> / 2 пи
* ИП9 / КвКор * ^ ВП 3 + Вx
- С/П
</pre>
</pre>


Line 2,483: Line 2,487:
taylor: 2.678938534708 1.354117939426 1.000000000000 0.892979511569 0.902745292951 1.000000000000 1.190639348759 1.504575488252 2.000000000000 2.778158480438
taylor: 2.678938534708 1.354117939426 1.000000000000 0.892979511569 0.902745292951 1.000000000000 1.190639348759 1.504575488252 2.000000000000 2.778158480438
stirling: 2.678938532866 1.354117938504 0.999999999306 0.892979510955 0.902745292336 0.999999999306 1.190639347940 1.504575487227 1.999999998611 2.778158478527</pre>
stirling: 2.678938532866 1.354117938504 0.999999999306 0.892979510955 0.902745292336 0.999999999306 1.190639347940 1.504575487227 1.999999998611 2.778158478527</pre>

=={{header|Perl 6}}==
<lang perl6>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;</lang>
{{out}}
<pre>2.67893853470775
1.3541179394264
1
0.892979511569248
0.902745292950934
1
1.190639348759
1.50457548825155
2
2.77815848043766</pre>


=={{header|Phix}}==
=={{header|Phix}}==
Line 3,208: Line 3,177:
; 1.068628702119319
; 1.068628702119319
; 1.0)</lang>
; 1.0)</lang>

=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>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;</lang>
{{out}}
<pre>2.67893853470775
1.3541179394264
1
0.892979511569248
0.902745292950934
1
1.190639348759
1.50457548825155
2
2.77815848043766</pre>


=={{header|REXX}}==
=={{header|REXX}}==