Professional Documents
Culture Documents
Programming
in Python
>>> Second Edition:
with Python 3
Maria Litvin
Phillips Academy, Andover, Massachusetts
Gary Litvin
Skylight Software, Inc.
Skylight Publishing
Andover, Massachusetts
Skylight Publishing
9 Bartlet Street, Suite 70
Andover, MA 01810
web:
e-mail:
http://www.skylit.com
sales@skylit.com
support@skylit.com
1 2 3 4 5 6 7 8 9 10
15 14 13 12 11 10
12 Polynomials
12.1 Prologue
An expression
an x n + an 1 x n 1 + ... + a1 x + a0
is called a polynomial. The numbers an , an 1 , ..., a0 are called the coefficients of the
polynomial. an 0 is the leading coefficient; a0 is called the constant term. n is
called the degree of the polynomial. We will work with polynomials whose
coefficients are real numbers. For example, an expression 2.4 x 3 + 3 x 2 + 1.5 x + 4.9 is
a polynomial of degree 3, with the leading coefficient 2.4 and the constant term 4.9.
When we write polynomials, we usually omit the terms with zero coefficients. For
example, 4 x 2 + 0 x + 1 is written simply as 4 x 2 + 1 . Also, when the coefficient is 1
we do not write it. For example, 1x 2 + 1x + 1 is written simply as x 2 + x + 1 . When a
polynomial has a term with a negative coefficient, that coefficient is usually written
with a minus sign in place of plus and without parentheses. For example,
x 7 + ( 3) x 2 is written simply as x 7 3x 2 .
In computer programs, a polynomial can be represented as a list of its coefficients.
For example, in Python 1.8 x 5 3 x 2 + 2.35 can be represented as
p = [1.8, 0, 0, -3, 0, 2.35]. An n-th degree polynomial is represented by
a list with n + 1 elements.
There are two ways to look at polynomials. First, we can view a polynomial as a
function P ( x) defined by the formula P ( x) = an x n + an 1 x n 1 + ... + a1 x + a0 . We can
calculate the value of P ( x) for any real number x. Second, we can view polynomials
as abstract algebraic objects. We can add, subtract, multiply, and factor polynomials.
We can study properties of polynomials with integer, rational, or real coefficients. In
many important ways, the algebraic properties of polynomials parallel some
properties of integers. For example, any positive integer can be factored, or
represented as a product of primes, and such a representation is unique. Similarly,
any polynomial can be uniquely represented as a product of prime polynomials
(give or take constant factors). For example, x 3 1 = ( x 1)( x 2 + x + 1) .
205
206
CHAPTER 12 ~ POLYNOMIALS
ax 2 + bx + c
(a x
+ ... + a1 x + a0 ) + ( bn x n + ... + b1 x + b0 ) =
( an + bn ) x n + ... + ( a1 + b1 ) x + ( a0 + b0 )
In other words, to add two polynomials we simply add the respective coefficients. If
the degree of one polynomial is lower than the degree of the other, the missing terms
in the lower-degree polynomial are considered to have zero coefficients.
Example 1
(3x 3 + 2 x 2 + 7 x + 2) + (4 x 2 + 3 x + 6) = 3 x3 + 6 x 2 + 10 x + 8
3x3 + 2x2 + 7x + 2
4x2 + 3x + 6
3x3 + 6x2 + 10x + 8
207
It is important to combine properly the like terms (that is, the terms with
the same degree of x) when adding two polynomials.
Example 2
(3 x 3 + 2 x 2 + 7 x + 2) + (4 x 2 + 1) = 3 x 3 + 6 x 2 + 7 x + 3
3x3 + 2x2 + 7x + 2
4x2
+ 1
3x3 + 6x2 + 7x + 3
With a little practice, you can quickly learn to combine the like terms in your head
and write the resulting sum without writing down the intermediate steps.
When you combine the coefficients of the like terms of polynomials, pay
careful attention to the signs.
Example 3
(3 x 3 2 x 2 7 x + 2) + (4 x 2 1) =
3 x 3 + (2 + 4) x 2 7 x + (2 1) =
3x3 + 2 x 2 7 x + 1
It is better to perform all the intermediate steps quickly in your head and write only
the final result. Something like this: for x cubed: 3 and nothing ==> 3x 3 ; for x
squared: 2 + 4 ==> +2 x 2 ; for x: -7 + nothing ==> 7x ; for constant terms: +2 1
==> +1 .
To negate a polynomial you need to negate all of its coefficients. P1 P2 is the same
as P1 + ( P2 ) .
208
CHAPTER 12 ~ POLYNOMIALS
Example 4
( x 2 1) (2 x 3 + x 2 5 x + 1) =
( x 2 1) + ( 2 x 3 x 2 + 5 x 1) =
2 x 3 + 5 x 2
With a little practice, you can learn to subtract a polynomial from another polynomial
in your head and write only the final result.
Exercises
1.
2.
Simplify ( x 4 + 3x 2 + 1) + ( x3 + x 2 + 8 ) . 3
3.
Simplify ( 2 x 4 + x 3 + 4 ) ( x 4 + 2 x 3 + 5 ) .
4.
5.
Write and test a Python function negate(p) that negates the polynomial
represented by the list of its coefficients p and returns a new polynomial
(represented as a list).
Hint: use a list comprehension. 3
6.
7.
9.
209
Write and test a function add(p1, p2) that adds the polynomials p1 and p2
(represented as lists) and returns their sum (represented as a list). When you
add or subtract polynomials, several leading coefficient(s) in the result may
become zeros. Dont forget to get rid of them.
Hints:
1. The degree of the sum does not exceed the largest of the degrees of p1
and p2.
2. Find the first non-zero element of the resulting list and return the slice of
the list starting from that element.
10.
Write and test a Python function toString(p) that takes a polynomial with
integer coefficients (represented as a list of its coefficients) and returns a
string that represents the polynomial in the conventional form, as a sum of
powers of x with appropriate coefficients. Use "x^k" to represent x k . Test
your function thoroughly on the following examples:
p
toString(p)
[1, 0, 0, 0, 0]
'x^4'
[3, 2, 1]
'3x^2 + 2x + 1'
[1, 0, 1]
'x^2 + 1'
[3, -2, 1]
'3x^2 - 2x + 1'
[-3, 0, 1]
'-3x^2 + 1'
[-1, 0, -1]
'-x^2 - 1'
[]
'0'
[0]
'0'
[0, 0, 0]
'0'
210
CHAPTER 12 ~ POLYNOMIALS
Example 1
( 3x
x 2 + 2 x 6 ) 3 x 2 = 9 x 5 3 x 4 + 6 x 3 18 x 2
p = p + k*[0]
Example 2
x 2 5 x + 6 is represented as [1, -5, 6]. What list represents x 3 ( x 2 5 x + 6 ) ?
Solution
[1, -5, 6, 0, 0, 0]
211
Example 3
( 3x
x 2 + 2 x 6 ) ( 3x 2 2 ) =
9 x 5 3x 4 + 6 x 3 18 x 2 6 x 3 + 2 x 2 4 x + 12 =
0
9 x 5 3 x 4 16 x 2 4 x + 12
The number of terms in the product, before collecting the like terms, is equal to the
number of terms in the first polynomial times the number of terms in the second
polynomial (here 4 2 = 8 ).
Many algebra textbooks teach the so-called FOIL (First, Outer, Inner, Last)
mnemonic rule for multiplying two linear polynomials.
For example,
2
2
x
1
(3
x
+
5)
=
6
x
+
10
x
3
x
5
.
This
order
of
listing
the
resulting
terms works
(
)
only for linear polynomials. It may mislead you when one or both factors are
quadratics or polynomials of higher degrees. To find P Q , it is better to multiply
each term of P by the first term of Q, then each term of P by the second term of Q,
and so on. In the above example, we would get ( 2 x 1) (3 x + 5) = 6 x 2 3 x + 10 x 5 .
This is not a big deal for linear polynomials one can get used to either order but
it would be better if FOIL didnt exist, and you were taught the more general
method from the outset. In either case, you have to collect the like terms at the end:
( 2 x 1) (3x + 5) = 6 x 2 3x + 10 x 5 = 6 x 2 + 7 x 5 .
Given two positive integers, we can divide one (the dividend) by the other (the
divisor) and get a quotient and a remainder. For example, when we divide 17 by 3,
the quotient is 5 and the remainder is 2: 17 = 5 3 + 2 . The remainder is smaller then
the divisor: 2 < 3 . A similar type of division with a remainder can be performed on
polynomials with real coefficients.
For example, if we divide
x 5 6.9 x 4 + 5.3 x 3 4 x 2 + 3x + 1.2
by
2 x 2 x + 0.2 ,
the
quotient
is
3
2
0.5 x 3.2 x + x 1.18 and the remainder is 1.62 x + 1.436 :
212
CHAPTER 12 ~ POLYNOMIALS
x5 - 6.9x4 + 5.3x3 -
4x2 +
3x +
1.2
3x +
1.2
3x +
1.2
x5 - 0.5x4 + 0.1x3
4
4x2 +
- 6.4x + 5.2x -
2x3 -3.36x2 +
3
2x -
x + 0.2x
2
-2.36x + 2.8x +
1.2
1.62x + 1.436
Figure 12-1. Example of long division for polynomials
213
b
.
a
A quadratic
b b 2 4ac
2a
b + b 2 4ac
(when b 2 4ac 0 ).
In general, an n-th degree
2a
polynomial with real coefficients can have up to n real roots.
and x2 =
The above fact is also known as the factor theorem: c is a root of a polynomial P( x)
The proof is not easy, though; it uses complex numbers and follows from the
fundamental theorem of algebra: any n-th degree polynomial (n > 1) with complex
coefficients can be represented as a product of n linear factors (with complex
coefficients) and, therefore, has exactly n complex roots (not necessarily all
different). For example, x 2 + 1 = ( x i )( x + i ) , where i is the imaginary number
i = 1 . As for real roots, any odd degree polynomial with real coefficients has at
least one real root.
214
CHAPTER 12 ~ POLYNOMIALS
Exercises
1.
2.
3.
Write and test a function multiply(p1, p2) that returns the product of
two polynomials. Use one loop; within it, call multiplyByOneTerm from
Question 2 and add(p1, p2) from Question 9 in Section 12.2.
4.
Write a version of multiply(p1, p2) that does not call other functions.
Start with the result list of the appropriate length, filled with zeros, then
calculate the value of each element of result as a sum of products:
result[0] = p1[0] * p2[0];
result[1] = p1[0] * p2[1] + p1[1] * p2[0];
and so on. 3
5.
Write and test a function divide(p1, p2) that divides the polynomial p1
by the polynomial p2 and returns the quotient and the remainder (combined
into a tuple). Use the long division algorithm.
6.
215
7.
Write a program that prompts the user to enter a positive integer n and prints
out the coefficients of the expansion of ( x + 1) n and their sum. 3
8.
9.
10.
If you expand ( x + 1) n , you get a polynomial. What are its coefficients? You had a
n
glimpse of them in Section 8.5, Question 8: the coefficient at x k turns out to be
k
n
(n-choose-k). In general, if you expand ( x + y ) , you get a sum of the terms
n
ak x k y n k . Again, ak = . ( x + y ) is a binomial, and the fact that the coefficients
k
n
of ( x + y ) are the numbers n-choose-k is called the binomial theorem.
The binomial theorem states that
n
n
n
n
n
( x + y ) n = x n + x n 1 y + ... + y n = x n k y k
k
0
1
n
k =0
That is why the n-choose-k numbers are often called binomial coefficients. The
formula was discovered by Isaac Newton and announced by him in 1676.
The proof of the binomial theorem goes like this:
( x + y ) n = (
x
+
y)
( x
+
y
) ...
( x
+
y)
216
CHAPTER 12 ~ POLYNOMIALS
x
y
x
x
...
y
x = x n k y k
We pick from each set of parentheses either x or y and multiply them. When we
collect the like terms, we get the coefficient for the term x n k y k . So the question is
how many ways are there to form x n k y k . In order to get such a term we must take y
k times and x (n k ) times. So the coefficient is equal to the number of ways in
which we can choose k objects (here the sets of parentheses where we picked y) from
n (all sets of parentheses).
The fact that the n-choose-k numbers are also binomial coefficients is useful for
establishing many of their amazing properties (see Questions 2 - 6).
Example 1
Expand ( x + y ) 4 .
Solution
( x + y ) 4 = x 4 + 4 x 3 y + 6 x 2 y 2 + 4 xy 3 + y 4
Example 2
What is the coefficient at x8 y10 in the expansion of ( x + y )18 ?
Solution
18!
18
= 6480
10 =
10! 8!
217
( x + y ) 2 = x 2 + 2 xy + y 2
( x y ) 2 = x 2 2 xy + y 2
( x + y )3 = x 3 + 3x 2 y + 3xy 2 + y 3
( x y )3 = x 3 3x 2 y + 3xy 2 y 3
3
0
4
0
1
1
2
1
3
1
4
1
1
1
2
2
3
2
4
2
1
3
3
4
3
1
4
4
.........................................................
1
2
3
4
1
3
1
4
.........................................................
Now look at this triangle. It is symmetrical about its vertical axis. The numbers on
the left and right borders are 1. Each number inside is the sum of the two numbers
n n 1 n 1
above it: =
+
. Starting from the top, we can build as many rows
k k 1 k
of the triangle as we need.
218
CHAPTER 12 ~ POLYNOMIALS
Exercises
1.
Write a program that prompts the user for a positive number n and prints out
n n
n
a list of the binomial coefficients , , ..., . Hint: see Question 4
0
1
n
in Section 8.5.
2.
If a set A has n elements, the total number of its subsets (including the empty
n n
n
set and the whole set A) is + + ... + . Using this fact, derive a
0
1
n
formula for this sum. 3
3.
4.
n n
n
Derive the formula for + + ... + (see Question 2) in a different
0 1
n
way, using the binomial theorem. Hint: consider (1 + 1) n .
Show that
n
k =0
5.
n n
n
n
n n
n
k = 0 + 2 1 + 4 2 + ... + 2 n = 3 3
Show that
k (1)
n
k =0
n n n
n
= + ... + (1) n = 0
0 1 2
n
219
Show that
2
n n
n 2n
0 + 1 + ... + n = n
Hint: ( x + y ) 2 n is the same polynomial as ( x + y )n ( x + y )n . Compare the
coefficients at x n y n when you expand each of these representations.
7.
Write a program that prompts the user for a positive number n and prints the
first n rows of Pascals Triangle. Do not multiply any polynomials in this
program just build the next row of the triangle from the preceding row
n n 1 n 1
using the relationship =
+
.
k k 1 k
8.
1
1
1
1
1
2
1
3
1
4
6
4
1
..................................................
Lets take the sum of the binomial coefficients in each band. Guess what
sequence these sums form and show that your guess is correct.
9.
10.
2n 2n 2n
Show that for any n 1 ,
+
n n 1 n + 1
3
Triangle.
220
CHAPTER 12 ~ POLYNOMIALS
12.5 Review
Terms and formulas introduced in this chapter:
Polynomial
Coefficient of a polynomial
Degree of a polynomial
Linear polynomial
Quadratic polynomial
Long division (for polynomials)
Remainder theorem
Factor theorem
Binomial coefficient
Binomial theorem
Pascals Triangle
P ( x) = Q ( x)( x c) + P (c)
c is a root of P ( x) if and only if ( x c) is a factor of P ( x) .
If an x n + ... + a1 x + a0 has n roots c1 , c2 , ..., cn , then
an x n + ... + a1 x + a0 = an ( x c1 )( x c2 )...( x cn ) .
( x + y ) 2 = x 2 + 2 xy + y 2
( x y ) 2 = x 2 2 xy + y 2
( x + y )3 = x3 + 3x 2 y + 3xy 2 + y 3
( x y )3 = x3 3x 2 y + 3xy 2 y 3
n
n
n
n
n
( x + y ) n = x n + x n 1 y + ... + y n = x n k y k
0
1
n
k
k =0