Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
a565258
Add files via upload
avivfaraj Sep 3, 2021
4951686
Changed print to f-string
avivfaraj Sep 4, 2021
ff2a162
Add files via upload
avivfaraj Sep 4, 2021
6550183
Fixes: #4710 provided return type
avivfaraj Sep 4, 2021
552546f
File exists in another pull request
avivfaraj Sep 4, 2021
bc31e3f
imported radians from math
avivfaraj Sep 5, 2021
974e0a6
Updated file according to pre-commit test
avivfaraj Sep 6, 2021
1d4fc81
Updated file
avivfaraj Sep 6, 2021
7bd84b4
Updated gamma
avivfaraj Sep 6, 2021
2841c11
Deleted duplicate file
avivfaraj Sep 6, 2021
cea2deb
removed pi
avivfaraj Sep 6, 2021
db7db3b
reversed tests
avivfaraj Sep 7, 2021
132e495
Fixed angle condition
avivfaraj Sep 7, 2021
c0e5071
Modified prints to f-string
avivfaraj Sep 7, 2021
894fa7f
Update horizontal_projectile_motion.py
avivfaraj Sep 7, 2021
ee49ce7
Merge branch 'TheAlgorithms:master' into master
avivfaraj Sep 7, 2021
07646ac
Update horizontal_projectile_motion.py
avivfaraj Sep 7, 2021
4e2fcaf
Fixes #4710 added exceptions and tests
avivfaraj Sep 7, 2021
dcacc95
Added float tests
avivfaraj Sep 7, 2021
3f2b238
Fixed type annotations
avivfaraj Sep 14, 2021
e3678fd
Fixed last annotation
avivfaraj Sep 14, 2021
c37bb95
Fixed annotations
avivfaraj Sep 14, 2021
5b0249a
fixed format
avivfaraj Sep 14, 2021
ec790c6
Revert "fixed format"
avivfaraj Sep 14, 2021
e865929
Revert "Fixed annotations"
avivfaraj Sep 14, 2021
82b4e0d
Revert "Fixed last annotation"
avivfaraj Sep 14, 2021
63c7c6a
Revert "Fixed type annotations"
avivfaraj Sep 14, 2021
0527866
Merge branch 'TheAlgorithms:master' into master
avivfaraj Apr 4, 2022
805050e
Revert to 4e2fcaf6fb
avivfaraj Apr 4, 2022
5f3486c
Fixing errors found during pre-commit
avivfaraj Apr 4, 2022
c382131
Merge branch 'TheAlgorithms:master' into master
avivfaraj Apr 4, 2022
99f00b5
Added gauss law
avivfaraj Apr 5, 2022
c75582f
Implemented Lorenz tranformation with four vector
avivfaraj Apr 13, 2022
a462736
pre-commit fixes
avivfaraj Apr 13, 2022
4eab40b
flake8 fixes
avivfaraj Apr 13, 2022
9d0e75c
More flake8 fixes
avivfaraj Apr 13, 2022
7cfaf9e
Added blank space for flake8
avivfaraj Apr 13, 2022
b88de32
Merge branch 'TheAlgorithms:master' into master
avivfaraj Apr 13, 2022
83cbeca
Added reference
avivfaraj Apr 13, 2022
a080632
Trailing whitespace fix
avivfaraj Apr 13, 2022
53a70f3
Merge branch 'master' of https://github.com/avivfaraj/Python
avivfaraj Apr 13, 2022
36f0cd7
Replaced argument u with velocity (descriptive name fix)
avivfaraj Apr 14, 2022
b31721d
Added tests for functions + moved velocity check to beta function
avivfaraj Apr 14, 2022
b1bbc83
Modified condition to 'not symbolic' in the transform function
avivfaraj Apr 14, 2022
ee3601f
trainling whitespace fix
avivfaraj Apr 14, 2022
4e337f6
Added type hint for 'smybolic' argument in transform function
avivfaraj Apr 15, 2022
798b91f
Changed reference to avoid pre-commit fails because of spelling issue…
avivfaraj Apr 15, 2022
982d66b
Added tests for gamma and transformation_matrix functions
avivfaraj Apr 15, 2022
ec4dbfb
Fixed transformation_matrix tests
avivfaraj Apr 17, 2022
9c7a85e
Fixed tests on beta and gamma functions
avivfaraj Apr 18, 2022
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
Prev Previous commit
Next Next commit
Updated file
  • Loading branch information
avivfaraj committed Sep 6, 2021
commit 1d4fc814fac69ecd821559b0a540f4eea588b922
83 changes: 83 additions & 0 deletions maths/gamma_recursive.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
"""
Gamma function is a very useful tool in physics.
It helps calculating complex integral in a convenient way.
for more info: https://en.wikipedia.org/wiki/Gamma_function
"""

# Importing packages
from math import sqrt, pi
from re import match
from typing import Union


def gamma(num : Union[int, float]) -> Union[int, 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:
>>> Gamma of: 0.5
√π
>>> Gamma of: 2
1
>>> Gamma of: 3.5
1.875√π
"""
if num == 1:
return 1
elif num == 0.5:
return sqrt(pi)
elif num > 1:
return (num - 1) * gamma(num - 1)
# Error
return -2


def test_gamma() -> None:
"""
>>> test_gamma()
"""
assert sqrt(pi) == gamma(0.5)
assert 1 == gamma(1)
assert 1 == gamma(2)


if __name__ == "__main__":
# Initialize boolean
number = True
# Get input from user
input_ = input("Gamma of: ")
# Ensure valid input
try:
# Ensure input matches half-integer (float) pattern
if match(r"^[0-9]*\.5$", input_):
# Convert string to float
num = float(input_)
# Ensure input matches an integer pattern
elif match(r"^[1-9][0-9]*$", input_):
# Convert string to int
num = int(input_)
# Input is not a valid number
else:
# raise an error
raise ValueError
# Ensure print an error message
except ValueError:
print("Error: Input must be an integer or an half-integer!")
number = False
finally:
# Ensure input is a valid number
if number:
print(f"\u0393({num}) = ", end="")
# Ensure input is an integer
if isinstance(gamma(num), int):
# Print result
print(gamma(num))
# Otherwise print results with √π (gamma of 0.5 is √π)
# Therefore all results will be a number times √π
else:
results = f"{gamma(num) / sqrt(pi):.4f}"
results = results.rstrip("0").rstrip(".")
if results == "1":
results = ""
print(results + "\u221A\u03c0")