Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from itertools import zip_longest
- class MyPolynomial:
- def __init__(self, *args):
- if not args:
- args = [0]
- self.__coefs = self._truncate_zeros(*args)
- def _truncate_zeros(self, *args):
- i = len(args)
- while i > 1 and args[i - 1] == 0:
- i -= 1
- return list(args[:i])
- @classmethod
- def from_iterable(cls, iterable):
- return cls(*iterable)
- def __repr__(self):
- return f"{self.__class__.__name__}({', '.join(repr(x) for x in self)})"
- def __str__(self):
- st = ""
- for exp, coef in enumerate(self):
- if coef == 0:
- continue
- if exp == 0:
- st += repr(coef)
- continue
- if coef > 0:
- if coef == 1:
- st += f" + x^{exp}"
- else:
- st += f" + {coef}x^{exp}"
- if coef < 0:
- if coef == -1:
- st += f" - x^{exp}"
- else:
- st += f" - {abs(coef)}x^{exp}"
- return st
- def __getitem__(self, idx):
- return self.__coefs[idx]
- def __len__(self):
- return len(self.__coefs)
- def __eq__(self, other):
- if len(self) != len(other):
- if len(other) == 0 and self[0] == 0:
- return True
- return False
- return all(i == j for i, j in zip(self, other))
- def __call__(self, x):
- return sum([x ** exp * coef for exp, coef in enumerate(self)])
- def __add__(self, other):
- if type(other) != type(self):
- res = self.__coefs
- res[0] += other
- return MyPolynomial(*res)
- res = [sum(t) for t in zip_longest(self, other, fillvalue=0)]
- return MyPolynomial(*res)
- def __iadd__(self, other):
- if type(other) != type(self):
- self.__coefs[0] += other
- return self
- res = [sum(t) for t in zip_longest(self, other, fillvalue=0)]
- self.__coefs = self._truncate_zeros(*res)
- return self
- def __radd__(self, other):
- return self + other
- def __sub__(self, other):
- if type(other) != type(self):
- new_coefs = self.__coefs
- new_coefs[0] -= other
- return MyPolynomial(*new_coefs)
- new_coefs = [t - f for (t, f) in zip_longest(self, other, fillvalue=0)]
- return MyPolynomial(*new_coefs)
- def __isub__(self, other):
- if type(other) != type(self):
- self.__coefs[0] -= other
- return self
- self.__coefs = [t - f for (t, f) in zip_longest(self, other, fillvalue=0)]
- return self
- def __rsub__(self, other):
- new_coefs = [-x for x in self.__coefs]
- new_coefs[0] += other
- return MyPolynomial(*new_coefs)
- def __neg__( self ):
- new_coefs = [-x for x in self.__coefs]
- return MyPolynomial(*new_coefs)
- def _inner_mul(self, other):
- new_len = len(self) + len(other) - 1
- new_coefs = [0] * new_len
- computaion_matrix = [[0] * len(self) for _ in range(len(other))]
- for o_i, o in enumerate(other):
- for s_i, s in enumerate(self):
- computaion_matrix[o_i][s_i] = o * s
- for o_i in range(len(other)):
- for s_i in range(len(self)):
- new_coefs[o_i + s_i] += computaion_matrix[o_i][s_i]
- coefs = self._truncate_zeros(*new_coefs)
- print(coefs)
- return coefs
- def __mul__(self, other):
- if type(other) != type(self):
- new_coefs = [x * other for x in self.__coefs]
- return MyPolynomial(*new_coefs)
- new_coefs = self._inner_mul(other)
- return MyPolynomial(*new_coefs)
- def __imul__(self, other):
- if type(other) != type(self):
- new_coefs = [x * other for x in self.__coefs]
- self.__coefs = self._truncate_zeros(*new_coefs)
- return self
- self.__coefs = self._inner_mul(other)
- return self
- def __rmul__(self, other):
- return self * other
- def degree(self):
- return len(self) - 1
- print(str(-MyPolynomial(3, 4)))
- assert '1 - 2x^1' == str(MyPolynomial(1, -2))
- assert '-1 + x^1 - 2x^2' == str(MyPolynomial(-1, 1, -2))
- assert '1 + 2x^2' == str(MyPolynomial(1, 0, 2))
- assert 'MyPolynomial(-1, -2)' == repr(MyPolynomial(-1, -2))
- assert MyPolynomial(2, 4) == MyPolynomial(5, 4) - 3
- assert MyPolynomial(2, 4) == MyPolynomial(5, 8) - MyPolynomial(3, 4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement