From c7887b71f9293ca25a80f7a0b57b2e0312eca727 Mon Sep 17 00:00:00 2001 From: Sirin Puenggun Date: Sat, 10 Dec 2022 17:40:16 +0700 Subject: [PATCH] Add basic polynomial func and array to string func --- nessesary/polynomial.py | 85 +++++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 3 deletions(-) diff --git a/nessesary/polynomial.py b/nessesary/polynomial.py index 4860cc0..5d00013 100644 --- a/nessesary/polynomial.py +++ b/nessesary/polynomial.py @@ -1,6 +1,85 @@ -from parser.parser import polynomial_parse +from parser.parser import parse_poly class Polynomial: def __init__(self, poly): - self.coeff = polynomial_parse(poly) - self.degree = len(polynomial_parse(poly)) - 1 \ No newline at end of file + if isinstance(poly, str): + self.coeff = parse_poly(poly) + self.degree = len(parse_poly(poly)) - 1 + self.string_form = poly + elif isinstance(poly, list): + self.coeff = poly + self.degree = len(poly) - 1 + self.string_form = self.to_str() + else: + raise ValueError + + #[degree0, degree1, degree2] + def __add__(self, other): + """ + >>> p1 = Polynomial([1, 1, 1, 1, 1]) + >>> p2 = Polynomial([1, 1, 1, 1, 1]) + >>> p3 = p1 + p2 + >>> p3 + 2x^4+2x^3+2x^2+2x+2 + """ + if isinstance(other, Polynomial): + for degree_index in range(self.degree): + self.coeff[degree_index] += other.coeff[degree_index] + if isinstance(other, int) or isinstance(other, float): + pass + + + def __sub__(self, other): + """ + >>> p1 = Polynomial([1, 1, 1, 1, 1]) + >>> p2 = Polynomial([1, 1, 1, 1, 1]) + >>> p3 = p1 - p2 + >>> p3 + 0 + """ + if isinstance(other, Polynomial): + for degree_index in range(self.degree): + self.coeff[degree_index] -= other.coeff[degree_index] + + def __mul__(self, other): + if isinstance(other, Polynomial): + result = [0 for i in self.degree + other.degree] + 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] + + def __pow__(self): + pass + + def __truediv__(self): + pass + + def to_str(self): + """ + convert coefficient list into string. + >>> p1 = Polynomial([1, 1, 1, 1, 1]) + >>> p1.to_str() + x^4+x^3+x^2+x+1 + >>> p2 = Polynomial([-2, 4, 0, -1, 2]) + >>> p2.to_str() + 2x^4-x^3+4x-2 + """ + result = [] + for ind in range(len(self.coeff)): + if self.coeff[ind]: + if ind == 0: + temp = "" + elif ind == 1: + temp = "x" + else: + temp = "x^"+str(ind) + result.append(str(self.coeff[ind])+temp) + + if result: + result.reverse() + return " + ".join(result) + else: + return "0" + + def __str__(self) -> str: + return self.string_form \ No newline at end of file