From e02b8e963189d786ac80960ac043bd5b79eb22a8 Mon Sep 17 00:00:00 2001 From: redxz5 Date: Tue, 1 Oct 2024 20:49:09 +0530 Subject: [PATCH 1/3] Taylor/Mclaurian expansion --- taylor_mclaurian_expansion/README.md | 25 ++++++++ taylor_mclaurian_expansion/main.py | 64 +++++++++++++++++++++ taylor_mclaurian_expansion/requirements.txt | 2 + 3 files changed, 91 insertions(+) create mode 100644 taylor_mclaurian_expansion/README.md create mode 100644 taylor_mclaurian_expansion/main.py create mode 100644 taylor_mclaurian_expansion/requirements.txt diff --git a/taylor_mclaurian_expansion/README.md b/taylor_mclaurian_expansion/README.md new file mode 100644 index 000000000..033a27172 --- /dev/null +++ b/taylor_mclaurian_expansion/README.md @@ -0,0 +1,25 @@ +# **Taylor and Mclaurian Expansions** + +[![python 3](https://img.shields.io/badge/python-3.8-blue)](https://python.org) + + This is a small convenient program to use to approximate values of expresstions using taylor expansions or + mclaurian expansions + + **Python** has a great library [*sympy*](https://github.com/sympy/sympy) which allows us to do many calculations, + and display them in a manner we are used to. + + +### Before you import sympy you need to install it: + + ```python + pip3 install sympy + ``` + + +### You can also do the same through the requirements.txt file + +```python +pip install requirements.txt +``` + +Some examples have been included for easier use. \ No newline at end of file diff --git a/taylor_mclaurian_expansion/main.py b/taylor_mclaurian_expansion/main.py new file mode 100644 index 000000000..b0dd7c894 --- /dev/null +++ b/taylor_mclaurian_expansion/main.py @@ -0,0 +1,64 @@ +import math +from sympy import * + +x = symbols('x') +y = symbols('y') + +def taylorexpansion(func,a,n,var): + t_y = symbols('t_y') + expansion = func.subs(var,a) + d = func + if(expansion==0): + n+=1 + try: + k=1 + while (k Date: Tue, 1 Oct 2024 21:18:37 +0530 Subject: [PATCH 2/3] flake8 linting --- taylor_mclaurian_expansion/main.py | 83 ++++++++++++++++-------------- 1 file changed, 44 insertions(+), 39 deletions(-) diff --git a/taylor_mclaurian_expansion/main.py b/taylor_mclaurian_expansion/main.py index b0dd7c894..83e69d7ae 100644 --- a/taylor_mclaurian_expansion/main.py +++ b/taylor_mclaurian_expansion/main.py @@ -1,64 +1,69 @@ -import math -from sympy import * +from sympy import symbols, factorial, diff, pprint, exp, log, sin x = symbols('x') y = symbols('y') -def taylorexpansion(func,a,n,var): + +def taylorexpansion(func, a, n, var): t_y = symbols('t_y') - expansion = func.subs(var,a) + expansion = func.subs(var, a) d = func - if(expansion==0): - n+=1 + if (expansion == 0): + n += 1 try: - k=1 - while (k Date: Tue, 1 Oct 2024 21:32:50 +0530 Subject: [PATCH 3/3] flake8 linting --- taylor_mclaurian_expansion/main.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/taylor_mclaurian_expansion/main.py b/taylor_mclaurian_expansion/main.py index 83e69d7ae..888a042f6 100644 --- a/taylor_mclaurian_expansion/main.py +++ b/taylor_mclaurian_expansion/main.py @@ -14,15 +14,15 @@ def taylorexpansion(func, a, n, var): k = 1 while (k < n): d = diff(d, var) - term = (d*((t_y-a)**k))/factorial(k) + term = (d * ((t_y - a) ** k)) / factorial(k) term = term.subs(var, a) if (term == 0): continue term = term.subs(t_y, var) - expansion = term+expansion + expansion = term + expansion k += 1 if (d == 0 and k < n): - print("only ", k-1, " terms present") + print("only ", k - 1, " terms present") if (n < 1): print("3rd argument is for no. of terms, provide a natural number") return '' @@ -54,16 +54,16 @@ def examples(): print(taylorvalue(exp(x), 1, 10, x)) # log(1+x) expansion at x=0 with 5 terms differentiating with respect to x - pprint(taylorexpansion(log(x+1), 0, 5, x)) + pprint(taylorexpansion(log(x + 1), 0, 5, x)) # sin(x) expansion at x=0 with 5 terms differentiating with respect to x pprint(taylorexpansion(sin(x), 0, 5, x)) # expansion for expression at x=0 with 3 terms differentiating wrt to x - pprint(taylorexpansion((5*x**2)+(3*x)+(7), 0, 3, x)) + pprint(taylorexpansion((5 * x ** 2) + (3 * x) + (7), 0, 3, x)) # e^(xy) expansion at x=1 with 5 terms differentiating with respect to x - pprint(taylorexpansion(exp(x*y), 1, 5, x)) + pprint(taylorexpansion(exp(x * y), 1, 5, x)) examples()