#---------------------------------------------------------------------- # Program to explore polynomials # # Copyright (C) April 23, 2018 -- Dr. William T. Verts # # A polynomial is a list of coefficients # 4x^3 -3x^2 + 2x -5 would be stored as [-5, 2, -3, 4] # The position of the coefficient is the power of x # # How do you handle cases where the result has extra zeroes in the # high-order coefficients? Such as [4,-2,4,6,1,0,0,0], which # should be "normalized" to [4,-2,4,6,1]. How might you handle # cases where the result is a float but a whole number such as 4.0? # Do you change that to an int or leave it as a float? Finally, # how do you print out a polynomial so it LOOKS like a polynomial? #---------------------------------------------------------------------- def addPoly(P0,P1,P2=[],P3=[]): # P0,P1,P2,P3 are polynomials, returns a polynomial Answer = [0] * max(len(P0), len(P1), len(P2), len(P3)) for I in range(len(P0)): Answer[I] = Answer[I] + P0[I] for I in range(len(P1)): Answer[I] = Answer[I] + P1[I] for I in range(len(P2)): Answer[I] = Answer[I] + P2[I] for I in range(len(P3)): Answer[I] = Answer[I] + P3[I] return Answer def subtractPoly(P0,P1,P2=[],P3=[]): # P0,P1,P2,P3 are polynomials, returns a polynomial Answer = [0] * max(len(P0), len(P1), len(P2), len(P3)) for I in range(len(P0)): Answer[I] = Answer[I] + P0[I] for I in range(len(P1)): Answer[I] = Answer[I] - P1[I] for I in range(len(P2)): Answer[I] = Answer[I] - P2[I] for I in range(len(P3)): Answer[I] = Answer[I] - P3[I] return Answer def multiplyPoly(P0,P1): # P0 and P1 are polynomials, returns a polynomial Length = len(P0) + len(P1) Answer = [0] * Length for I in range(len(P0)): for J in range(len(P1)): Answer[I+J] = Answer[I+J] + P0[I] * P1[J] return Answer def dividePoly(P0,P1): # P0 and P1 are polynomials, returns a polynomial # Division is ugly return def integratePoly(P,C=0): # P is a polynomial, returns a polynomial # But calculus isn't too bad! return def differentiatePoly(P): # P is a polynomial, returns a polynomial # But calculus isn't too bad! return