Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essentials of Database Management

Authors: Jeffrey A. Hoffer, Heikki Topi, Ramesh Venkataraman

1st edition

ISBN: 133405680, 9780133547702 , 978-0133405682

More Books

Students also viewed these Databases questions

Question

Explain the incorporation of doctrine

Answered: 1 week ago

Question

Describe how to measure the quality of work life.

Answered: 1 week ago

Question

What attracts you about this role?

Answered: 1 week ago

Question

How many states in India?

Answered: 1 week ago

Question

What is Change Control and how does it operate?

Answered: 1 week ago

Question

How do Data Requirements relate to Functional Requirements?

Answered: 1 week ago