[Python-3000] adding gamma and error functions to math module
nirinA raseliarison
nirina at mail.blueline.mg
Wed Jul 16 18:03:49 CEST 2008
on Tue, 15 Jul 2008 01:39:19 -0700
Raymond Hettinger wrote:
> In the meantime, here's a little cheat:
>
> >>> from test.test_random import gamma
> >>> gamma(4)
> 5.9999999999999982
> >>> gamma(5)
> 24.0
> >>> gamma(6)
> 120.00000000000003
> >>> gamma(6.1)
> 142.45194406567856
there is a simple implementation in Python at:
http://en.wikipedia.org/wiki/Lanczos_approximation
with coefficients slightly different from those in
test_random.py, one gets:
>>> gamma(4)
(6.0000000000000071+0j)
>>> gamma(5)
(23.999999999999996+0j)
>>> gamma(6)
(120.00000000000021+0j)
>>> gamma(6.1)
(142.4519440656791+0j)
>>>
with the gamma from gcc under linux,
which uses the MPFR library,
the result is not really satisfying:
>>> lgamma(4)
1.791759469228055
>>> tgamma(4)
6.0
>>> tgamma(5)
24.000000000000004
>>> tgamma(6)
119.99999999999997
>>> tgamma(6.1)
142.45194406567862
using the code from numerical recipes chap 6:
>>> lgamma(4)
1.7917594692280554
>>> math.exp(lgamma(4))
6.0000000000000027
>>> math.exp(lgamma(5))
23.999999999999993
>>> math.exp(lgamma(6))
119.99999999999987
>>> math.exp(lgamma(6.1))
142.45194406567873
and from the scipy package,
which uses the cephes math library:
>>> from scipy.special import *
>>> gamma(4)
6.0
>>> gamma(5)
24.0
>>> gamma(6)
120.0
>>> gamma(6.1)
142.45194406567867
i'll check if an appropriate choice of
of a set of coefficients or extending the
number of coefficients can lead to a more
accurate result.
nirinA
--
More information about the Python-3000
mailing list