Calculates the value of Gamma function of num where num is either an integer (1, 2, 3..) or a half-integer (0.5, 1.5, 2.5 ...). >>> gamma_iterative(-1) Traceback (most recent call last): ... ValueError: math domain error >>> gamma_iterative(0) Traceback (most re
(num: float)
| 16 | |
| 17 | |
| 18 | def gamma_iterative(num: float) -> float: |
| 19 | """ |
| 20 | Calculates the value of Gamma function of num |
| 21 | where num is either an integer (1, 2, 3..) or a half-integer (0.5, 1.5, 2.5 ...). |
| 22 | |
| 23 | >>> gamma_iterative(-1) |
| 24 | Traceback (most recent call last): |
| 25 | ... |
| 26 | ValueError: math domain error |
| 27 | >>> gamma_iterative(0) |
| 28 | Traceback (most recent call last): |
| 29 | ... |
| 30 | ValueError: math domain error |
| 31 | >>> gamma_iterative(9) |
| 32 | 40320.0 |
| 33 | >>> from math import gamma as math_gamma |
| 34 | >>> all(.99999999 < gamma_iterative(i) / math_gamma(i) <= 1.000000001 |
| 35 | ... for i in range(1, 50)) |
| 36 | True |
| 37 | >>> gamma_iterative(-1)/math_gamma(-1) <= 1.000000001 |
| 38 | Traceback (most recent call last): |
| 39 | ... |
| 40 | ValueError: math domain error |
| 41 | >>> gamma_iterative(3.3) - math_gamma(3.3) <= 0.00000001 |
| 42 | True |
| 43 | """ |
| 44 | if num <= 0: |
| 45 | raise ValueError("math domain error") |
| 46 | |
| 47 | return quad(integrand, 0, inf, args=(num))[0] |
| 48 | |
| 49 | |
| 50 | def integrand(x: float, z: float) -> float: |