Skip to content

Commit ad1dd73

Browse files
committed
Improve polynomials
1 parent befa1af commit ad1dd73

15 files changed

Lines changed: 43 additions & 24 deletions

eggdriver/resources/math/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
from eggdriver.resources.math.float import *
44
from eggdriver.resources.math.functions import *
55
from eggdriver.resources.math.linear import *
6-
from eggdriver.resources.math.polynomial import *
6+
from eggdriver.resources.math.polynomial import *
7+
from eggdriver.resources.math.constants import *
46 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
475 Bytes
Binary file not shown.
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
from eggdriver.resources.structures.lists import Set
22
from eggdriver.resources.math.algorithms.solver.dichotomy import root
3-
from eggdriver.resources.math.constants import R
3+
from eggdriver.resources.math.constants import R, inf
44

5-
def solutions(function, bias = 0, domain = R, accurancy = 16, degree = 1):
5+
def solve(function, bias = 0, domain = R, accurancy = 16, degree = 1, alerts = True):
66
roots = Set()
7-
while roots.size < degree:
7+
count = 0
8+
customDomain = domain != R
9+
while roots.size < degree and count < inf:
10+
if not customDomain:
11+
domain = [- 2.0 ** int(count), 2.0 ** int(count)]
812
r = root(function, bias, domain, accurancy)
9-
roots.insert(r)
13+
roots.insert(str(r))
14+
if alerts:
15+
print(degree - roots.size, "solutions remaining")
16+
count += 1 / (2 * degree)
1017
return roots
Binary file not shown.
Binary file not shown.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
def root(function, bias = 0, domain = R, accurancy = 16):
66
"""Gives a root of function(x) = bias, in a certain domain"""
7-
error = 10 ** (- accurancy - 5)
7+
error = 10 ** (- accurancy - 100)
88
while True:
99
bound = [random.uniform(domain[0], domain[1]), random.uniform(domain[0], domain[1])]
1010
bound.sort()
@@ -17,7 +17,7 @@ def root(function, bias = 0, domain = R, accurancy = 16):
1717
mean = (a + b) / 2
1818
fm = function(mean) - bias
1919
if abs(fm) < error:
20-
return truncate(round(mean, accurancy + 3), accurancy)
20+
return round(mean, accurancy)
2121
if fa * fm < 0:
2222
b = mean
2323
else:

eggdriver/resources/math/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
inf = 1000000
1+
inf = 10 ** 9
22
R = [-inf, inf]
33

44
e = 2.7182818284590452353602874713526624977572470936999595749669676277240766303

eggdriver/resources/math/polynomial.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
from eggdriver.resources.math.linear import Vector, dualExpand
2+
from eggdriver.resources.math.algorithms.solver import solve
23

3-
def x_(i):
4-
if i==0 :
4+
def x_(i, variable = "x"):
5+
if i == 0 :
56
return ""
67
elif i == 1:
7-
return "x"
8-
return f"x^{i}"
8+
return variable
9+
return f"{variable}^{i}"
910

1011
def plusPoly(a, b):
1112
if type(a) != Polynomial:
@@ -43,13 +44,14 @@ def vectorize(poly: str):
4344
pass
4445

4546
class Polynomial(Vector):
46-
def __init__(self, poly = []):
47+
def __init__(self, poly = [], variable = "x"):
4748
if type(poly) != list:
4849
if type(poly) != str:
4950
poly = [poly]
5051
else:
5152
poly = vectorize(poly)
5253
super().__init__(poly)
54+
self.var = variable
5355
def display(self):
5456
result = Polynomial()
5557
nonZeros = 0
@@ -58,9 +60,9 @@ def display(self):
5860
if self[i] > 0:
5961
nonZeros += 1
6062
if self[i] > 0 and nonZeros > 1:
61-
result.append("+ " + str(self[i]) + x_(i))
63+
result.append("+ " + str(self[i]) + x_(i, self.var))
6264
elif self[i] != 0:
63-
result.append(str(self[i]) + x_(i))
65+
result.append(str(self[i]) + x_(i, self.var))
6466
text = ""
6567
for i in result:
6668
text += i + " "
@@ -79,4 +81,14 @@ def eval(self, x):
7981
for i in range(0, self.size):
8082
f = self[i]
8183
result += f * (x ** i)
82-
return result
84+
return result
85+
@property
86+
def zeros(self):
87+
return solve(self.eval, degree = self.degree)
88+
@property
89+
def degree(self):
90+
deg = 0
91+
for i in range(0, self.size):
92+
if self[i] != 0:
93+
deg = i
94+
return deg

0 commit comments

Comments
 (0)