Skip to content

Commit

Permalink
updated
Browse files Browse the repository at this point in the history
  • Loading branch information
Vipul-Cariappa committed Jan 1, 2023
1 parent f06ea63 commit 7f1ba67
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 53 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,4 @@ poetry.toml
# User defined
*.png
.vscode/
*tmp*
51 changes: 27 additions & 24 deletions fourier_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
import matplotlib.pyplot as plt


def fourier_series_(func, start, end):
n = symbols("n")
x, n = symbols("x n")

def fourier_series_evaluator(func, start, end):
T = end - start
cos_term = cos((2 * n * pi * x) / T)
sin_term = sin((2 * n * pi * x) / T)
Expand All @@ -14,24 +15,18 @@ def fourier_series_(func, start, end):
an = 2 / T * (integrate(func * cos_term, (x, start, end)).doit()) * cos_term
bn = 2 / T * (integrate(func * sin_term, (x, start, end)).doit()) * sin_term

return a0 + an + bn
return (a0, an, bn)


def fourier_visualizer(func, start, end, terms=6, repeat=1, dir=1):
n = symbols("n")
def fourier_graph_compute(func, start, end, terms=6, repeat=1, dir=1):
a0, an, bn = fourier_series_evaluator(func, start, end)
T = end - start
cos_term = cos((2 * n * pi * x) / T)
sin_term = sin((2 * n * pi * x) / T)

a0 = 2 / T * (integrate(func, (x, start, end)).doit())

an = 2 / T * (integrate(func * cos_term, (x, start, end)).doit()) * cos_term
bn = 2 / T * (integrate(func * sin_term, (x, start, end)).doit()) * sin_term

if dir > 0:
x_axis = np.linspace(float(start), float(start + T * repeat), 100)
x_axis = np.linspace(start, start + T * repeat, 50)
else:
x_axis = np.linspace(float(end - (T * repeat)), float(end), 100)
x_axis = np.linspace(end - (T * repeat), end, 50)

y_axis = []

for i in range(terms):
Expand All @@ -40,26 +35,34 @@ def fourier_visualizer(func, start, end, terms=6, repeat=1, dir=1):

