diff --git a/src/main.py b/src/main.py index 4897a93..8dadd26 100644 --- a/src/main.py +++ b/src/main.py @@ -1,4 +1,8 @@ +from sympy import diff, limit, oo, symbols + from modules.math import ( + t_build_partial_derivate, + t_build_derivate, t_calculate_e, t_calculate_e_limit, t_compound_interest, @@ -23,3 +27,14 @@ if __name__=="__main__": print(t_limit()) print(t_calculate_e_limit()) print(t_calculate_e_limit().evalf()) + x = symbols("x") + derivative = t_build_derivate(x ** 2) + value_on_two = derivative.subs(x,2) + print(derivative, value_on_two) + # Partial derivates are applied to one variable at a time + y = symbols("y") + z = symbols("z") + partial_derivative_y = t_build_partial_derivate((3 * y ** 3) + 2 * z ** 2, y) + partial_derivative_z = t_build_partial_derivate((3 * y ** 3) + 2 * z ** 2, z) + print(partial_derivative_y, partial_derivative_z) + diff --git a/src/modules/__pycache__/math.cpython-313.pyc b/src/modules/__pycache__/math.cpython-313.pyc index eb1aa60..81f90b5 100644 Binary files a/src/modules/__pycache__/math.cpython-313.pyc and b/src/modules/__pycache__/math.cpython-313.pyc differ diff --git a/src/modules/__pycache__/strings.cpython-313.pyc b/src/modules/__pycache__/strings.cpython-313.pyc index 1cfcf5c..11854a7 100644 Binary files a/src/modules/__pycache__/strings.cpython-313.pyc and b/src/modules/__pycache__/strings.cpython-313.pyc differ diff --git a/src/modules/math.py b/src/modules/math.py index 096bd86..ac8785e 100644 --- a/src/modules/math.py +++ b/src/modules/math.py @@ -2,7 +2,7 @@ from cmath import log as complex_log # used for complex numbers from math import e, exp, log # Sympy is powerful since it does not make aproximations, it keeps every primitive as it is, this means that it's slower but more precisse -from sympy import limit, oo, symbols +from sympy import diff, limit, oo, symbols def t_summ(): @@ -50,3 +50,13 @@ def t_calculate_e_limit(): x = symbols("x") f = (1 + 1 / x) ** x return limit(f, x, oo) + +# Calculate the slope by taking a close enough point +def t_calculate_derivative(f, x, step_size): + return (f(x + step_size) - f(x)) / (x +step_size - x) + +def t_build_derivate(f): + return diff(f) + +def t_build_partial_derivate(f,symbol): + return diff(f, symbol)