From ac1ddfb98b9697ee80e0831a72d0d202252453d4 Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Mon, 17 Nov 2025 02:33:06 +0100 Subject: [PATCH] feat: math chapter done + editorconfig + remove pycache --- .editorconfig | 16 +++++ src/main.py | 50 ++------------ src/modules/__pycache__/math.cpython-313.pyc | Bin 3341 -> 0 bytes .../__pycache__/strings.cpython-313.pyc | Bin 406 -> 0 bytes src/modules/math.py | 64 +++++++++++++++++- src/modules/probability.py | 10 +++ 6 files changed, 93 insertions(+), 47 deletions(-) create mode 100644 .editorconfig delete mode 100644 src/modules/__pycache__/math.cpython-313.pyc delete mode 100644 src/modules/__pycache__/strings.cpython-313.pyc create mode 100644 src/modules/probability.py diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..e7ef85b --- /dev/null +++ b/.editorconfig @@ -0,0 +1,16 @@ +root = true + +# Unix-style newlines with a newline ending every file +[*] +end_of_line = lf +insert_final_newline = true + +[*.{md,py}] +charset = utf-8 + +[*.py] +indent_style = tab +indent_size = 4 + +[Makefile] +indent_style = tab diff --git a/src/main.py b/src/main.py index fadbf60..7c9e113 100644 --- a/src/main.py +++ b/src/main.py @@ -1,55 +1,13 @@ from sympy import diff, limit, oo, symbols from modules.math import ( - t_build_derivate, - t_build_derivative_from_limit, - t_build_partial_derivate, - t_calculate_derivative_limit, - t_calculate_e, - t_calculate_e_limit, - t_compound_interest, - t_compound_interest_algorigthm, - t_compound_interest_continious, - t_compound_interest_continious_exp, - t_exponent, - t_limit, - t_logarithm_natural, + test_math_module ) from modules.strings import t_strings if __name__=="__main__": t_strings() - t_exponent(2,8) - print(t_compound_interest(100, 20 / 100, 2, 12)) - print(t_compound_interest_algorigthm(100, 20 / 100, 2, 12)) - print(t_compound_interest_continious(100, 20 / 100, 2)) - print(t_compound_interest_continious_exp(100, 20 / 100, 2)) - print(t_calculate_e(1000000000000)) - print(t_logarithm_natural(10)) - 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) - print(t_build_derivative_from_limit(x ** 2)) - print(t_calculate_derivative_limit(x ** 2, 2)) - ## Chain rule - # for a given function y (lets say y = x ** 2 + 1) and another z (lets say z = y ** 3 - 2), we can find a derivativeof z with respect to x by multiplying the derivatives - # normal derivative calc - x = symbols("x") - z = (x ** 2 + 1) ** 3 - 2 - print(t_build_derivate(z)) - # chain rule calc - x, y, z = symbols("x y z") - y_f = x ** 2 + 1 - z_f = y ** 3 - 2 - print(t_build_derivate(y_f), t_build_derivate(z_f)) + print(">> Math module") + test_math_module() + print(">>>>>>>>>>>>>>>>>>>>") diff --git a/src/modules/__pycache__/math.cpython-313.pyc b/src/modules/__pycache__/math.cpython-313.pyc deleted file mode 100644 index 81f90b537ffe18a1be647ff46417af161cb3d4de..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3341 zcmbtW&2JmW6`$d9$>oz%J4x2lrOp`lpZ(5V#8mIiwfeQt43i*58}uQdHHZhYYZ9W@l#S z{oe0=?3`pWNrtwUnzMcvW9(~s30tH;IQb(6&za72evfT%5y{awa!=Wais*(aRLpTX zr`(He#6^5V6WT^XBsP*FiTUUp6DeJVOzSboj2?#^(KX0XJpnnUCn2+X3UXXeLr&-! z$P4-i1Td&t8DozJm{v?7}x(Up}xDHCj^KfBW3 zork{7%HUJ1(uY69bMga-@3S0^F0l!A7mV9nXC=O*GWM7~S99!snz0mi?C=}PBVLFc zk($i|R*3kK`D?{pr*0Oj@7q?jc*m(U>!$6w#oJEREH?JNUB_P9-d{5J zjC!MH7F|&()}3mzX1c|?;q5Lr_Wi`o9n&U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;k-bv3nZ?Qm^h9_v5;2y R%yF9Y8#Fg|0x=^Y{2K+ZcA5YH diff --git a/src/modules/__pycache__/strings.cpython-313.pyc b/src/modules/__pycache__/strings.cpython-313.pyc deleted file mode 100644 index 11854a754f43f5da6c69f99ff42cc2910136a7ca..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 406 zcmZ9I!AiqG5Qb;d+O%r%s8xg6;9>JJjHH9dcV_-%%1)iRVp2V7+fTR( znQ)1&n~rPgl~hED(v4d#Vqf84EzX9D#p%RH!U#LMPeUFv8r&sOK+pMT8nZ-6+UEhI zX{JJ+bZ@dQyZ3R6Q7J|==D~ErB$W!)$MP`EbW07(O0-bbD9c?8IecvFi_@% diff --git a/src/modules/math.py b/src/modules/math.py index 475affd..063fc52 100644 --- a/src/modules/math.py +++ b/src/modules/math.py @@ -1,8 +1,14 @@ +## This module represents the first chapter of the book +## "Essential Math for Data Science" - Thomas Nield +## Chaper 1 - Basic Math and Calculus Review + from cmath import log as complex_log # used for complex numbers from math import e, exp, log +from numpy import arange + # 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 diff, limit, oo, symbols +from sympy import diff, limit, oo, symbols, integrate def t_summ(): @@ -71,3 +77,59 @@ def t_build_derivate(f): def t_build_partial_derivate(f,symbol): return diff(f, symbol) + +def t_approximate_integral(f, init, end, precission): + integral = 0 + + for i in arange(init, end, precission): + w = i + precission + h = f(w) # or f(i) to estimate down + integral += (h * precission) + return integral + +def t_calculate_integral(f, init, end, symbol): + return integrate(f, (symbol, init, end)) + +def test_math_module(): + t_exponent(2,8) + print(t_compound_interest(100, 20 / 100, 2, 12)) + print(t_compound_interest_algorigthm(100, 20 / 100, 2, 12)) + print(t_compound_interest_continious(100, 20 / 100, 2)) + print(t_compound_interest_continious_exp(100, 20 / 100, 2)) + print(t_calculate_e(1000000000000)) + print(t_logarithm_natural(10)) + 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) + print(t_build_derivative_from_limit(x ** 2)) + print(t_calculate_derivative_limit(x ** 2, 2)) + + ## Chain rule + # for a given function y (lets say y = x ** 2 + 1) and another z (lets say z = y ** 3 - 2), we can find a derivativeof z with respect to x by multiplying the derivatives + # normal derivative calc + x = symbols("x") + z = (x ** 2 + 1) ** 3 - 2 + print(t_build_derivate(z)) + # chain rule calc + x, y, z = symbols("x y z") + y_f = x ** 2 + 1 + z_f = y ** 3 - 2 + print(t_build_derivate(y_f), t_build_derivate(z_f)) + + ## Integrals + def foo(x): + return x ** 2 + 1 + print(t_approximate_integral(foo, 0, 1, 0.001)) + x = symbols("x") + f = x ** 2 + 1 + print(t_calculate_integral(f, 0, 1, x)) diff --git a/src/modules/probability.py b/src/modules/probability.py new file mode 100644 index 0000000..43f72ab --- /dev/null +++ b/src/modules/probability.py @@ -0,0 +1,10 @@ +## This module represents the second chapter of the book +## "Essential Math for Data Science" - Thomas Nield +## Chaper 2 - Probability + +# 2.0 odds means than an events has twice the probabilities to happen than not +def p_odds_to_probability(o): + return (o / (1 + o)) + +def p_probability_to_odds(p): + return (p / (1 - p))