#----------------------------------------------------------------------
# Code to implement polynomials as lists of coefficients, where the
# exponent is the index into the list. For a polynomial L, L[3]
# contains the coefficient for X^3, for example.
#
# See pages 272-275 of the Companion.
#
# Copyright (C) April 19, 2017 -- Dr. William T. Verts
#----------------------------------------------------------------------
Poly1 = [-6, 7.5, 0, 2, -3, -4]
Poly2 = [3, 2.5, 0, 1]
#----------------------------------------------------------------------
# Strip out leading-zero coefficients, and convert floats that are
# close to integers into true integers.
#----------------------------------------------------------------------
def polyNormalize (P):
Q = [0] * max(1,len(P))
for I in range(len(P)):
Term = P[I]
if (abs(Term) - float(int(abs(Term))) < 1.0E-14):
Q[I] = int(Term)
else:
Q[I] = Term
while (len(Q) > 1) and (Q[-1] == 0): del Q[-1]
return Q
#----------------------------------------------------------------------
# Basic math operations on polynomials
#----------------------------------------------------------------------
def polyAdd (P1,P2,P3=[]):
Q = [0] * max(len(P1),len(P2),len(P3))
for I in range(len(P1)): Q[I] = Q[I] + P1[I]
for I in range(len(P2)): Q[I] = Q[I] + P2[I]
for I in range(len(P3)): Q[I] = Q[I] + P3[I]
return polyNormalize(Q)
def polySubtract (P1,P2):
Q = [0] * max(len(P1),len(P2))
for I in range(len(P1)): Q[I] = Q[I] + P1[I]
for I in range(len(P2)): Q[I] = Q[I] - P2[I]
return polyNormalize(Q)
def polyMultiply (P1,P2):
Q = [0] * max(1,len(P1) + len(P2))
for I in range(len(P1)):
for J in range(len(P2)):
Q[I+J] = Q[I+J] + P1[I] * P2[J]
return polyNormalize(Q)
def polyDifferentiate (P):
Q = [0] * max(1,len(P)-1)
for I in range(1,len(P)): Q[I-1] = I * P[I]
return polyNormalize(Q)
def polyIntegrate (P, C=0):
Q = [0] * (len(P) + 1)
Q[0] = C
for I in range(len(P)): Q[I+1] = float(P[I]) / float(I+1)
return polyNormalize(Q)
#----------------------------------------------------------------------
# Convert a polynomial into a string that looks like a math equation.
#----------------------------------------------------------------------
def polyStr(P, HTML=False):
S = ""
for Exponent in range(len(P)-1,-1,-1):
Coefficient = P[Exponent]
if (Coefficient != 0):
if (Coefficient < 0):
if (S != ""):
S = S + " - "
else:
S = S + "-"
else:
if (S != ""):
S = S + " + "
if (Exponent == 0) or (abs(Coefficient) != 1):
S = S + str(abs(Coefficient))
if (Exponent >= 1):
if HTML:
S = S + "x"
else:
S = S + "X"
if (Exponent > 1):
if HTML:
S = S + "" + str(Exponent) + ""
else:
S = S + "^" + str(Exponent)
return S
#----------------------------------------------------------------------
# Test frame
#----------------------------------------------------------------------
def Main():
HTML = (requestIntegerInRange("Enter 0 for normal text, 1 for HTML code", 0, 1))
print "Poly1 = ", polyStr(Poly1, HTML)
print "Poly2 = ", polyStr(Poly2, HTML)
print "Poly1 + Poly2 = ", polyStr(polyAdd(Poly1,Poly2), HTML)
print "Poly1 - Poly2 = ", polyStr(polySubtract(Poly1,Poly2), HTML)
print "Poly1 * Poly2 = ", polyStr(polyMultiply(Poly1,Poly2), HTML)
print "d/dx(Poly1) = ", polyStr(polyDifferentiate(Poly1), HTML)
print "Integrate(Poly2) = ", polyStr(polyIntegrate(Poly2), HTML)
return