Download code

Jump to: navigation, search

Back to Gamma_function_with_the_Lanczos_approximation_(Python)

Download for Windows: single file, zip

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

gamma_lanczos.py

 1 # The authors of this work have released all rights to it and placed it
 2 # in the public domain under the Creative Commons CC0 1.0 waiver
 3 # (http://creativecommons.org/publicdomain/zero/1.0/).
 4 # 
 5 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 6 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 7 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 8 # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 9 # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
10 # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
11 # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
12 # 
13 # Retrieved from: http://en.literateprograms.org/Gamma_function_with_the_Lanczos_approximation_(Python)?oldid=19134
14 
15 from cmath import *
16 
17 g = 7
18 lanczos_coef = [ \
19      0.99999999999980993,
20    676.5203681218851,
21  -1259.1392167224028,
22    771.32342877765313,
23   -176.61502916214059,
24     12.507343278686905,
25     -0.13857109526572012,
26      9.9843695780195716e-6,
27      1.5056327351493116e-7]
28 
29 def gamma(z):
30     z = complex(z)
31     if z.real < 0.5:
32         return pi / (sin(pi*z)*gamma(1-z))
33     else:
34         z -= 1
35         x = lanczos_coef[0]
36         for i in range(1, g+2):
37             x += lanczos_coef[i]/(z+i)
38         t = z + g + 0.5
39         return sqrt(2*pi) * t**(z+0.5) * exp(-t) * x
40 


hijacker
hijacker
hijacker
hijacker