From dc796b6ea743c25eb555813d4cdb518b5cbeb773 Mon Sep 17 00:00:00 2001 From: felixdupont Date: Wed, 4 Oct 2023 21:43:57 -0400 Subject: [PATCH 1/4] refactor(gamma): Append _iterative to func name --- maths/gamma.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/maths/gamma.py b/maths/gamma.py index d5debc58764b..138ee9a6759f 100644 --- a/maths/gamma.py +++ b/maths/gamma.py @@ -4,32 +4,32 @@ from scipy.integrate import quad -def gamma(num: float) -> float: +def gamma_iterative(num: float) -> float: """ https://en.wikipedia.org/wiki/Gamma_function - In mathematics, the gamma function is one commonly + In mathematics, the gamma_iterative function is one commonly used extension of the factorial function to complex numbers. - The gamma function is defined for all complex numbers except the non-positive + The gamma_iterative function is defined for all complex numbers except the non-positive integers - >>> gamma(-1) + >>> gamma_iterative(-1) Traceback (most recent call last): ... ValueError: math domain error - >>> gamma(0) + >>> gamma_iterative(0) Traceback (most recent call last): ... ValueError: math domain error - >>> gamma(9) + >>> gamma_iterative(9) 40320.0 >>> from math import gamma as math_gamma - >>> all(.99999999 < gamma(i) / math_gamma(i) <= 1.000000001 + >>> all(.99999999 < gamma_iterative(i) / math_gamma(i) <= 1.000000001 ... for i in range(1, 50)) True - >>> gamma(-1)/math_gamma(-1) <= 1.000000001 + >>> gamma_iterative(-1)/math_gamma(-1) <= 1.000000001 Traceback (most recent call last): ... ValueError: math domain error - >>> gamma(3.3) - math_gamma(3.3) <= 0.00000001 + >>> gamma_iterative(3.3) - math_gamma(3.3) <= 0.00000001 True """ if num <= 0: From b6cde457a0867ce99229403db8800c2c16558c80 Mon Sep 17 00:00:00 2001 From: felixdupont Date: Wed, 4 Oct 2023 21:57:36 -0400 Subject: [PATCH 2/4] refactor(gamma): Consolidate implementations --- maths/gamma.py | 84 +++++++++++++++++++++++++++++++++++++--- maths/gamma_recursive.py | 77 ------------------------------------ 2 files changed, 79 insertions(+), 82 deletions(-) delete mode 100644 maths/gamma_recursive.py diff --git a/maths/gamma.py b/maths/gamma.py index 138ee9a6759f..dfbaaec454f5 100644 --- a/maths/gamma.py +++ b/maths/gamma.py @@ -1,3 +1,13 @@ +""" +Gamma function is a very useful tool in math and physics. +It helps calculating complex integral in a convenient way. +for more info: https://en.wikipedia.org/wiki/Gamma_function +In mathematics, the gamma function is one commonly +used extension of the factorial function to complex numbers. +The gamma function is defined for all complex numbers except +the non-positive integers +Python's Standard Library math.gamma() function overflows around gamma(171.624). +""" import math from numpy import inf @@ -6,11 +16,9 @@ def gamma_iterative(num: float) -> float: """ - https://en.wikipedia.org/wiki/Gamma_function - In mathematics, the gamma_iterative function is one commonly - used extension of the factorial function to complex numbers. - The gamma_iterative function is defined for all complex numbers except the non-positive - integers + 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): ... @@ -42,7 +50,73 @@ def integrand(x: float, z: float) -> float: return math.pow(x, z - 1) * math.exp(-x) +def gamma_recursive(num: float) -> float: + """ + 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 ...). + Implemented using recursion + Examples: + >>> from math import isclose, gamma as math_gamma + >>> gamma_recursive(0.5) + 1.7724538509055159 + >>> gamma_recursive(2) + 1.0 + >>> gamma_recursive(3.5) + 3.3233509704478426 + >>> gamma_recursive(171.5) + 9.483367566824795e+307 + >>> all(isclose(gamma_recursive(num), math_gamma(num)) \ + for num in (0.5, 2, 3.5, 171.5)) + True + >>> gamma_recursive(0) + Traceback (most recent call last): + ... + ValueError: math domain error + >>> gamma_recursive(-1.1) + Traceback (most recent call last): + ... + ValueError: math domain error + >>> gamma_recursive(-4) + Traceback (most recent call last): + ... + ValueError: math domain error + >>> gamma_recursive(172) + Traceback (most recent call last): + ... + OverflowError: math range error + >>> gamma_recursive(1.1) + Traceback (most recent call last): + ... + NotImplementedError: num must be an integer or a half-integer + """ + if num <= 0: + raise ValueError("math domain error") + if num > 171.5: + raise OverflowError("math range error") + elif num - int(num) not in (0, 0.5): + raise NotImplementedError("num must be an integer or a half-integer") + elif num == 0.5: + return math.sqrt(math.pi) + else: + return 1.0 if num == 1 else (num - 1) * gamma_recursive(num - 1) + + +def test_gamma_recursive() -> None: + """ + >>> test_gamma_recursive() + """ + assert gamma_recursive(0.5) == math.sqrt(math.pi) + assert gamma_recursive(1) == 1.0 + assert gamma_recursive(2) == 1.0 + + if __name__ == "__main__": from doctest import testmod testmod() + num = 1.0 + while num: + num = float(input("Gamma of: ")) + print(f"gamma_iterative({num}) = {gamma_iterative(num)}") + print(f"gamma_recursive({num}) = {gamma_recursive(num)}") + print("\nEnter 0 to exit...") diff --git a/maths/gamma_recursive.py b/maths/gamma_recursive.py deleted file mode 100644 index 3d6b8c5e8138..000000000000 --- a/maths/gamma_recursive.py +++ /dev/null @@ -1,77 +0,0 @@ -""" -Gamma function is a very useful tool in math and physics. -It helps calculating complex integral in a convenient way. -for more info: https://en.wikipedia.org/wiki/Gamma_function -Python's Standard Library math.gamma() function overflows around gamma(171.624). -""" -from math import pi, sqrt - - -def gamma(num: float) -> float: - """ - 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 ...). - Implemented using recursion - Examples: - >>> from math import isclose, gamma as math_gamma - >>> gamma(0.5) - 1.7724538509055159 - >>> gamma(2) - 1.0 - >>> gamma(3.5) - 3.3233509704478426 - >>> gamma(171.5) - 9.483367566824795e+307 - >>> all(isclose(gamma(num), math_gamma(num)) for num in (0.5, 2, 3.5, 171.5)) - True - >>> gamma(0) - Traceback (most recent call last): - ... - ValueError: math domain error - >>> gamma(-1.1) - Traceback (most recent call last): - ... - ValueError: math domain error - >>> gamma(-4) - Traceback (most recent call last): - ... - ValueError: math domain error - >>> gamma(172) - Traceback (most recent call last): - ... - OverflowError: math range error - >>> gamma(1.1) - Traceback (most recent call last): - ... - NotImplementedError: num must be an integer or a half-integer - """ - if num <= 0: - raise ValueError("math domain error") - if num > 171.5: - raise OverflowError("math range error") - elif num - int(num) not in (0, 0.5): - raise NotImplementedError("num must be an integer or a half-integer") - elif num == 0.5: - return sqrt(pi) - else: - return 1.0 if num == 1 else (num - 1) * gamma(num - 1) - - -def test_gamma() -> None: - """ - >>> test_gamma() - """ - assert gamma(0.5) == sqrt(pi) - assert gamma(1) == 1.0 - assert gamma(2) == 1.0 - - -if __name__ == "__main__": - from doctest import testmod - - testmod() - num = 1.0 - while num: - num = float(input("Gamma of: ")) - print(f"gamma({num}) = {gamma(num)}") - print("\nEnter 0 to exit...") From 5d73de72c93a8cf898aeb2a45e37a8357d74082d Mon Sep 17 00:00:00 2001 From: felixdupont Date: Thu, 5 Oct 2023 11:30:14 -0400 Subject: [PATCH 3/4] refactor(gamma): Redundant test function removal --- maths/gamma.py | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/maths/gamma.py b/maths/gamma.py index dfbaaec454f5..38b7a66a914f 100644 --- a/maths/gamma.py +++ b/maths/gamma.py @@ -59,6 +59,8 @@ def gamma_recursive(num: float) -> float: >>> from math import isclose, gamma as math_gamma >>> gamma_recursive(0.5) 1.7724538509055159 + >>> gamma_recursive(1) + 1.0 >>> gamma_recursive(2) 1.0 >>> gamma_recursive(3.5) @@ -101,15 +103,6 @@ def gamma_recursive(num: float) -> float: return 1.0 if num == 1 else (num - 1) * gamma_recursive(num - 1) -def test_gamma_recursive() -> None: - """ - >>> test_gamma_recursive() - """ - assert gamma_recursive(0.5) == math.sqrt(math.pi) - assert gamma_recursive(1) == 1.0 - assert gamma_recursive(2) == 1.0 - - if __name__ == "__main__": from doctest import testmod From 63cd7de639d68a411aa6f9fd6ca5b81e174d9c70 Mon Sep 17 00:00:00 2001 From: Tianyi Zheng Date: Fri, 6 Oct 2023 14:29:08 -0400 Subject: [PATCH 4/4] Update maths/gamma.py --- maths/gamma.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/maths/gamma.py b/maths/gamma.py index 38b7a66a914f..822bbc74456f 100644 --- a/maths/gamma.py +++ b/maths/gamma.py @@ -67,8 +67,8 @@ def gamma_recursive(num: float) -> float: 3.3233509704478426 >>> gamma_recursive(171.5) 9.483367566824795e+307 - >>> all(isclose(gamma_recursive(num), math_gamma(num)) \ - for num in (0.5, 2, 3.5, 171.5)) + >>> all(isclose(gamma_recursive(num), math_gamma(num)) + ... for num in (0.5, 2, 3.5, 171.5)) True >>> gamma_recursive(0) Traceback (most recent call last):