Polynomial Module
#
# This module contains operations to manipulate polynomials.
#
# Need some string services, and some standard system services.
import string, sys
#
# Function to evaluate a polynomial at x. The polynomial is given
# as a list of coefficients, from the greatest to the least. It returns
# the value of the polynomial at x.
def eval(x, poly):
'''Evaluate at x the polynomial with coefficients given in poly.
The value p(x) is returned.'''
sum = 0
while 1:
sum = sum + poly[0] # Add the next coef.
poly = poly[1:] # Done with that one.
if not poly: break # If no more, done entirely.
sum = sum * x # Mult by x (each coef gets x right num times)
return sum
#
# Function to read a line containing a list of integers and return
# them as a list of integers. If the string conversion fails, it
# returns the empty list. The input 'quit' is special. If that is
# this input, this value is returned. The input comes from the file
# if given, otherwise from standard input. If the prompt is given, it
# is printed first. If reading reaches EOF, or the input line quit,
# the call throws EOFError. If the conversion fails, it throws
# ValueError.
def read(prompt = '', file = sys.stdin):
'''Read a line of integers and return the list of integers.'''
# Read a line
if prompt:
line = input(prompt)
if line == 'quit':
raise EOFError('Input quit on attempt to read polynomial.')
# Go through each item on the line, converting each one and adding it
# to retval.
retval = [ ];
for val in str.split(line):
retval.append(int(val))
return retval
#
# Create a string of the polynomial in sort-of-readable form.
def srep(p):
'''Print the coefficient list as a polynomial.'''
# Get the exponent of first coefficient, plus 1.
exp = len(p)
# Go through the coefs and turn them into terms.
retval = ''
while p:
# Adjust exponent. Done here so continue will run it.
exp = exp - 1
# Strip first coefficient
coef = p[0]
p = p[1:]
# If zero, leave it out.
if coef == 0: continue
# If adding, need a + or -.
if retval:
if coef >= 0:
retval = retval + ' + '
else:
coef = -coef
retval = retval + ' - '
# Add the coefficient, if needed.
if coef != 1 or exp == 0:
retval = retval + str(coef)
if exp != 0: retval = retval + '*'
# Put the x, if we need it.
if exp != 0:
retval = retval + 'x'
if exp != 1: retval = retval + '^' + str(exp)
# For zero, say that.
if not retval: retval = '0'
return retval
This is a Python module which provides the facilities
for the polynomial evaluator.
It is really nothing more than a bunch of Python
code (mostly function definitions) inside a file.
The name of the module is the name of the file, plus
the .py suffix. The module name is not given in the
text of the file, so module can be renamed by simply renaming the file.
After a module has been used, Python will create a
file with the same name and the extension .pyc. This is
the byte code for the module. Python creates or recreates
these as needed, so you really don't need to do anything about them.