Gamma function: Difference between revisions

Added Easylang
m (ANSI Standard BASIC, BASIC256, and BBC BASIC moved to the BASIC section.)
(Added Easylang)
 
(5 intermediate revisions by 5 users not shown)
Line 674:
 
=={{header|BASIC}}==
==={{header|ANSI Standard BASIC}}===
{{trans|BBC BASIC}} - Lanczos method.
{{works with|Decimal BASIC}}
<syntaxhighlight lang="ansi standard basic">100 DECLARE EXTERNAL FUNCTION FNlngamma
<syntaxhighlight lang="basic">100 DECLARE EXTERNAL FUNCTION FNlngamma
110
120 DEF FNgamma(z) = EXP(FNlngamma(z))
Line 702 ⟶ 703:
340 LET FNlngamma = (LOG(SQR(2*PI)) + LOG(a) - b) + LOG(b) * (z+0.5)
350 END FUNCTION</syntaxhighlight>
{{out}}
<pre>
.1 9.513507698670
.2 4.590843712000
.3 2.991568987689
.4 2.218159543760
.5 1.772453850902
.6 1.489192248811
.7 1.298055332647
.8 1.164229713725
.9 1.068628702119
1.0 1.000000000000
1.1 .951350769867
1.2 .918168742400
1.3 .897470696306
1.4 .887263817503
1.5 .886226925453
1.6 .893515349288
1.7 .908638732853
1.8 .931383770980
1.9 .961765831907
2.0 1.000000000000
</pre>
 
==={{header|BASIC256}}===
Line 1,260 ⟶ 1,284:
140,0 9,615723196940235E238
170,0 4,269068009004271E304</pre>
 
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight>
e = 2.718281828459
func stirling x .
return sqrt (2 * pi / x) * pow (x / e) x
.
print " X Stirling"
for i to 20
d = i / 10
numfmt 2 4
write d & " "
numfmt 3 4
print stirling d
.
</syntaxhighlight>
 
 
=={{header|Elixir}}==
Line 2,279 ⟶ 2,321:
<pre>2.220446049250313e-16</pre>
 
=={{header|Koka}}==
Based on OCaml implementation
<syntaxhighlight lang="koka">
import std/num/float64
 
