Question

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

The collapse method The collapse() method reduces a Polynomial object to a single term. It does this by adding together all o

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 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 collapse method The collapse() method reduces a Polynomial object to a single term. It does this by adding together all of the coefficients to produce a coefficient sum, and adding together all of the exponents to produce an exponent sum. The single term then just consists of the coefficient sum and the exponent sum. For example p1- Polynomial( pl.add_term(1, 10) p1.add_term(2, 0) pl.add term (3, -1) p1.add term(4, -2) p1.add_term(5, -3) p1.collapse() print( 'p1-,p1) would produce the output: pl-15x4 Implement the collapse method and submit the entire Polynomial class For example Test Result p1 - Polynomial) 4x4 pl.add_term(2, 4) p1.add (pl) p1.collapse() print (pl)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

# Coefficient and exponent will be maintained as list of tuples [ (coeff,expo)] in terms list
class Polynomial():
    def __init__(self):
        self.terms = [] 
        
    def __str__(self):
        if self.terms == []: # If there are no terms in the list
            return '0'
        else:
            s = ""
            for tup in self.terms: # Traversing over list of coeff and expo and returing polynomial
                c,e = tup
                if c>0:
                    s+="+"+str(c)+"x^"+str(e)
                else:
                    s+=str(c)+"x^"+str(e)
            return s
        
    def add_term(self,c,e): # Adding term to the given polynomial
        self.terms.append((c,e))
    
    def evaluate(self,x):
        ans = 0
        for c,e in self.terms: # Performing evaluation for given value of x
            ans += c*pow(x,e)
        return ans
    
    def collapse(self):
        netc,nete = 0,0
        for c,e in self.terms: 
            netc+=c
            nete+=e
        self.terms = [(netc,nete)]
    
    def scale(self,factor):
        newlist = []
        for c,e in self.terms: 
            netc=c*factor
            newlist.append((netc,e))
        self.terms = newlist[:]
        
    def get_degree(self):
        deg = -100
        for c,e in self.terms:
            if deg < e:
                deg = e
        return deg
    
p1 = Polynomial()
p1.add_term(2,4)
p1.add_term(1,2)
p1.add_term(-7,1)
p1.add_term(13,0)
print("p1 =",p1)
print("Evaluating with value 2 =",p1.evaluate(2))
p1.scale(2)
print("Scaling with value 2 = ",p1)
print("Degree of the polynomial is = ",p1.get_degree())
p1.collapse()
print("Collapsing polynomial ",p1)

OUTPUT:

class Polynomial): def init(self): self.terms[] def _str_(self): if self.terms[] return e else for tup in self.terms: c,e-tu

def get_degree(self) deg = -100 for c,e in self.terms: if deg 〈 e: deg = e return deg p Polynomial() p1.add_term(2,4) p1.add_

NOTE:

Explain the difference between add() and __add__() functions, so that I can also add them.

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 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...

  • 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...

  • 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...

  • 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...

  • 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...

  • 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...

  • 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),...

  • 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,...

  • In Java. Needing help with this homework assignment. The following is the class I need to...

    In Java. Needing help with this homework assignment. The following is the class I need to write up and implement. two maps. P15.3 Write a class Polynomial that stores a polynomial such as f(x) = 5x10 + 9x7-x-10 as a linked list of terms. A term contains the coefficient and the power of x. For example, you would store p(x) as (5,10),(9,7),(-1,1),(?10,0) Supply methods to add, multiply, and print polynomials. Supply a constructor that makes a polynomial from a single...

  • The polynomial addition C function of Program 2.6 padd is the code when the polynomial is used to...

    The polynomial addition C function of Program 2.6 padd is the code when the polynomial is used to arrange the polynomial in the two arrangement methods of the polynomial described in the text 2.4.2. For the remaining method, when the expression polynomial is arranged by a coefficient, create a polynomial addition C function padd() corresponding to Program 2.6. 66 Arrays And Structures are zero are not displayed. The term with exponent equal to zero does not shouw able since x...

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