From b540a55a1a6561f9f0158da2ddea37d514cfe083 Mon Sep 17 00:00:00 2001 From: Sirin Puenggun Date: Mon, 12 Dec 2022 05:28:34 +0700 Subject: [PATCH] Fix __pow__, to_str(), solve --- nessesary/polynomial.py | 258 +++++++++++++++++++++++++--------------- 1 file changed, 160 insertions(+), 98 deletions(-) diff --git a/nessesary/polynomial.py b/nessesary/polynomial.py index a46c3ae..066548c 100644 --- a/nessesary/polynomial.py +++ b/nessesary/polynomial.py @@ -22,9 +22,6 @@ class Polynomial: except: raise ValueError("Can't turn all number into fraction.") - while len(self.coeff) < 3: - self.coeff.append(0) - self.degree = len(self.coeff) #[degree0, degree1, degree2] @@ -83,16 +80,16 @@ class Polynomial: x^2+2x+1 """ 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 other_index in range(other.degree): result[self_index+other_index] += self.coeff[self_index]*other.coeff[other_index] poly_result = Polynomial(result) return poly_result 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): - result[self_index+other_index] += self.coeff[self_index]*other + result[self_index] += self.coeff[self_index]*other poly_result = Polynomial(result) return poly_result @@ -105,18 +102,15 @@ class Polynomial: >>> str(p1 ** 2) x^2+2x+1 """ - result = Polynomial([0]) + result = Polynomial([1]) if isinstance(other, int) and other >= 0: if other == 0: return Polynomial([1]) else: for i in range(other): - result += self * self + result = result * self return result - def __truediv__(self): - pass - def to_str(self) -> str: """ convert coefficient list into string. @@ -143,53 +137,119 @@ class Polynomial: # return "+".join(result) # else: # return "0" - last_count = len(str(self.coeff[-1])) - result = [] + # last_count = len(str(self.coeff[-1])) + # 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 - 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 - - if result[0] == "+" or result[0] == "-": - del result[0] - result = "".join(result) - - if self.coeff[-1] > 0: - return result[last_count:] - elif self.coeff[-1] < 0: - return result[last_count-1:] + result = '' + if len(self.coeff) >= 3: + while i >= 0: + coe = self.coeff[i] + if coe == 0: + i -= 1 + continue + + if i == len(self.coeff) - 1: + if coe == 1: + result += f'x^{i}' + i -= 1 + continue + elif coe == -1: + result += f'-x^{i}' + i -= 1 + continue + elif (coe != 1) and (coe > 0): + result += f'{coe}x^{i}' + i -= 1 + continue + elif (coe != 1) and (coe < 0): + result += f'{coe}x^{i}' + i -= 1 + continue + else: + if coe == 1: + result += f'+x^{i}' + i -= 1 + continue + elif coe == -1: + result += f'-x^{i}' + i -= 1 + continue + elif (coe != 1) and (coe > 0): + result += f'+{coe}x^{i}' + i -= 1 + continue + elif (coe != 1) and (coe < 0): + result += f'{coe}x^{i}' + i -= 1 + continue 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: degree = len(self.coeff) - 1 sol = [{"real":0, "imag":0}, {"real":0, "imag":0}] - if degree == 2: - a, b, c = self.coeff[2], self.coeff[1], self.coeff[0] + if degree <= 2: + 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 b == 0: if c == 0: @@ -200,7 +260,7 @@ class Polynomial: return sol else: sol[0]["real"] = -c/b - del sol[1] + sol.pop() return sol else: root_term = b**2-4*a*c @@ -222,62 +282,64 @@ class Polynomial: sol[1]["real"] = (-b+(root_term)**0.5)/(2*a) return sol - elif degree == 3: - a, b = self.coeff[3], self.coeff[2] - c, d = self.coeff[1], self.coeff[0] + elif degree >= 3: + raise ValueError + # 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_r = (-(27.0*d)+b*(9*c-2.0*(b * b)))/54.0 - first_term = b/3 - temp_check = (temp_q**3)+(temp_r**2) - sol = [{"real":0, "imag":0},{"real":0, "imag":0},{"real":0, "imag":0}] + # temp_q = 3.0*c-(b*b)/9.0 + # temp_r = (-(27.0*d)+b*(9*c-2.0*(b * b)))/54.0 + # first_term = b/3 + # temp_check = (temp_q**3)+(temp_r**2) + # sol = [{"real":0, "imag":0},{"real":0, "imag":0},{"real":0, "imag":0}] - if temp_check > 0: - temp = 1/3 - i = temp_r + (temp_check**0.5) - if i < 0: - i = -(-i**(temp)) - else: - i = i**temp - j = temp_r - (temp_check)**0.5 - if j < 0: - j = -(-j**(temp)) - else: - j = j**temp + # if temp_check > 0: + # temp = 1/3 + # i = temp_r + (temp_check**0.5) + # if i < 0: + # i = -(-i**(temp)) + # else: + # i = i**temp + # j = temp_r - (temp_check)**0.5 + # if j < 0: + # j = -(-j**(temp)) + # else: + # j = j**temp - sol[0]["real"] = -first_term + i + j - sol[2]["real"] = -(first_term+((i+j)/2)) - sol[1]["real"] = (first_term+((i+j)/2)) - sol[1]["imag"] = (3**0.5) * (-i+j)/2 - sol[2]["imag"] = -sol[1]["imag"] + # sol[0]["real"] = -first_term + i + j + # sol[2]["real"] = -(first_term+((i+j)/2)) + # sol[1]["real"] = (first_term+((i+j)/2)) + # sol[1]["imag"] = (3**0.5) * (-i+j)/2 + # sol[2]["imag"] = -sol[1]["imag"] - return sol + # return sol - elif temp_check == 0: - if temp_r < 0: - new_r = (-temp_r)**(1/3) - else: - new_r = temp_r**(1/3) + # elif temp_check == 0: + # if temp_r < 0: + # new_r = (-temp_r)**(1/3) + # else: + # new_r = temp_r**(1/3) - sol[0]["real"] = -first_term+2*new_r - sol[1]["real"] = -(new_r+first_term) - sol[2]["real"] = sol[1]["real"] + # sol[0]["real"] = -first_term+2*new_r + # sol[1]["real"] = -(new_r+first_term) + # sol[2]["real"] = sol[1]["real"] - return sol + # return sol - else: - temp2 = acos(temp_r/(-temp_q*-temp_q*-temp_q)**0.5) - temp = -first_term + 2*temp_q**0.5 + # else: + # temp2 = acos(temp_r/(-temp_q*-temp_q*-temp_q)**0.5) + # temp = -first_term + 2*temp_q**0.5 - sol[0]["real"] = temp*cos(temp2/3) - sol[1]["real"] = temp*cos((temp2+2*pi)/3) - sol[2]["real"] = temp*cos((temp2+4*pi)/3) - return sol + # sol[0]["real"] = temp*cos(temp2/3) + # sol[1]["real"] = temp*cos((temp2+2*pi)/3) + # sol[2]["real"] = temp*cos((temp2+4*pi)/3) + # return sol - elif degree == 4: - pass + # elif degree == 4: + # pass def __str__(self) -> str: return self.to_str()