#### Previous topic

Day 1: A First Look at Python

#### Next topic

Homework for Tuesday

# Modeling Polynomials¶

In class we introduced python by modeling polynomials using lists. For example the polynomial $$4 x^5 + 7 x^3 + x^2 + 1$$ can be represented by the list [1,0,1,7,0,4]. By starting with the constant term on the left, and explicitly writing a zero as the coefficient of ‘missing terms’ we can use the list index as the exponent.

## Starting at the Shell¶

One of the nice features of python is that we can sketch out ideas at the shell. Here is an example of a rough print function.

>>> testP = [1,0,1,7,0,4]

>>> testP
[1, 0, 1, 7, 0, 4]
>>> def printPoly1(P):
for i in range(len(P)):
print("%s x^%s + "%(P[i], i)),
print

>>> printPoly1(testP)
1 x^0 +  0 x^1 +  1 x^2 +  7 x^3 +  0 x^4 +  4 x^5 +


We can see that there are several improvements we could make on this output. First, we need a brief detour on using IDLE to write programs.

## Using IDLE’s Editor¶

This video has a good demonstration of using IDLE’s editor to write, save, and run Python code. Although the narrator is using Python 3 (and we use Python 2.7) most of what he says is applicable to our work. The first five minutes of the video demonstrate Python in a different shell. He starts talking about IDLE just before 5:00.

Alternatively, open a new file in IDLE; copy the following code into the new file; and save it as polynomials.py.

"""
polynomials.py

sample program for Math  400

bic     1/28/16

* represent a polynomial by a list
* show examples of functions that print the polynomial
* take the derivative of a polynomial

"""

testP = [1,0,1,7,0,4]

def printPoly1(P):
for i in range(len(P)):
print("%s x^%s + "%(P[i], i)),
print

def printPoly2(P):
for i in range(len(P)):
if P[i] != 0:
print("%s x^%s + "%(P[i], i)),
print

def reverse(L):
R = []
for i in range(len(L)-1,-1,-1):
R.append(L[i])
return(R)

def printPoly3(P):
for i in reverse(range(len(P))):
if P[i] != 0:
print("%s x^%s + "%(P[i], i)),
print

def printPoly4(P):
for i in reverse(range(len(P))):
if P[i] != 0:
if i != 0:
print("%s x^%s + "%(P[i], i)),
else:
print("%s"%(P[i])),
print

def printPoly5(P):
for i in reverse(range(len(P))):
if P[i] != 0:
if i != 0:
if P[i] != 1:
print("%s x^%s + "%(P[i], i)),
else:
print("x^%s + "%(i)),
else:
print("%s"%(P[i])),
print

def derivative(P):
"returns dP/dx"
D=[]
for i in range(1,len(P)):
D.append(P[i]*i)
return(D)

def test(P,v=5):
if v>0:
print("printPoly1(testP) returns:")
printPoly1(P)
if v>1:
print("printPoly2(testP) returns:")
printPoly2(P)
if v>2:
print("printPoly3(testP) returns:")
printPoly3(P)
if v>3:
print("printPoly4(testP) returns:")
printPoly4(P)
if v>4:
print("printPoly5(testP) returns:")
printPoly5(P)

def testDerivative(P):
print("Derivatives:")
printPoly5(P)
for i in range(len(P)):
P = derivative(P)
printPoly5(P)

print(testP)
test(testP)
testDerivative(testP)


Once you have your program saved in the editor, you can run it using the Run Module option in the Run menu. (This is also function key F5.) The shell window should look like this:

>>> ================================ RESTART ================================
>>>
[1, 0, 1, 7, 0, 4]
printPoly1(testP) returns:
1 x^0 +  0 x^1 +  1 x^2 +  7 x^3 +  0 x^4 +  4 x^5 +
printPoly2(testP) returns:
1 x^0 +  1 x^2 +  7 x^3 +  4 x^5 +
printPoly3(testP) returns:
4 x^5 +  7 x^3 +  1 x^2 +  1 x^0 +
printPoly4(testP) returns:
4 x^5 +  7 x^3 +  1 x^2 +  1
printPoly5(testP) returns:
4 x^5 +  7 x^3 +  x^2 +  1
Derivatives:
4 x^5 +  7 x^3 +  x^2 +  1
20 x^4 +  21 x^2 +  2 x^1 +
80 x^3 +  42 x^1 +  2
240 x^2 +  42
480 x^1 +
480

>>>


At the prompt you can use the shell again, but now you have access to the functions you defined in your program. For example:

>>> testP
[1, 0, 1, 7, 0, 4]
>>> len(testP)
6


Let’s write a little code to look at lists and for loops. Copy this code into a new window. This first loop uses i as an index, while the second uses i as a list element.

P = ['a', 2, 2.3]
print("i as an index:")
for i in P:
print i,

print

print("i as an element:")
for i in range(len(P)):
print i,
print
print("show testP:")
print(testP)


Notice that python barks at us about testP.

>>> ================================ RESTART ================================
>>>
i as an index:
a 2 2.3
i as an element:
0 1 2
show testP:

Traceback (most recent call last):
File "/Users/bic/m400/jepna/source/for-experiment.py", line 14, in <module>
print(testP)
NameError: name 'testP' is not defined
>>>


This is because the RESTART has wiped out all the previous definitions and computations you made before running the module.

## A Closer Look at the Polynomial Program¶

Having our test polynomial list, testP = [1,0,1,7,0,4], we want to produce a nice human readable equivalent. Our first attempt,

testP = [1,0,1,7,0,4]

def printPoly1(P):
for i in range(len(P)):
print("%s x^%s + "%(P[i], i)),
print

printPoly1(testP)


produces,

1 x^0 +  0 x^1 +  1 x^2 +  7 x^3 +  0 x^4 +  4 x^5 +


This version writes out terms whose value is zero. By using an if statement, we can skip those terms.

def printPoly2(P):
for i in range(len(P)):
if P[i] != 0:
print("%s x^%s + "%(P[i], i)),
print

printPoly2(testP)


produces,

1 x^0 +  1 x^2 +  7 x^3 +  4 x^5 +


Thu Jan 28 10:18:38 PST 2016