diff --git a/mathfunctionize/algebra.py b/mathfunctionize/algebra.py new file mode 100644 index 0000000..c8077d1 --- /dev/null +++ b/mathfunctionize/algebra.py @@ -0,0 +1,30 @@ +import arithmetic +def algebraGamma(x): + if x == 0 or (x < 0 and x % 1 == 0): + raise Exception("Invalid input") + if x == 1: + return 1 + if x == 0.5: + return algebraSquareRoot(3.141592653589793) + return (x - 1) * algebraGamma(x - 1) +def algebraFactorial(x): + if x == 0: + return 1 + else: + return x * algebraFactorial(x-1) +def algebraAbsolute(x): + if x < 0: + return -x + else: + return x +def algebraSquareRoot(x): + return x ** (1/2) +def algebraCubeRoot(x): + return x ** (1/3) +def algebraNthRoot(x, n): + return x ** (1/n) +def algebraRound(x, place): + if (place > 0 and arithmetic.arithmeticModulo(place, 10) == 0) or (place == 1): + if arithmetic.arithmeticModulo(x, place) < (arithmetic.arithmeticMultiplication(0.5, place)): + return arithmetic.arithmeticFlatDivision(x, place) + return arithmetic.arithmeticFlatDivision(x, place) + place \ No newline at end of file diff --git a/mathfunctionize/mathfunctionize.py b/mathfunctionize/mathfunctionize.py index bed9c76..2c0c6d1 100644 --- a/mathfunctionize/mathfunctionize.py +++ b/mathfunctionize/mathfunctionize.py @@ -18,35 +18,21 @@ def modulo(a, b): def flatDivision(a, b): return arithmetic.arithmeticFlatDivision(a, b) # algebra +import algebra def gamma(x): - if x == 0 or (x < 0 and x % 1 == 0): - raise Exception("Invalid input") - if x == 1: - return 1 - if x == 0.5: - return squareRoot(pi) - return (x - 1) * gamma(x - 1) + return algebra.algebraGamma(x) def factorial(x): - if x == 0: - return 1 - else: - return x * factorial(x-1) + return algebra.algebraFactorial(x) def absolute(x): - if x < 0: - return -x - else: - return x + return algebra.algebraAbsolute(x) def squareRoot(x): - return x ** (1/2) + return algebra.algebraSquareRoot(x) def cubeRoot(x): - return x ** (1/3) + return algebra.algebraCubeRoot(x) def nthRoot(x, n): - return x ** (1/n) + return algebra.algebraNthRoot(x, n) def round(x, place): - if (place > 0 and modulo(place, 10) == 0) or (place == 1): - if modulo(x, place) < (multiplication(0.5, place)): - return flatDivision(x, place) - return flatDivision(x, place) + place + return algebra.algebraRound(x, place) # counting def combinations(n, r): return division(permutations(n, r), factorial(r))