if i > 0:
y_axis.append(
tmp := np.vectorize(
np.vectorize(
lambda value: float(an_i.subs(x, value) + bn_i.subs(x, value))
)(x_axis)
+ y_axis[i - 1]
)
else:
y_axis.append(
tmp := np.vectorize(
np.vectorize(
lambda value: float(an_i.subs(x, value) + bn_i.subs(x, value) + a0)
)(x_axis)
)
plt.plot(x_axis, y_axis[i])

return x_axis, y_axis


def fourier_visualizer(func, start, end, terms=6, repeat=1, dir=1):
x_axis, y_axis = fourier_graph_compute(func, start, end, terms, repeat, dir)

for i in y_axis:
plt.plot(x_axis, i)
plt.show()


x = symbols("x")
func = eval(input("Enter Function to be Plot\n\tf(x) = "))
start = float(input("Enter start of period: "))
end = float(input("Enter end of period: "))
repeat = int(input("Repeat the period how many time [1]: ") or "1")
direction = int(input("Repeat in +ve or -ve [1]: ") or "1")
if __name__ == "__main__":
func = eval(input("Enter Function to be Plot\n\tf(x) = "))
start = float(input("Enter start of period: "))
end = float(input("Enter end of period: "))
repeat = int(input("Repeat the period how many time [1]: ") or "1")
direction = int(input("Repeat in +ve or -ve [1]: ") or "1")

fourier_visualizer(func, start, end, repeat=repeat, dir=direction)
fourier_visualizer(func, start, end, repeat=repeat, dir=direction)
32 changes: 19 additions & 13 deletions graph_2d.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
from math import *
from sympy import *

string = input("Enter Function to be Plot\n\tf(x) = ")

func_str = "lambda x: " + string.replace("^", "**")
func = eval(func_str)
x = symbols("x")

x = np.linspace(-10, 10, 100)
y = np.apply_along_axis(np.vectorize(func), 0, x)

plt.plot(x, y)
plt.ylabel(string)
plt.xlabel("x")
plt.show()
# file_name = str(datetime.now()).split(".")[0].replace(":", "-") + ".png"
# plt.savefig(file_name, bbox_inches='tight')
def graph_compute_points(func, start=-3.14, end=3.14):
x_axis = np.linspace(start, end, 50)
y_axis = np.vectorize(lambda value: float(func.subs(x, value)))(x_axis)

return x_axis, y_axis


def graph(func, start=-3.14, end=3.14):
x_axis, y_axis = graph_compute_points(func, start, end)

plt.plot(x_axis, y_axis)
plt.show()


if __name__ == "__main__":
func = input("f(x): ")
graph(eval(func))
13 changes: 13 additions & 0 deletions helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from collections.abc import Iterable
from sympy import *


def convert_to_latex(item):
if not isinstance(item, Iterable):
return latex(item)

item_latex = []
for i in item:
item_latex.append(latex(i))

return item_latex
66 changes: 61 additions & 5 deletions logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ def __str__(self):
class Variable:
def __init__(self, name):
self.name = name
self.table = None

def generate_table_md(self):
self.table = dict()
self.table[str(self)] = [0, 1]
return f"| {str(self)} |\n| - |\n| 0 |\n| 1 |"

def __add__(self, other):
# self + other
Expand Down Expand Up @@ -452,6 +458,39 @@ def display_table(self):
print("\n")
print("\n")

def generate_table_md(self):
if self.table is None:
self.generate_truth_table()

table_md = ""

table_length = len(self.table[str(self)])
header_string_length = [len(i) for i in self.table.keys()]

table_md += "|"
for i in self.table.keys():
table_md += f" {i} |"
table_md += "\n"

table_md += "|"
for _ in self.table.keys():
table_md += f" - |"
table_md += "\n"

for i in range(table_length):
table_md += "|"
for index, j in enumerate(self.table.keys()):
table_md += (
" " * math.floor((header_string_length[index] - 1) / 2)
+ f" {self.table[j][i]}"
+ " " * math.ceil((header_string_length[index] - 1) / 2)
+ " |"
)
table_md += "\n"
table_md += "\n"

return table_md

def __add__(self, other):
# self + other
eq = Equation()
Expand Down Expand Up @@ -829,10 +868,27 @@ def __str__(self):
return " ".join((str(i) for i in self.string))


if __name__ == "__main__":
# Example use
x = Variable("x")
y = Variable("y")
z = Variable("z")
a = Variable("a")
b = Variable("b")
c = Variable("c")
d = Variable("d")
e = Variable("e")
p = Variable("p")
q = Variable("q")
r = Variable("r")
s = Variable("s")
u = Variable("u")
v = Variable("v")
w = Variable("w")
x = Variable("x")
y = Variable("y")
z = Variable("z")


def truth_table_generator(expression_string):
expression = eval(expression_string)
return expression.generate_table_md(), expression.table


if __name__ == "__main__":
(x % y).display_table()
32 changes: 21 additions & 11 deletions taylor_series.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,25 @@
x = symbols("x")


def taylor_series(func, at=1, power_upto=6):
def taylor_series(func, at=1, powers_upto=6):
terms = []

terms.append(func.subs(x, at))

factorial = 1
for i in range(1, power_upto + 1):
for i in range(1, powers_upto + 1):
func = func.diff(x)
factorial *= i
term = (func.subs(x, at) * (x - at) ** i) / factorial
# print(f"{i = }, {func = }, {factorial = }, {term = }, {func.subs(x, 1) = }")
terms.append(term)

return terms


def visualize(terms, at, interval=5):
x_axis = np.linspace(at - interval, at + interval, 100)
def taylor_series_computer(func, at=1, powers_upto=4, interval=3.14):
terms = taylor_series(func, at, powers_upto)

x_axis = np.linspace(at - interval, at + interval, 50)
y_axis = []

for i in range(len(terms)):
Expand All @@ -36,15 +37,24 @@ def visualize(terms, at, interval=5):
y_axis.append(
np.vectorize(lambda value: float(terms[i].subs(x, value)))(x_axis)
)
plt.plot(x_axis, y_axis[i])

return x_axis, y_axis


def taylor_series_visualizer(func, at=1, powers_upto=4, interval=3.14):
x_axis, y_axis = taylor_series_computer(func, at, powers_upto, interval)

for i in y_axis:
plt.plot(x_axis, i)

plt.show()


string = input("Enter Function to generate taylor series of\n\tf(x) = ")
pos = float(input("Enter the value around to generate: "))
no_terms = int(input("Enter number of terms: "))
if __name__ == "__main__":
string = input("Enter Function to generate taylor series of\n\tf(x) = ")
pos = float(input("Enter the value around to generate: "))
no_terms = int(input("Enter number of terms: "))

func = eval(string)
func = eval(string)

print(taylor_series(func, pos, no_terms))
print(taylor_series(func, pos, no_terms))

0 comments on commit 7f1ba67

Please sign in to comment.