Download code

Jump to: navigation, search

Back to Gamma_function_with_the_Lanczos_approximation_(Perl)

Download for Windows: single file, zip

Download for UNIX: single file, zip, tar.gz, tar.bz2

gamma_lanczos_test.pl

 1 #!/usr/bin/env perl
 2 
 3 # Pragmas.
 4 use strict;
 5 
 6 # Modules.
 7 use Math::Complex;
 8 
 9 # Global variables.
10 use vars qw($g $lanczos_coef);
11 
12 $g = 7;
13 $lanczos_coef = [
14         0.99999999999980993,
15         676.5203681218851,
16         -1259.1392167224028,
17         771.32342877765313,
18         -176.61502916214059,
19         12.507343278686905,
20         -0.13857109526572012,
21         9.9843695780195716e-6,
22         1.5056327351493116e-7
23 ];
24 
25 sub gamma_lanczos {
26         my $z = shift;
27         $z = Math::Complex->make($z);
28         if (Re($z) < 0.5) {
29                 return pi / (sin(pi * $z) * gamma(1 - $z));
30         } else {
31                 $z -= 1;
32                 my $x = $lanczos_coef->[0];
33                 foreach my $i (1 .. $g + 2) {
34                         $x += $lanczos_coef->[$i] / ($z + $i);
35                 }
36                 my $t = $z + $g + 0.5;
37                 return sqrt(2 * pi) * $t ** ($z + 0.5) * exp(-$t) * $x;
38         }
39 }
40 
41 
42 print gamma_lanczos(10), "\n";


hijacker
hijacker
hijacker
hijacker