Skip to content

Commit f979b2e

Browse files
committed
Add vectorize
1 parent ad1dd73 commit f979b2e

File tree

11 files changed

+52
-36
lines changed

11 files changed

+52
-36
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,18 @@
11
from eggdriver.resources.math.constants import inf
2+
from eggdriver.resources.math.theoric import positiveInfinity, negativeInfinity, undefined
23

34
def lim(f, value):
4-
result = f(value - 0.00000000001)
5+
zero = 1 / inf
6+
below = f(value - zero)
7+
above = f(value + zero)
8+
if -zero <= below - above <= zero:
9+
result = (below + above) / 2
10+
else:
11+
return undefined.value
512
if result >= inf:
6-
return "inf"
13+
return positiveInfinity.value
714
elif result <= -inf:
8-
return "-inf"
15+
return negativeInfinity.value
16+
elif -zero <= result <= zero:
17+
return 0
918
return result

eggdriver/resources/math/linear.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from eggdriver.resources.structures.lists import List
2+
from eggdriver.resources.utils import indexes
23

34
def dualExpand(a, b):
45
if a.size < b.size:
@@ -37,6 +38,33 @@ def scale(vector, scalar):
3738
result.append(scalar * i)
3839
return result
3940

41+
def vectorize(poly: str):
42+
p = poly.split(" ")
43+
result = Vector()
44+
coefs = List()
45+
exps = List()
46+
for i in p:
47+
temp = i + "^1"
48+
monomial = temp.strip("+").split("^")
49+
c = monomial[0].strip("x")
50+
if c == "":
51+
coef = 1.0
52+
else:
53+
coef = float(c)
54+
exp = int(monomial[1])
55+
if "x" not in i:
56+
exp = 0
57+
coefs.append(coef)
58+
exps.append(exp)
59+
degree = 0
60+
for i in indexes(exps):
61+
if exps[i] > degree:
62+
degree = exps[i]
63+
result.expand(degree + 1)
64+
for i in indexes(coefs):
65+
result[exps[i]] += coefs[i]
66+
return result
67+
4068
class Vector(List):
4169
def __init__(self, list = []):
4270
super().__init__(list)

eggdriver/resources/math/polynomial.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from eggdriver.resources.math.linear import Vector, dualExpand
1+
from eggdriver.resources.math.linear import Vector, dualExpand, vectorize
22
from eggdriver.resources.math.algorithms.solver import solve
33

44
def x_(i, variable = "x"):
@@ -40,9 +40,6 @@ def times(a, b):
4040
result = result.plus(product)
4141
return result
4242

43-
def vectorize(poly: str):
44-
pass
45-
4643
class Polynomial(Vector):
4744
def __init__(self, poly = [], variable = "x"):
4845
if type(poly) != list:
@@ -60,7 +57,7 @@ def display(self):
6057
if self[i] > 0:
6158
nonZeros += 1
6259
if self[i] > 0 and nonZeros > 1:
63-
result.append("+ " + str(self[i]) + x_(i, self.var))
60+
result.append("+" + str(self[i]) + x_(i, self.var))
6461
elif self[i] != 0:
6562
result.append(str(self[i]) + x_(i, self.var))
6663
text = ""

eggdriver/resources/math/theoric/__init__.py

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,8 @@ class infinity():
22
def __init__(self, name):
33
self.value = name
44

5-
class constant():
6-
def __init__(self, x):
7-
self.value = x
8-
95
def isConstant(x):
10-
return type(x) == constant
6+
return type(x) == int or type(x) == float
117

128
positiveInfinity = infinity("positiveInfinity")
139
negativeInfinity = infinity("negativeInfinity")
@@ -18,7 +14,7 @@ def isInfinity(x):
1814

1915
def add(x, y):
2016
if isConstant(x) and isConstant(y):
21-
return x.value + y.value
17+
return x + y
2218
elif isConstant(x) and isInfinity(y):
2319
return y.value
2420
elif isConstant(y) and isInfinity(x):
@@ -33,7 +29,7 @@ def multiply(x, y):
3329
if x == undefined or y == undefined:
3430
return undefined
3531
elif isConstant(x) and isConstant(y):
36-
return x.value * y.value
32+
return x * y
3733
if isConstant(x) and isInfinity(y):
3834
if x == 0:
3935
return undefined.value
@@ -49,7 +45,4 @@ def multiply(x, y):
4945
if x == y:
5046
return positiveInfinity.value
5147
else:
52-
return negativeInfinity.value
53-
54-
class variable():
55-
pass
48+
return negativeInfinity.value
Binary file not shown.

eggdriver/resources/utils.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,6 @@ def colour(id: int, bgcolor: int = 0):
88
if bgcolor != 0:
99
return "\033[1;" + str(id) + ";" + str(bgcolor + 10) + "m "
1010
return "\033[1;" + str(id) + "m"
11+
12+
def indexes(x):
13+
return range(0, len(x))

0 commit comments

Comments
 (0)