Added to maths and strings (#1642)

* Added to maths and strings

* added changes suggest by cclauss
This commit is contained in:
Cole Mollica
2020-01-08 08:18:17 -05:00
committed by Christian Clauss
parent e849578e59
commit a26ae00b24
6 changed files with 196 additions and 0 deletions

19
maths/combinations.py Normal file
View File

@ -0,0 +1,19 @@
from math import factorial
def combinations(n, k):
"""
>>> combinations(10,5)
252
>>> combinations(6,3)
20
>>> combinations(20,5)
15504
"""
return int(factorial(n) / ((factorial(k)) * (factorial(n - k))))
if __name__ == "__main__":
from doctest import testmod
testmod()

60
maths/gamma.py Normal file
View File

@ -0,0 +1,60 @@
import math
from scipy.integrate import quad
from numpy import inf
def gamma(num: float) -> float:
"""
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
>>> 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
>>> from math import gamma as math_gamma
>>> all(gamma(i)/math_gamma(i) <= 1.000000001 and abs(gamma(i)/math_gamma(i)) > .99999999 for i in range(1, 50))
True
>>> from math import gamma as math_gamma
>>> gamma(-1)/math_gamma(-1) <= 1.000000001
Traceback (most recent call last):
...
ValueError: math domain error
>>> from math import gamma as math_gamma
>>> gamma(3.3) - math_gamma(3.3) <= 0.00000001
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()

29
maths/radians.py Normal file
View File

@ -0,0 +1,29 @@
from math import pi
def radians(degree: float) -> float:
"""
Coverts the given angle from degrees to radians
https://en.wikipedia.org/wiki/Radian
>>> radians(180)
3.141592653589793
>>> radians(92)
1.6057029118347832
>>> radians(274)
4.782202150464463
>>> radians(109.82)
1.9167205845401725
>>> from math import radians as math_radians
>>> all(abs(radians(i)-math_radians(i)) <= 0.00000001 for i in range(-2, 361))
True
"""
return degree / (180 / pi)
if __name__ == "__main__":
from doctest import testmod
testmod()