fun gamma-lanczos(x)
val g = 7.0
// Coefficients used by the GNU Scientific Library
val c = [0.99999999999980993, 676.5203681218851, -1259.1392167224028,
771.32342877765313, -176.61502916214059, 12.507343278686905,
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7]
fun ag(z: float64, d: int)
if d == 0 then c[0].unjust + ag(z, 1)
elif d < 8 then c[d].unjust / (z + d.float64) + ag(z, d.inc)
else c[d].unjust / (z + d.float64)
fun gamma(z)
val z' = z - 1.0
val p = z' + g + 0.5
sqrt(2.0 * pi) * pow(p, (z' + 0.5)) * exp(0.0 - p) * ag(z', 0)
gamma(x)
 
val e = exp(1.0)
fun gamma-stirling(x)
sqrt(2.0 * pi / x) * pow(x / e, x)
 
fun gamma-stirling2(x')
// Extended Stirling method seen in Abramowitz and Stegun
val d = [1.0/12.0, 1.0/288.0, -139.0/51840.0, -571.0/2488320.0]
fun corr(z, x, n)
if n < d.length - 1 then d[n].unjust / x + corr(z, x*z, n.inc)
else d[n].unjust / x
fun gamma(z)
gamma-stirling(z)*(1.0 + corr(z, z, 0))
gamma(x')
 
fun mirror(gma, z)
if z > 0.5 then gma(z) else pi / sin(pi * z) / gma(1.0 - z)
 
fun main()
println("z\tLanczos\t\t\tStirling\t\tStirling2")
for(1, 20) fn(i)
val z = i.float64 / 10.0
println(z.show(1) ++ "\t" ++ mirror(gamma-lanczos, z).show ++ "\t" ++
mirror(gamma-stirling, z).show ++ "\t" ++ mirror(gamma-stirling2, z).show)
for(1, 7) fn(i)
val z = 10.0 * i.float64
println(z.show ++ "\t" ++ gamma-lanczos(z).show ++ "\t" ++
gamma-stirling(z).show ++ "\t" ++ gamma-stirling2(z).show)
</syntaxhighlight>
 
{{out}}
 
<pre>
z Lanczos Stirling Stirling2
0.1 9.5135076986687359 10.405084329555955 9.5210418318004439
0.2 4.5908437119988017 5.0739927535371763 4.596862295030256
0.3 2.9915689876875904 3.3503395433773222 2.9984402802949961
0.4 2.218159543757686 2.5270578096699556 2.2277588907113128
0.5 1.7724538509055157 2.0663656770612464 1.7883901437260497
0.6 1.4891922488128184 1.3071588574483559 1.4827753636029286
0.7 1.2980553326475577 1.1590532921139201 1.2950806801024195
0.8 1.1642297137253037 1.0533709684256085 1.1627054102439229
0.9 1.068628702119319 0.97706150787769541 1.0677830813298756
1.0 1.0000000000000002 0.92213700889578909 0.99949946853364036
1.1 0.95135076986687361 0.88348995316870382 0.95103799705518899
1.2 0.91816874239976076 0.85775533539659088 0.91796405783487933
1.3 0.89747069630627774 0.84267825944839203 0.8973312868034562
1.4 0.88726381750307537 0.8367445486370817 0.88716548484542823
1.5 0.88622692545275827 0.83895655252649626 0.88615538430170204
1.6 0.89351534928769061 0.8486932421525738 0.89346184003019224
1.7 0.90863873285329122 0.86562147179388405 0.90859770150945562
1.8 0.93138377098024272 0.8896396352879945 0.93135158986107858
1.9 0.96176583190738729 0.92084272189422933 0.96174006762796482
2.0 1.0000000000000002 0.95950217574449159 0.99997898067003532
10 362880.00000000105 359869.56187410367 362879.99717458693
20 1.2164510040883245e+17 1.2113934233805675e+17 1.2164510037907557e+17
30 8.841761993739658e+30 8.8172365307655063e+30 8.8417619934546387e+30
40 2.0397882081197221e+46 2.0355431612365591e+46 2.0397882081041343e+46
50 6.0828186403425409e+62 6.0726891878763362e+62 6.0828186403274418e+62
60 1.3868311854568534e+80 1.3849063858294502e+80 1.3868311854555093e+80
70 1.7112245242813438e+98 1.7091885781910795e+98 1.711224524280615e+98
</pre>
=={{header|Kotlin}}==
<syntaxhighlight lang="scala">// version 1.0.6
Line 4,234 ⟶ 4,358:
:<math>RecGamma(a) = \frac{1}{\Gamma(a)}</math>;
:<math>Pochhammer(a,x) = \frac{\Gamma(a+x)}{\Gamma(x)}</math>.
 
=={{header|RPL}}==
{{trans|Ada}}
≪ 1 -
{ 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 }
→ y a
≪ a DUP SIZE GET
a SIZE 1 - 1 '''FOR''' n
y * a n GET +
-1 '''STEP'''
INV
≫ ≫ '<span style="color:blue>GAMMA</span>' STO
 
.3 <span style="color:blue>GAMMA</span>
The built-in FACT instruction is obviously based on a similar Taylor formula, since it returns same results:
.3 1 - FACT
{{out}}
<pre>
2: 2.99156898769
1: 2.99156898769
</pre>
 
=={{header|Ruby}}==
Line 4,279 ⟶ 4,433:
2.0
2.7781584804376647
</pre>
 
=={{header|Rust}}==
====Stirling====
{{trans|AWK}}
 
<syntaxhighlight lang="rust">
use std::f64::consts;
 
fn main() {
let gamma = |x: f64| { assert_ne!(x, 0.0); (2.0*consts::PI/x).sqrt() * (x * (x/consts::E).ln()).exp()};
(1..=20).for_each(|x| {
let x = f64::from(x) / 10.0;
println!("{:.02} => {:.10}", x, gamma(x));
});
}
</syntaxhighlight>
 
{{out}}
<pre>
0.10 => 5.6971871490
0.20 => 3.3259984240
0.30 => 2.3625300363
0.40 => 1.8414763359
0.50 => 1.5203469011
0.60 => 1.3071588574
0.70 => 1.1590532921
0.80 => 1.0533709684
0.90 => 0.9770615079
1.00 => 0.9221370089
1.10 => 0.8834899532
1.20 => 0.8577553354
1.30 => 0.8426782594
1.40 => 0.8367445486
1.50 => 0.8389565525
1.60 => 0.8486932422
1.70 => 0.8656214718
1.80 => 0.8896396353
1.90 => 0.9208427219
2.00 => 0.9595021757
</pre>
 
Line 5,235 ⟶ 5,429:
{{libheader|Wren-math}}
The ''gamma'' method in the Math class is based on the Lanczos approximation.
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
import "./math" for Math
 
var stirling = Fn.new { |x| (2 * Num.pi / x).sqrt * (x / Math.e).pow(x) }
Line 5,243 ⟶ 5,437:
for (i in 1..20) {
var d = i / 10
Fmt.print("$4.2f\t$16.14f\t$16.14f", d, stirling.call(d), Math.gamma(d))
System.write("%(Fmt.f(4, d, 2))\t")
System.write("%(Fmt.f(16, stirling.call(d), 14))\t")
System.print("%(Fmt.f(16, Math.gamma(d), 14))")
}</syntaxhighlight>
 
Line 5,254 ⟶ 5,446:
0.10 5.69718714897717 9.51350769866875
0.20 3.32599842402239 4.59084371199881
0.30 2.36253003626962 2.9915689876876099156898768759
0.40 1.84147633593624 2.21815954375769
0.50 1.52034690106628 1.77245385090552
2,041

edits