Python Programming Problem 2. Polynomials Design and implement (in file p2.py) a
ID: 3863226 • Letter: P
Question
Python Programming
Problem 2. Polynomials Design and implement (in file p2.py) a class called Poly for representing and operating with polynomials. A polynomial in variable x of degree n20 has this general expression: n-1 P(X) n-1 with coefficients akEIR and a, 0 Find out more about polynomials at http:llwww.mathplanet.com/education/algebra-1factoring-and-polynomialslmonomials-and polynomials or at https:LAwww.math.auckland.ac.nzlclass255408slL01s24polynomials.pdf a) The Poly class must support the following operations, illustrated with examples below: Initialization, with the constructor taking an iterable (like a list ao a a, l) that generates the coefficients, starting with ao. The coefficients are floats or ints, but the constructor must convert them to type float. The degree of the polynomial is given by the length of the sequence of the sequence. Conversion to string Str Skip terms aXK with coefficient ak 0. Use the default number of decimals for floats. Representation, for printing at the terminal Lrepr Indexing. This operation takes parameter k and returns the coefficient ak if 0K kKan or throws Value Error otherwise. If p is a Poly object plk] returns ak.Cgetitem Addition with another Poly object Ladd Multiplication with another Poly object and with a float or an int. Cmul and rmul) Testing for equality Ceq, ne Two polynomials are equal if their coefficients are respectively equal. Equal polynomials must be of the same degree. r Evaluation of the polynomial for a given value x for variable X. The method is called eval o if x is an int or float then p eval(x) returns the value of expression a, X o if x is a sequence of elements xo, xi,... (an iterable, such as a tuple or a list), then p eval(x) returns a list with the matching elements [self eval(xo), self eval(x1), 1. Use a list comprehensions for this evaluation b) Write in file p2.py a function called test poly that tests all operations and methods from part a). Use the function testif from Homework 3 or something similar. Here are examples how Poly objects could be used from the Python shell:Explanation / Answer
from operator import add
class Polynomial(object):
def __init__(self, coeffs):
self.degree = len(coeffs) - 1
self.rep = self.__str(coeffs)
self.coefficients = coeffs
def __repr__(self):
return self.rep
def __str(self, coeffs):
terms = [' + ' + str(coeffs[k]) + 'x^'
+ str(k)
for k in range(0, self.degree + 1)
if coeffs[k] <> 0]
if len(terms) == 0:
return str(0)
p = reduce(add, terms)
return p.lstrip(' + ')
def __add(self, other):
d = max(self.degree, other.degree) + 1
self_temp = self.coefficients + [0]*(d-self.degree-1)
other_temp = other.coefficients + [0]*(d-other.degree-1)
return Polynomial(map(add, self_temp, other_temp))
def __mul(self, other):
n = self.degree + other.degree
prod_coeffs = [0]*(n+1)
for i in range(0, self.degree + 1):
for j in range(0, other.degree + 1):
prod_coeffs[i+j] += self.coefficients[i] * other.coefficients[j]
return Polynomial(prod_coeffs)
def __eq__(self,other):
for i in range(0, self.degree + 1):
if (self.coefficients[i] != other.other.coefficients[i]):
return False
return True
def __nq__(self,other):
for i in range(0, self.degree + 1):
if (self.coefficients[i] != other.other.coefficients[i]):
return True
return False
def eval(self, x):
n = 0
for i in range(0, self.degree + 1):
n += self.coefficients[i]*(x**i)
return n
def __getitem(self, key):
return self.coefficients[key]
def __getitem__(self, key):
return self.__getitem[key]
def __add__(self, other):
return self.__add(other)
def __mul__(self, other):
return self.__mul(other)
def __call__(self, val):
sum = 0
return reduce(add, [self.coefficients[i]*(val**i)
for i in range(self.degree + 1)])
#
p = Polynomial([1,2,3])
q = Polynomial([3,1,2,3])
print "p = "+str(p)
print "q = "+str(q)
r = p *q
print "P*Q = "+str(r)
r = p=q
print "P+Q = "+str(r)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.