Question

For this lab, you must define a class called Polynomial. This class definition must include the following methods: ._init_0-

The_add method This method is called automatically by Python whenever the method call that is made is operator is used on a P

Please answer this in python 3, thank you.

For this lab, you must define a class called Polynomial. This class definition must include the following methods: ._init_0- the initialiser for the class ._str_0- returns a formatted string representation of a Polynomial object add_term) - adds a new term (coefficient and exponent) to the Polynomial .addo-modifies the existing Polynomial by adding another one to it ._add_0-returns a new Polynomial object that is the sum of two polynomials scale) - scales a Polynomial object by multiplying each coefficient by a factor evaluate0 - evaluates a Polynomial object for a given x-value .get_degree0-returns the degree of the Polynomial object collapse) reduces the Polynomial object to a single term Background A polynomial is a mathematical expression that is the sum of a series of terms that each consist of a variable (we will use x) which is taken to a given power and multiplied by a given coefficient. For example, the following are two different polynomials 2x4 +x2- 7x +13 x3-5x21 For this lab you will be defining a class called Polynomial that can be used to represent polynomials in our programs. When a Polynomial object is first created it contains no terms, and thus has the corresponding value 0. You can add terms to a Polynomial object by calling the add_termO method. Each term consists of a coefficient (which should be non-zero) and an exponent. Each exercise in the lab will build progressively on the previous exercise to show how a class is developed
The_add method This method is called automatically by Python whenever the method call that is made is operator is used on a Polynomial object. Strictly speaking, when the expression "a + b appears, the actual a .-add--(b) This method should add the two Polynomial objects together and return a new Polynomial object representing their sum. Note that in this case, neither of the original polynomials should be modified. Adding two polynomials is just a matter of combining all of the terms from each polynomial, and adding the coefficients of any terms that have the same exponent. For example, executing the code p1 -Polynomial() p1.add_term(10, 2) p1.add_term(-5, ) p2 Polynomial() p2.add term(5,-1) p3 p1 + p2 print( p1p1) print ('p2p2) print('p3 -,p3) would produce the output p1 = 10x^2-5 Implement the addmethod and submit the entire Polynomial class For example Test Result p1-Polynomial) 2x4 p1.add_term(2, 4) 2x 4 print(pl) p2 p1 + p1 print(pl) print(p2) 4x 4
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the code below:::

polynomialExpressionPrint A1 class Polynomial: 2e def _init_(self): self.p1 0 4 def _str__(self) if self.p1-0: 6 7 return 0

polynomialExpressionPrint A33 return returnMe 34 def add term(self,a,b): 35 36 37 38 39 def evaluate(self,i): 40 if self.p10:

class Polynomial:
def __init__(self):
self.p1 = 0
def __str__(self):
if self.p1==0:
return "0"
returnMe = ""
first = True
for key in sorted(self.p1,reverse=True):
power = key
printMe = self.p1[key]
if(printMe==0):
continue
elif printMe==1:
if not first:
returnMe +=" + "
elif printMe<0:
returnMe +=" - "
returnMe +=str(-1*printMe)
else:
if not first:
returnMe +=" + "
returnMe +=str(1*printMe)
  
  
if power==1:
returnMe+="X"
elif power==0:
returnMe+=""
else:
returnMe+="X^"+str(power)
first = False
return returnMe
def add_term(self,a,b):
if self.p1 ==0:
self.p1 = {}
self.p1[b] = a
  
def evaluate(self,i):
result = 0
for key in (self.p1):
result += self.p1[key]*pow(i,key)
return result
  
def scale(self,i):
for key in (self.p1):
self.p1[key] = self.p1[key]*i
  
def __add__(self,p):
copyP = Polynomial()
for key in (self.p1):
copyP.add_term(self.p1[key],key)
for key in (p.p1):
if key in copyP.p1:
copyP.p1[key] = copyP.p1[key] + p.p1[key]
else:
copyP.p1[key] = p.p1[key]
return copyP
def main():
p1 = Polynomial()
p1.add_term(10,2)
p1.add_term(-5,0)
p2 = Polynomial()
p2.add_term(5,-1)
p2.add_term(-10,2)
p3 = p1+p2
print("p1 = ",p1)
print("p2 = ",p2)
print("p3 = ",p3)
  
  
  
  
  
main()

Console 3 1 = 10x^2-5 p2 = -10x^2 + 5x^-1

