-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
Physics new code #4709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Physics new code #4709
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5b663f6
added gamma_function
avivfaraj a267f40
Add files via upload
avivfaraj 051167d
Resolved issue with str.format
avivfaraj 902e13a
Update gamma_function.py
avivfaraj 6ccad7f
Rename physics/gamma_function.py to maths/gamma_recursive.py
avivfaraj a99383e
Fixes: #4709 Fixed issues for pre-commit test
avivfaraj c2d7623
Fixes: #4709 solved issues with doctests
avivfaraj 5c1af2d
Fixes: #4709 Added failed tests to doctest
avivfaraj 4b5755d
Align with Python's Standard Library math.gamma()
cclauss 100c16f
Update gamma_recursive.py
cclauss ce11d45
Update gamma_recursive.py
cclauss de5562e
Update gamma_recursive.py
cclauss 6a1e1b9
Update gamma_recursive.py
cclauss File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
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 | ||
while num: | ||
num = float(input("Gamma of: ")) | ||
print(f"gamma({num}) = {gamma(num)}") | ||
print("\nEnter 0 to exit...") |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.