Question: Python Design and implement (in file p2.py) a class called Poly for representing and operating with polynomials. A polynomial in variable X of degree n0
Python
Design and implement (in file p2.py) a class called Poly for representing and operating with polynomials. A polynomial in variable X of degree n0 has this general expression: P(X)=a0+a1 X+a2X2++an1Xn1+an Xn with coefficients ak and an0 . Find out more about polynomials at http://www.mathplanet.com/education/algebra-1/factoring-and-polynomials/monomials-andpolynomials or at https://www.math.auckland.ac.nz/class255/08s1/01s2/polynomials.pdf a) The Poly class must support the following operations, illustrated with examples below: Initialization, with the constructor taking an iterable (like a list [ a0, a1,, an ]) that generates the coefficients, starting with a0. 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 akXk with coefficient ak=0. Use the default number of decimals for floats. Representation, for printing at the terminal (__repr__). Indexing. This operation takes parameter k and returns the coefficient ak if 0<=k<=n or throws ValueError otherwise. If p is a Poly object p[k] returns ak. (__getitem__) Addition with another Poly object (__add__). Multiplication with another Poly object and with a float or an int. (__mul__ and __rmul__) Testing for equality (__eq__, __ne__). Two polynomials are equal if their coefficients are respectively equal. Equal polynomials must be of the same degree. Evaluation of the polynomial for a given value x for variable X. The method is called eval : if x is an int or float then p.eval(x) returns the value of expression k=0 n ak xk . if x is a sequence of elements x0, x1, (an iterable, such as a tuple or a list), then p.eval(x) returns a list with the matching elements [self.eval(x0), self.eval(x1), . ]. Use a list comprehensions for this evaluation.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
