2020-01-08 13:18:17 +00:00
|
|
|
import math
|
2020-07-06 07:44:19 +00:00
|
|
|
|
2020-01-08 13:18:17 +00:00
|
|
|
from numpy import inf
|
2020-07-06 07:44:19 +00:00
|
|
|
from scipy.integrate import quad
|
2020-01-08 13:18:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
def gamma(num: float) -> float:
|
|
|
|
"""
|
|
|
|
https://en.wikipedia.org/wiki/Gamma_function
|
2020-05-22 06:10:11 +00:00
|
|
|
In mathematics, the gamma function is one commonly
|
|
|
|
used extension of the factorial function to complex numbers.
|
2020-06-16 08:09:19 +00:00
|
|
|
The gamma function is defined for all complex numbers except the non-positive
|
|
|
|
integers
|
2020-01-08 13:18:17 +00:00
|
|
|
>>> gamma(-1)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: math domain error
|
|
|
|
>>> gamma(0)
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: math domain error
|
|
|
|
>>> gamma(9)
|
|
|
|
40320.0
|
2020-05-22 06:10:11 +00:00
|
|
|
>>> from math import gamma as math_gamma
|
Fix long line, tests (#2123)
* Fix long line
* updating DIRECTORY.md
* Add doctest
* ...
* ...
* Update tabu_search.py
* space
* Fix doctest
>>> find_neighborhood(['a','c','b','d','e','a']) # doctest: +NORMALIZE_WHITESPACE
[['a','e','b','d','c','a',90], [['a','c','d','b','e','a',90],
['a','d','b','c','e','a',93], ['a','c','b','e','d','a',102],
['a','c','e','d','b','a',113], ['a','b','c','d','e','a',93]]
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: John Law <johnlaw.po@gmail.com>
2020-06-16 12:29:13 +00:00
|
|
|
>>> all(.99999999 < gamma(i) / math_gamma(i) <= 1.000000001
|
|
|
|
... for i in range(1, 50))
|
2020-01-08 13:18:17 +00:00
|
|
|
True
|
|
|
|
>>> gamma(-1)/math_gamma(-1) <= 1.000000001
|
|
|
|
Traceback (most recent call last):
|
|
|
|
...
|
|
|
|
ValueError: math domain error
|
2020-05-22 06:10:11 +00:00
|
|
|
>>> gamma(3.3) - math_gamma(3.3) <= 0.00000001
|
2020-01-08 13:18:17 +00:00
|
|
|
True
|
|
|
|
"""
|
|
|
|
if num <= 0:
|
|
|
|
raise ValueError("math domain error")
|
|
|
|
|
|
|
|
return quad(integrand, 0, inf, args=(num))[0]
|
|
|
|
|
|
|
|
|
|
|
|
def integrand(x: float, z: float) -> float:
|
|
|
|
return math.pow(x, z - 1) * math.exp(-x)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
from doctest import testmod
|
|
|
|
|
|
|
|
testmod()
|