Skip to content

Commit 1bd36b1

Browse files
committed
Add Matrix, improve Polynomial
1 parent f979b2e commit 1bd36b1

File tree

9 files changed

+41
-10
lines changed

9 files changed

+41
-10
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.

eggdriver/resources/math/linear.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def plus(a, b, autoExpand = False):
1313
if autoExpand:
1414
dualExpand(a, b)
1515
else:
16-
raise Exception("Vectors have to be of the same size")
16+
raise LinearError
1717
if len(a) != 0 and len(a) == len(b):
1818
for i in range(0, a.size):
1919
result.append(a[i] + b[i])
@@ -25,7 +25,7 @@ def dot(a, b, autoExpand = False):
2525
if autoExpand:
2626
dualExpand(a, b)
2727
else:
28-
raise Exception("Vectors have to be of the same size")
28+
raise LinearError
2929
if len(a) != 0 and len(a) == len(b):
3030
for i in range(0, a.size):
3131
result += a[i] * b[i]
@@ -39,6 +39,7 @@ def scale(vector, scalar):
3939
return result
4040

4141
def vectorize(poly: str):
42+
"""Transform a string polynomial into a coordinates vector"""
4243
p = poly.split(" ")
4344
result = Vector()
4445
coefs = List()
@@ -77,3 +78,22 @@ def scale(self, scalar):
7778
def expand(self, scalar):
7879
[self.append(0) for i in range(0, scalar)]
7980

81+
class Matrix(Vector):
82+
def __init__(self, n, m):
83+
vectorOfVectors = Vector()
84+
for i in range(m):
85+
v = Vector()
86+
v.expand(n)
87+
vectorOfVectors.append(v)
88+
super().__init__(vectorOfVectors)
89+
def display(self):
90+
for i in self:
91+
print(i.display(True, ["(", ")"]))
92+
93+
class LinearError(Exception):
94+
def __init__(self, type = 0):
95+
message = {
96+
0: "Vectors have to be of the same size"
97+
}
98+
super().__init__(message[type])
99+

eggdriver/resources/math/polynomial.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ def times(a, b):
4040
result = result.plus(product)
4141
return result
4242

43+
def fromZeros(zeros = []):
44+
result = Polynomial(1)
45+
for i in zeros:
46+
result = Polynomial([-i, 1]).times(result)
47+
return result
48+
4349
class Polynomial(Vector):
4450
def __init__(self, poly = [], variable = "x"):
4551
if type(poly) != list:
Binary file not shown.

eggdriver/resources/structures/lists.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@ def removeLast(self):
1818
self.reverse()
1919
def contains(self, item):
2020
return item in self
21-
def display(self):
22-
text = "[ "
21+
def display(self, returnText = False, symbols = ["[", "]"]):
22+
text = symbols[0] + " "
2323
for i in self:
2424
text += str(i) + " "
25-
print(text + "]")
25+
if returnText:
26+
return text + symbols[1]
27+
else:
28+
print(text + "]")
2629
def Iterator(self):
2730
return Iterator(self)
2831
def iterate(self, function):

eggdriver/resources/utils.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ def colour(id: int, bgcolor: int = 0):
1111

1212
def indexes(x):
1313
return range(0, len(x))
14+

test.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
from eggdriver import Polynomial, vectorize
2-
3-
4-
u = Polynomial("7 +x -49x^2 -2x^9 +x^40 +3x^2")
5-
u.display()
1+
from eggdriver import Matrix, Vector
62

3+
v = Vector()
4+
v.expand(3)
5+
v.display()
6+
u = Matrix(3, 3)
7+
u.display()

0 commit comments

Comments
 (0)