Skip to content

Commit befa1af

Browse files
committed
Add polynomials
1 parent 31767ee commit befa1af

15 files changed

Lines changed: 160 additions & 10 deletions

File tree

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
from eggdriver.resources.math.algorithms import *
22
from eggdriver.resources.math.theoric import *
33
from eggdriver.resources.math.float import *
4-
from eggdriver.resources.math.functions import *
4+
from eggdriver.resources.math.functions import *
5+
from eggdriver.resources.math.linear import *
6+
from eggdriver.resources.math.polynomial import *
90 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
2.12 KB
Binary file not shown.
2.72 KB
Binary file not shown.
Binary file not shown.

eggdriver/resources/math/algorithms/solver/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
from eggdriver.resources.math.algorithms.solver.dichotomy import root
33
from eggdriver.resources.math.constants import R
44

5-
def solutions(function, bias = 0, domain = R, accurancy = 16, grade = 1):
5+
def solutions(function, bias = 0, domain = R, accurancy = 16, degree = 1):
66
roots = Set()
7-
while roots.size < grade:
7+
while roots.size < degree:
88
r = root(function, bias, domain, accurancy)
99
roots.insert(r)
1010
return roots
Binary file not shown.

eggdriver/resources/math/linear.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
from eggdriver.resources.structures.lists import List
2+
3+
def dualExpand(a, b):
4+
if a.size < b.size:
5+
a.expand(b.size - a.size)
6+
else:
7+
b.expand(a.size - b.size)
8+
9+
def plus(a, b, autoExpand = False):
10+
result = Vector()
11+
if len(a) != len(b):
12+
if autoExpand:
13+
dualExpand(a, b)
14+
else:
15+
raise Exception("Vectors have to be of the same size")
16+
if len(a) != 0 and len(a) == len(b):
17+
for i in range(0, a.size):
18+
result.append(a[i] + b[i])
19+
return result
20+
21+
def dot(a, b, autoExpand = False):
22+
result = 0
23+
if len(a) != len(b):
24+
if autoExpand:
25+
dualExpand(a, b)
26+
else:
27+
raise Exception("Vectors have to be of the same size")
28+
if len(a) != 0 and len(a) == len(b):
29+
for i in range(0, a.size):
30+
result += a[i] * b[i]
31+
return result
32+
33+
def scale(vector, scalar):
34+
result = Vector()
35+
if vector.size != 0:
36+
for i in vector:
37+
result.append(scalar * i)
38+
return result
39+
40+
class Vector(List):
41+
def __init__(self, list = []):
42+
super().__init__(list)
43+
def plus(self, vector):
44+
return plus(self, vector)
45+
def dot(self, vector):
46+
return dot(self, vector)
47+
def scale(self, scalar):
48+
return scale(self, scalar)
49+
def expand(self, scalar):
50+
[self.append(0) for i in range(0, scalar)]
51+
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from eggdriver.resources.math.linear import Vector, dualExpand
2+
3+
def x_(i):
4+
if i==0 :
5+
return ""
6+
elif i == 1:
7+
return "x"
8+
return f"x^{i}"
9+
10+
def plusPoly(a, b):
11+
if type(a) != Polynomial:
12+
a = Polynomial([a])
13+
if type(b) != Polynomial:
14+
b = Polynomial([b])
15+
result = Polynomial()
16+
if len(a) != len(b):
17+
dualExpand(a, b)
18+
if len(a) != 0 and len(a) == len(b):
19+
for i in range(0, a.size):
20+
result.append(a[i] + b[i])
21+
return result
22+
23+
def scalePoly(polynomial, scalar):
24+
result = Vector()
25+
if polynomial.size != 0:
26+
for i in polynomial:
27+
result.append(scalar * i)
28+
return result
29+
30+
def times(a, b):
31+
result = Polynomial()
32+
if len(a) != 0:
33+
for i in range(0, len(a)):
34+
product = Polynomial()
35+
for j in range(0, i):
36+
product.append(0)
37+
for j in b.scale(a[i]):
38+
product.append(j)
39+
result = result.plus(product)
40+
return result
41+
42+
def vectorize(poly: str):
43+
pass
44+
45+
class Polynomial(Vector):
46+
def __init__(self, poly = []):
47+
if type(poly) != list:
48+
if type(poly) != str:
49+
poly = [poly]
50+
else:
51+
poly = vectorize(poly)
52+
super().__init__(poly)
53+
def display(self):
54+
result = Polynomial()
55+
nonZeros = 0
56+
if self.size != 0:
57+
for i in range(0, self.size):
58+
if self[i] > 0:
59+
nonZeros += 1
60+
if self[i] > 0 and nonZeros > 1:
61+
result.append("+ " + str(self[i]) + x_(i))
62+
elif self[i] != 0:
63+
result.append(str(self[i]) + x_(i))
64+
text = ""
65+
for i in result:
66+
text += i + " "
67+
print(text)
68+
def plus(self, vector):
69+
return plusPoly(self, vector)
70+
def times(self, vector):
71+
return times(self, vector)
72+
def power(self, exponent):
73+
result = Polynomial(1)
74+
for i in range(0, exponent):
75+
result = result.times(self)
76+
return result
77+
def eval(self, x):
78+
result = 0
79+
for i in range(0, self.size):
80+
f = self[i]
81+
result += f * (x ** i)
82+
return result

0 commit comments

Comments
 (0)