Question
Python 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
Python
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 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.
b) Write in file p2.py a function called test_poly that tests all operations and methods from part a)
c) add a method to class Poly called graphit(xseq) that takes a sequence of floats in parameter xseq (such as a list), evaluates with method eval() the polynomial in the xseq points, and then plots the function nicely using the pylab or matplotlib plot() function. Display the coordinate axes and proper labels. Use sufficient points in xseq to make the chart smooth
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started