Add a comment
Know the answer?
Add Answer to:
For this lab, you must define a class called Polynomial. This class definition must include the f...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • For this lab, you must define a class called Polynomial. This class definition must include the f...

    please answer this question in python 3 For this lab, you must define a class called Polynomial. This class definition must include the following methods: ._init_0- the initialiser for the class ._str_0- returns a formatted string representation of a Polynomial object add_term) - adds a new term (coefficient and exponent) to the Polynomial .addo-modifies the existing Polynomial by adding another one to it ._add_0-returns a new Polynomial object that is the sum of two polynomials scale) - scales a Polynomial...

  • A polynomial p(x) is an expression in variable x which is in the form axn + bxn-1 + …. + jx + k, where a, b, …, j, k are...

    A polynomial p(x) is an expression in variable x which is in the form axn + bxn-1 + …. + jx + k, where a, b, …, j, k are real numbers, and n is a non-negative integer. n is called the degree of polynomial. Every term in a polynomial consists of a coefficient and an exponent. For example, for the first term axn, a is the coefficient and n is the exponent. This assignment is about representing and computing...

  • Create a class to represent a term in an algebraic expression. As defined here, a term...

    Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g. in the term 4x2, the coefficient is 4 and the exponent 2 in -6x8, the coefficient is -6 and the exponent 8 Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent. Your class...

  • The second lab continues with the Polynomial class from the first lab by adding new methods...

    The second lab continues with the Polynomial class from the first lab by adding new methods for polynomial arithmetic to its source code. (There is no inheritance or polymorphism taking place yet in this lab.) Since the class Polynomial is designed to be immutable, none of the following methods should modify the objects this or other in any way, but return the result of that arithmetic operation as a brand new Polynomial object created inside that method. public Polynomial add(Polynomial...

  • The language is java if you coukd shiwnstep by step on how to get each section

    the language is java if you coukd shiwnstep by step on how to get each section ebapps/blackboard/execute/content/file?cmd-view&content jid _975287 1&course id:_693311 Lab 3 Directions (linked lists) Program #1 1. Show PolynomialADT interface 2. Create the PolyNodeClass with the following methods: default constructor, . overloaded constructor, copy constructor, setCoefficient, setExponent, setNext, getCoefficient, getExponent, getNext 3. Create the PolynomialDataStrucClass with the following methods: defaut constructor, overloaded constructor, copy constructor, isEmpty. setFirstNode. getfirstNode,addPolyNodeFirst (PolyNode is created and set to the end of polynomial),...

  • Polynomial Using LinkedList class of Java Language Description: Implement a polynomial class using a LinkedList defined...

    Polynomial Using LinkedList class of Java Language Description: Implement a polynomial class using a LinkedList defined in Java (1) Define a polynomial that has the following methods for Polynomial a. public Polynomial() POSTCONDITION: Creates a polynomial represents 0 b. public Polynomial(double a0) POSTCONDITION: Creates a polynomial has a single x^0 term with coefficient a0 c. public Polynomial(Polynomial p) POSTCONDITION: Creates a polynomial is the copy of p d. public void add_to_coef(double amount, int exponent) POSTCONDITION: Adds the given amount to...

  • Which of the following versions of the polynomial class implementation provides the most space efficient representation...

    Which of the following versions of the polynomial class implementation provides the most space efficient representation for polynomials with a large degree and very few terms, for example: 10x+ x200. Recall that the degree of the polynomial is the largest exponent of the term with a non-zero coefficient. SELECT THE SINGLE BEST ANSWER A static array-based implementation, where only the coefficients are stored in the array, and the exponent of each term is the index of the coefficient in the...

  • Write a Python class called polynomial that represents a polynomial in the following fashion, The initializer...

    Write a Python class called polynomial that represents a polynomial in the following fashion, The initializer of your polynomial class will be designed to take any number of numeric coefficients, The coefficient with the highest power will be the first coefficient. The last coefficient will be the constant value, that is, the value that is associated with . power 0. • Therefore, the number of the coefficients specified will directly indicate the highest power of the polynomial to be represented....

  • in C++ as Exercise P12.14. Write a class Polynomial that stores a polynomial such p(x) =...

    in C++ as Exercise P12.14. Write a class Polynomial that stores a polynomial such p(x) = 5x10 + 9x7-x-10 as a linked list of terms. A term contains the coefficient and the example, you would store p(x) as power of x. For (5,10),(9,7),(-1,1),(一10,0) Supply member functions to add, multiply, and print polynomials. Supply a com- structor that makes a polynomial from a single term. For example, the polynomia; can be constructed as Polynomial p(Term(-10, 0)); p.add (Polynomial(Term(-1, 1))); p.add (Polynomial(Term(9,...

  • Its a report . Implement and test a preliminary version of the polynomial class, using a...

    Its a report . Implement and test a preliminary version of the polynomial class, using a small array to store the coefficients of the polynomial in order of increasing exponents, following the design pattern of object-oriented approach. Suppose the range of the exponents is in [0, 30). • Use an array as a private member variable. • insert(), adding a new term on specific exponent. • remove(), deleting a term. • Have a add() method, deciding the parameters and the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT