1. Polynomials
Jordi Cortadella
Department of Computer Science
Outline
โข How to represent a polynomial?
โข Evaluation of a polynomial.
โข Basic operations: sum, product and division.
โข Greatest common divisor.
Introduction to Programming ยฉ Dept. CS, UPC 2
Representation of polynomials
Introduction to Programming ยฉ Dept. CS, UPC 3
A list of coefficients
Properties of the representation:
โข ๐๐ โ 0 (the topmost element is not zero).
โข ๐ ๐ฅ = 0 is represented with an empty vector.
๐ ๐ฅ = ๐๐๐ฅ๐ + ๐๐โ1๐ฅ๐โ1 + โฏ + ๐1๐ฅ + ๐0
๐ ๐ โ 1 โฏ 1 0
๐๐ ๐๐โ1 โฏ ๐1 ๐0
Polynomial evaluation (Hornerโs scheme)
โข Design a function that evaluates the value of a
polynomial.
โข A polynomial of degree ๐ can be efficiently
evaluated using Hornerโs algorithm:
๐ ๐ฅ = ๐๐๐ฅ๐ + ๐๐โ1๐ฅ๐โ1 + โฏ + ๐1๐ฅ + ๐0 =
(โฏ ((๐๐๐ฅ + ๐๐โ1)๐ฅ + ๐๐โ2)๐ฅ + โฏ )๐ฅ + ๐0
โข Example:
3๐ฅ3
โ 2๐ฅ2
+ ๐ฅ โ 4 = 3๐ฅ โ 2 ๐ฅ + 1 ๐ฅ โ 4
Introduction to Programming ยฉ Dept. CS, UPC 4
2. Polynomial evaluation (Hornerโs scheme)
Introduction to Programming ยฉ Dept. CS, UPC 5
3๐ฅ3
โ 2๐ฅ2
+ ๐ฅ โ 4
3 ๐ฅ โ 2 ๐ฅ + 1
3 ๐ฅ โ 2 ๐ฅ + 1 ๐ฅ โ 4
3 ๐ฅ โ 2
3
Polynomial evaluation (Hornerโs scheme)
def poly_eval(P, x):
"""Returns the evaluation of P(x)"""
r = 0
# Evaluates the polynomial using Horner's scheme
# The coefficients are visited from highest to lowest
for i in range(len(P)-1, -1, -1):
r = r*x + P[i]
return r
Introduction to Programming ยฉ Dept. CS, UPC 6
Product of polynomials
Example:
Introduction to Programming ยฉ Dept. CS, UPC 7
def poly_mul(P, Q):
"""Returns P*Q"""
๐ ๐ฅ = 2๐ฅ3 + ๐ฅ2 โ 4
๐ ๐ฅ = ๐ฅ2
โ 2๐ฅ + 3
๐ โ ๐ ๐ฅ = 2๐ฅ5 + โ4 + 1 ๐ฅ4 + 6 โ 2 ๐ฅ3 + โ4 + 3 ๐ฅ2 + 8๐ฅ โ 12
๐ โ ๐ ๐ฅ = 2๐ฅ5
โ 3๐ฅ4
+ 4๐ฅ3
โ ๐ฅ2
+ 8๐ฅ โ 12
Product of polynomials
โข Key point:
Given ๐ ๐ฅ = ๐๐๐ฅ๐ + ๐๐โ1๐ฅ๐โ1 + โฏ + ๐1๐ฅ + ๐0
and ๐ ๐ฅ = ๐๐๐ฅ๐ + ๐๐โ1๐ฅ๐โ1 + โฏ + ๐1๐ฅ + ๐0,
what is the coefficient ๐๐ of ๐ฅ๐
in (๐ โ ๐)(๐ฅ)?
โข The product ๐๐๐ฅ๐
โ ๐๐๐ฅ๐
must be added to the term
with ๐ฅ๐+๐
.
โข Idea: for every ๐ and ๐, add ๐๐ โ ๐๐ to the (๐ + ๐)-th
coefficient of the product.
Introduction to Programming ยฉ Dept. CS, UPC 8
3. Product of polynomials
Introduction to Programming ยฉ Dept. CS, UPC 9
2 -1 9 -5 10 -3
๐ฅ5
๐ฅ4
๐ฅ3
๐ฅ2
๐ฅ1
๐ฅ0
๐ฅ3 ๐ฅ2 ๐ฅ1 ๐ฅ0
2 -1 3
1 0 3 -1
ร
2 6
-1
0 -2
0 -3 1
3 0 9 -3
+
Product of polynomials
def poly_mul(P, Q):
"""Returns P*Q"""
# Special case for a polynomial of size 0
if len(P) == 0 or len(Q) == 0:
return []
R = [0]*(len(P)+len(Q)-1) # list of zeros
for i in range(len(P)):
for j in range(len(Q)):
R[i+j] += P[i]*Q[j]
return R
Introduction to Programming ยฉ Dept. CS, UPC 10
Sum of polynomials
โข Note that over the real numbers,
degree ๐ โ ๐ = degree ๐ + degree(๐)
(except if ๐ = 0 or ๐ = 0).
So we know the size of the result vector a priori.
โข This is not true for the polynomial sum, e.g.,
degree ๐ฅ + 5 + โ๐ฅ โ 1 = 0
Introduction to Programming ยฉ Dept. CS, UPC 11
Sum of polynomials
A function to normalize a polynomial might be useful in
some algorithms, i.e., remove the leading zeros to
guarantee the most significant coefficient is not zero.
Introduction to Programming ยฉ Dept. CS, UPC 12
def poly_normalize(P):
"""Resizes the polynomial to guarantee that the
leading coefficient is not zero."""
while len(P) > 0 and P[-1] == 0:
P.pop(-1)
4. Sum of polynomials
def poly_add(P, Q):
"""Returns P+Q"""
if len(Q) > len(P): # guarantees len(P) >= len(Q)
P, Q = Q, P
# Adds the coefficients up to len(Q)
R = []
for i in range(len(Q)):
R.append(P[i]+Q[i])
R += P[i+1:] # appends the remaining coefficients of P
poly_normalize(R)
return R
Introduction to Programming ยฉ Dept. CS, UPC 13
P
Q
Euclidean division of polynomials
โข For every pair of polynomials ๐ด and ๐ต, such that
๐ต โ 0, find ๐ and ๐ such that
๐ด = ๐ต โ ๐ + ๐
and degree ๐ < degree(๐ต).
๐ and ๐ are the only polynomials satisfying this
property.
โข Classical algorithm: Polynomial long division.
Introduction to Programming ยฉ Dept. CS, UPC 14
Polynomial long division
def poly_div(A, B):
"""Returns the quotient (Q) and the remainder (R)
of the Euclidean division A/B. They are returned
as a tuple (Q, R)"""
Introduction to Programming ยฉ Dept. CS, UPC 15
A B R
Q A B R
Q
Polynomial long division
Introduction to Programming ยฉ Dept. CS, UPC 16
def poly_div(A, B):
"""Returns the quotient (Q) and the remainder (R)
of the Euclidean division A/B. They are returned
as a tuple (Q, R)"""
5. A B R
Q
Polynomial long division
Introduction to Programming ยฉ Dept. CS, UPC 17
def poly_div(A, B):
"""Returns the quotient (Q) and the remainder (R)
of the Euclidean division A/B. They are returned
as a tuple (Q, R)"""
A B R
Q
Polynomial long division
Introduction to Programming ยฉ Dept. CS, UPC 18
def poly_div(A, B):
"""Returns the quotient (Q) and the remainder (R)
of the Euclidean division A/B. They are returned
as a tuple (Q, R)"""
A B R
Q
Polynomial long division
Introduction to Programming ยฉ Dept. CS, UPC 19
def poly_div(A, B):
"""Returns the quotient (Q) and the remainder (R)
of the Euclidean division A/B. They are returned
as a tuple (Q, R)"""
A B R
Q
Polynomial long division
Introduction to Programming ยฉ Dept. CS, UPC 20
def poly_div(A, B):
"""Returns the quotient (Q) and the remainder (R)
of the Euclidean division A/B. They are returned
as a tuple (Q, R)"""
6. A B R
Q
Polynomial long division
Introduction to Programming ยฉ Dept. CS, UPC 21
def poly_div(A, B):
"""Returns the quotient (Q) and the remainder (R)
of the Euclidean division A/B. They are returned
as a tuple (Q, R)"""
Polynomial long division
Introduction to Programming ยฉ Dept. CS, UPC 22
A B R
Q
def poly_div(A, B):
"""Returns the quotient (Q) and the remainder (R)
of the Euclidean division A/B. They are returned
as a tuple (Q, R)"""
Polynomial long division
Introduction to Programming ยฉ Dept. CS, UPC 23
2 6 -3 1 0 -1 2 0 1 -1
1 3 -2
5 4 3 2 1 0 3 2 1 0
2 1 0
R: B:
Q:
6 -4 2 0 -1
-4 -1 3 -1
-1 5 -3
A
Invariant: A = B๏Q+R
Polynomial long division
def poly_div(A, B):
"""Returns the quotient (Q) and the remainder (R) of the
Euclidean division A/B. They are returned as a tuple (Q, R)"""
R = A[:] # copy of A
if len(A) < len(B):
return [], R
Q = [0]*(len(A)-len(B)+1)
# For each digit of Q
for iQ in range(len(Q)-1, -1, -1):
Q[iQ] = R[-1]/B[-1]
R.pop(-1)
for k in range(len(B)-1):
R[k+iQ] -= Q[iQ]*B[k]
poly_normalize(R)
return Q, R
Introduction to Programming ยฉ Dept. CS, UPC 24
iQ
k
k+iQ
7. GCD of two polynomials
Introduction to Programming ยฉ Dept. CS, UPC 25
Example:
Re-visiting Euclidean algorithm for gcd
# gcd(a, 0) = a
# gcd(a, b) = gcd(b, a%b)
def gcd(a, b):
"""Returns gcd(a, b)"""
while b > 0:
a, b = b, a%b
return a
For polynomials:
โข a and b are polynomials.
โข a%b is the remainder of
the Euclidean division.
Introduction to Programming ยฉ Dept. CS, UPC 26
Euclidean algorithm for gcd
def poly_gcd(A, B):
"""Returns gcd(A, B)"""
while len(B) > 0:
Q, R = poly_div(A, B)
A, B = B, R
# Converts to monic polynomial (e.g., 3x-6 ๏ x-2)
c = A[โ1]
for i in range(len(A)):
A[i] /= c
return A
Introduction to Programming ยฉ Dept. CS, UPC 27
Euclidean algorithm for gcd
monic polynomial
Introduction to Programming ยฉ Dept. CS, UPC 28
8. Conclusions
โข Polynomials are used very often in numerical
analysis.
โข Polynomial functions have nice properties:
continuous and simple derivatives and
antiderivatives.
โข There are simple root-finding algorithms to
find approximations of the roots.
Introduction to Programming ยฉ Dept. CS, UPC 29