mirror of
https://github.com/Sosokker/Calculator-for-Matrix-and-Algebra.git
synced 2025-12-18 20:54:05 +01:00
Fix __pow__, to_str(), solve
This commit is contained in:
parent
84c1d8a357
commit
b540a55a1a
@ -22,9 +22,6 @@ class Polynomial:
|
|||||||
except:
|
except:
|
||||||
raise ValueError("Can't turn all number into fraction.")
|
raise ValueError("Can't turn all number into fraction.")
|
||||||
|
|
||||||
while len(self.coeff) < 3:
|
|
||||||
self.coeff.append(0)
|
|
||||||
|
|
||||||
self.degree = len(self.coeff)
|
self.degree = len(self.coeff)
|
||||||
|
|
||||||
#[degree0, degree1, degree2]
|
#[degree0, degree1, degree2]
|
||||||
@ -83,16 +80,16 @@ class Polynomial:
|
|||||||
x^2+2x+1
|
x^2+2x+1
|
||||||
"""
|
"""
|
||||||
if isinstance(other, Polynomial):
|
if isinstance(other, Polynomial):
|
||||||
result = [0 for i in self.degree + other.degree]
|
result = [0 for i in range(self.degree + other.degree +1)]
|
||||||
for self_index in range(self.degree):
|
for self_index in range(self.degree):
|
||||||
for other_index in range(other.degree):
|
for other_index in range(other.degree):
|
||||||
result[self_index+other_index] += self.coeff[self_index]*other.coeff[other_index]
|
result[self_index+other_index] += self.coeff[self_index]*other.coeff[other_index]
|
||||||
poly_result = Polynomial(result)
|
poly_result = Polynomial(result)
|
||||||
return poly_result
|
return poly_result
|
||||||
elif isinstance(other, int) or isinstance(other, float):
|
elif isinstance(other, int) or isinstance(other, float):
|
||||||
result = [0 for i in self.degree + other.degree]
|
result = [0 for i in (range(self.degree))]
|
||||||
for self_index in range(self.degree):
|
for self_index in range(self.degree):
|
||||||
result[self_index+other_index] += self.coeff[self_index]*other
|
result[self_index] += self.coeff[self_index]*other
|
||||||
poly_result = Polynomial(result)
|
poly_result = Polynomial(result)
|
||||||
return poly_result
|
return poly_result
|
||||||
|
|
||||||
@ -105,18 +102,15 @@ class Polynomial:
|
|||||||
>>> str(p1 ** 2)
|
>>> str(p1 ** 2)
|
||||||
x^2+2x+1
|
x^2+2x+1
|
||||||
"""
|
"""
|
||||||
result = Polynomial([0])
|
result = Polynomial([1])
|
||||||
if isinstance(other, int) and other >= 0:
|
if isinstance(other, int) and other >= 0:
|
||||||
if other == 0:
|
if other == 0:
|
||||||
return Polynomial([1])
|
return Polynomial([1])
|
||||||
else:
|
else:
|
||||||
for i in range(other):
|
for i in range(other):
|
||||||
result += self * self
|
result = result * self
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def __truediv__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def to_str(self) -> str:
|
def to_str(self) -> str:
|
||||||
"""
|
"""
|
||||||
convert coefficient list into string.
|
convert coefficient list into string.
|
||||||
@ -143,53 +137,119 @@ class Polynomial:
|
|||||||
# return "+".join(result)
|
# return "+".join(result)
|
||||||
# else:
|
# else:
|
||||||
# return "0"
|
# return "0"
|
||||||
last_count = len(str(self.coeff[-1]))
|
# last_count = len(str(self.coeff[-1]))
|
||||||
result = []
|
# result = []
|
||||||
|
# i = len(self.coeff) - 1
|
||||||
|
# while i >= 0:
|
||||||
|
# if self.coeff[i] != 0:
|
||||||
|
# if self.coeff[i] < 0:
|
||||||
|
# result.append("-")
|
||||||
|
|
||||||
|
# elif len(self.coeff) != 0:
|
||||||
|
# result.append("+")
|
||||||
|
|
||||||
|
# elif self.coeff[i] == 0:
|
||||||
|
# i -= 1
|
||||||
|
# continue
|
||||||
|
|
||||||
|
# if self.coeff[i] != 1 and self.coeff[i] != -1:
|
||||||
|
# result.append(str(abs(self.coeff[i])))
|
||||||
|
|
||||||
|
# if (i == len(self.coeff) - 1):
|
||||||
|
# result.append(str(self.coeff[i]))
|
||||||
|
|
||||||
|
# if (self.coeff[i] == 1) and (i != len(self.coeff) - 1):
|
||||||
|
# result.append(str(1))
|
||||||
|
|
||||||
|
# if i == 1:
|
||||||
|
# result.append("x")
|
||||||
|
|
||||||
|
# if i > 1:
|
||||||
|
# result.append("x^"+str(i))
|
||||||
|
# i -= 1
|
||||||
|
# print(result)
|
||||||
|
# if result[0] == "+" or result[0] == "-":
|
||||||
|
# del result[0]
|
||||||
|
# result = "".join(result)
|
||||||
|
# return result
|
||||||
|
|
||||||
i = len(self.coeff) - 1
|
i = len(self.coeff) - 1
|
||||||
while i >= 0:
|
result = ''
|
||||||
if self.coeff[i] != 0:
|
if len(self.coeff) >= 3:
|
||||||
if self.coeff[i] < 0:
|
while i >= 0:
|
||||||
result.append("-")
|
coe = self.coeff[i]
|
||||||
|
if coe == 0:
|
||||||
elif len(self.coeff) != 0:
|
i -= 1
|
||||||
result.append("+")
|
continue
|
||||||
|
|
||||||
elif self.coeff[i] == 0:
|
if i == len(self.coeff) - 1:
|
||||||
i -= 1
|
if coe == 1:
|
||||||
continue
|
result += f'x^{i}'
|
||||||
|
i -= 1
|
||||||
if self.coeff[i] != 1 and self.coeff[i] != -1:
|
continue
|
||||||
result.append(str(abs(self.coeff[i])))
|
elif coe == -1:
|
||||||
|
result += f'-x^{i}'
|
||||||
if (i == len(self.coeff) - 1):
|
i -= 1
|
||||||
result.append(str(self.coeff[i]))
|
continue
|
||||||
|
elif (coe != 1) and (coe > 0):
|
||||||
if (self.coeff[i] == 1) and (i != len(self.coeff) - 1):
|
result += f'{coe}x^{i}'
|
||||||
result.append(str(1))
|
i -= 1
|
||||||
|
continue
|
||||||
if i == 1:
|
elif (coe != 1) and (coe < 0):
|
||||||
result.append("x")
|
result += f'{coe}x^{i}'
|
||||||
|
i -= 1
|
||||||
if i > 1:
|
continue
|
||||||
result.append("x^"+str(i))
|
else:
|
||||||
i -= 1
|
if coe == 1:
|
||||||
|
result += f'+x^{i}'
|
||||||
if result[0] == "+" or result[0] == "-":
|
i -= 1
|
||||||
del result[0]
|
continue
|
||||||
result = "".join(result)
|
elif coe == -1:
|
||||||
|
result += f'-x^{i}'
|
||||||
if self.coeff[-1] > 0:
|
i -= 1
|
||||||
return result[last_count:]
|
continue
|
||||||
elif self.coeff[-1] < 0:
|
elif (coe != 1) and (coe > 0):
|
||||||
return result[last_count-1:]
|
result += f'+{coe}x^{i}'
|
||||||
|
i -= 1
|
||||||
|
continue
|
||||||
|
elif (coe != 1) and (coe < 0):
|
||||||
|
result += f'{coe}x^{i}'
|
||||||
|
i -= 1
|
||||||
|
continue
|
||||||
else:
|
else:
|
||||||
return result
|
if len(self.coeff) == 2:
|
||||||
|
result = f'{self.coeff[1]}x'
|
||||||
|
if self.coeff[0] > 0:
|
||||||
|
result += f'+{self.coeff[0]}'
|
||||||
|
elif self.coeff[0] < 0:
|
||||||
|
result += f'-{self.coeff[0]}'
|
||||||
|
elif self.coeff[0] == 0:
|
||||||
|
pass
|
||||||
|
return result
|
||||||
|
elif len(self.coeff) == 1:
|
||||||
|
return f'{self.coeff[0]}'
|
||||||
|
|
||||||
|
if result[0] == "+":
|
||||||
|
result = result[1:]
|
||||||
|
return result
|
||||||
|
|
||||||
def solve(self) -> dict:
|
def solve(self) -> dict:
|
||||||
degree = len(self.coeff) - 1
|
degree = len(self.coeff) - 1
|
||||||
sol = [{"real":0, "imag":0}, {"real":0, "imag":0}]
|
sol = [{"real":0, "imag":0}, {"real":0, "imag":0}]
|
||||||
if degree == 2:
|
if degree <= 2:
|
||||||
a, b, c = self.coeff[2], self.coeff[1], self.coeff[0]
|
try:
|
||||||
|
a = self.coeff[2]
|
||||||
|
except :
|
||||||
|
a = 0
|
||||||
|
try:
|
||||||
|
b = self.coeff[1]
|
||||||
|
except :
|
||||||
|
b = 0
|
||||||
|
try:
|
||||||
|
c = self.coeff[0]
|
||||||
|
except :
|
||||||
|
c = 0
|
||||||
|
|
||||||
if a == 0:
|
if a == 0:
|
||||||
if b == 0:
|
if b == 0:
|
||||||
if c == 0:
|
if c == 0:
|
||||||
@ -200,7 +260,7 @@ class Polynomial:
|
|||||||
return sol
|
return sol
|
||||||
else:
|
else:
|
||||||
sol[0]["real"] = -c/b
|
sol[0]["real"] = -c/b
|
||||||
del sol[1]
|
sol.pop()
|
||||||
return sol
|
return sol
|
||||||
else:
|
else:
|
||||||
root_term = b**2-4*a*c
|
root_term = b**2-4*a*c
|
||||||
@ -222,62 +282,64 @@ class Polynomial:
|
|||||||
sol[1]["real"] = (-b+(root_term)**0.5)/(2*a)
|
sol[1]["real"] = (-b+(root_term)**0.5)/(2*a)
|
||||||
return sol
|
return sol
|
||||||
|
|
||||||
elif degree == 3:
|
elif degree >= 3:
|
||||||
a, b = self.coeff[3], self.coeff[2]
|
raise ValueError
|
||||||
c, d = self.coeff[1], self.coeff[0]
|
# elif degree == 3:
|
||||||
|
# a, b = self.coeff[3], self.coeff[2]
|
||||||
|
# c, d = self.coeff[1], self.coeff[0]
|
||||||
|
|
||||||
b, c, d = b/a, c/a, d/a
|
# b, c, d = b/a, c/a, d/a
|
||||||
|
|
||||||
temp_q = 3.0*c-(b*b)/9.0
|
# temp_q = 3.0*c-(b*b)/9.0
|
||||||
temp_r = (-(27.0*d)+b*(9*c-2.0*(b * b)))/54.0
|
# temp_r = (-(27.0*d)+b*(9*c-2.0*(b * b)))/54.0
|
||||||
first_term = b/3
|
# first_term = b/3
|
||||||
temp_check = (temp_q**3)+(temp_r**2)
|
# temp_check = (temp_q**3)+(temp_r**2)
|
||||||
sol = [{"real":0, "imag":0},{"real":0, "imag":0},{"real":0, "imag":0}]
|
# sol = [{"real":0, "imag":0},{"real":0, "imag":0},{"real":0, "imag":0}]
|
||||||
|
|
||||||
if temp_check > 0:
|
# if temp_check > 0:
|
||||||
temp = 1/3
|
# temp = 1/3
|
||||||
i = temp_r + (temp_check**0.5)
|
# i = temp_r + (temp_check**0.5)
|
||||||
if i < 0:
|
# if i < 0:
|
||||||
i = -(-i**(temp))
|
# i = -(-i**(temp))
|
||||||
else:
|
# else:
|
||||||
i = i**temp
|
# i = i**temp
|
||||||
j = temp_r - (temp_check)**0.5
|
# j = temp_r - (temp_check)**0.5
|
||||||
if j < 0:
|
# if j < 0:
|
||||||
j = -(-j**(temp))
|
# j = -(-j**(temp))
|
||||||
else:
|
# else:
|
||||||
j = j**temp
|
# j = j**temp
|
||||||
|
|
||||||
sol[0]["real"] = -first_term + i + j
|
# sol[0]["real"] = -first_term + i + j
|
||||||
sol[2]["real"] = -(first_term+((i+j)/2))
|
# sol[2]["real"] = -(first_term+((i+j)/2))
|
||||||
sol[1]["real"] = (first_term+((i+j)/2))
|
# sol[1]["real"] = (first_term+((i+j)/2))
|
||||||
sol[1]["imag"] = (3**0.5) * (-i+j)/2
|
# sol[1]["imag"] = (3**0.5) * (-i+j)/2
|
||||||
sol[2]["imag"] = -sol[1]["imag"]
|
# sol[2]["imag"] = -sol[1]["imag"]
|
||||||
|
|
||||||
return sol
|
# return sol
|
||||||
|
|
||||||
elif temp_check == 0:
|
# elif temp_check == 0:
|
||||||
if temp_r < 0:
|
# if temp_r < 0:
|
||||||
new_r = (-temp_r)**(1/3)
|
# new_r = (-temp_r)**(1/3)
|
||||||
else:
|
# else:
|
||||||
new_r = temp_r**(1/3)
|
# new_r = temp_r**(1/3)
|
||||||
|
|
||||||
sol[0]["real"] = -first_term+2*new_r
|
# sol[0]["real"] = -first_term+2*new_r
|
||||||
sol[1]["real"] = -(new_r+first_term)
|
# sol[1]["real"] = -(new_r+first_term)
|
||||||
sol[2]["real"] = sol[1]["real"]
|
# sol[2]["real"] = sol[1]["real"]
|
||||||
|
|
||||||
return sol
|
# return sol
|
||||||
|
|
||||||
else:
|
# else:
|
||||||
temp2 = acos(temp_r/(-temp_q*-temp_q*-temp_q)**0.5)
|
# temp2 = acos(temp_r/(-temp_q*-temp_q*-temp_q)**0.5)
|
||||||
temp = -first_term + 2*temp_q**0.5
|
# temp = -first_term + 2*temp_q**0.5
|
||||||
|
|
||||||
sol[0]["real"] = temp*cos(temp2/3)
|
# sol[0]["real"] = temp*cos(temp2/3)
|
||||||
sol[1]["real"] = temp*cos((temp2+2*pi)/3)
|
# sol[1]["real"] = temp*cos((temp2+2*pi)/3)
|
||||||
sol[2]["real"] = temp*cos((temp2+4*pi)/3)
|
# sol[2]["real"] = temp*cos((temp2+4*pi)/3)
|
||||||
return sol
|
# return sol
|
||||||
|
|
||||||
elif degree == 4:
|
# elif degree == 4:
|
||||||
pass
|
# pass
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
return self.to_str()
|
return self.to_str()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user