SlideShare a Scribd company logo
1 of 8
Download to read offline
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
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
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)
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)"""
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)"""
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
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
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

More Related Content

Similar to Polynomials.pdf

Python Lab manual program for BE First semester (all department
Python Lab manual program for BE First semester (all departmentPython Lab manual program for BE First semester (all department
Python Lab manual program for BE First semester (all departmentNazeer Wahab
ย 
Kolev skalna2018 article-exact_solutiontoa_parametricline
Kolev skalna2018 article-exact_solutiontoa_parametriclineKolev skalna2018 article-exact_solutiontoa_parametricline
Kolev skalna2018 article-exact_solutiontoa_parametriclineAlina Barbulescu
ย 
1. Regression_V1.pdf
1. Regression_V1.pdf1. Regression_V1.pdf
1. Regression_V1.pdfssuser4c50a9
ย 
SPU Optimizations - Part 2
SPU Optimizations - Part 2SPU Optimizations - Part 2
SPU Optimizations - Part 2Naughty Dog
ย 
Optimization Techniques
Optimization TechniquesOptimization Techniques
Optimization TechniquesAjay Bidyarthy
ย 
An efficient algorithm for the computation of Bernoulli numbers
 An efficient algorithm for the computation of Bernoulli numbers An efficient algorithm for the computation of Bernoulli numbers
An efficient algorithm for the computation of Bernoulli numbersXequeMateShannon
ย 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquerKrish_ver2
ย 
03 dc
03 dc03 dc
03 dcHira Gul
ย 
Recursion Algorithms Derivation
Recursion Algorithms DerivationRecursion Algorithms Derivation
Recursion Algorithms DerivationRodrigue Tchamna
ย 
Unit-1 Basic Concept of Algorithm.pptx
Unit-1 Basic Concept of Algorithm.pptxUnit-1 Basic Concept of Algorithm.pptx
Unit-1 Basic Concept of Algorithm.pptxssuser01e301
ย 
Practical Volume Estimation of Zonotopes by a new Annealing Schedule for Cool...
Practical Volume Estimation of Zonotopes by a new Annealing Schedule for Cool...Practical Volume Estimation of Zonotopes by a new Annealing Schedule for Cool...
Practical Volume Estimation of Zonotopes by a new Annealing Schedule for Cool...Apostolos Chalkis
ย 
Applied Algorithms and Structures week999
Applied Algorithms and Structures week999Applied Algorithms and Structures week999
Applied Algorithms and Structures week999fashiontrendzz20
ย 
14th Athens Colloquium on Algorithms and Complexity (ACAC19)
14th Athens Colloquium on Algorithms and Complexity (ACAC19)14th Athens Colloquium on Algorithms and Complexity (ACAC19)
14th Athens Colloquium on Algorithms and Complexity (ACAC19)Apostolos Chalkis
ย 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptxKokilaK25
ย 
Mathematical preliminaries in Automata
Mathematical preliminaries in AutomataMathematical preliminaries in Automata
Mathematical preliminaries in AutomataMobeen Mustafa
ย 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfAmayJaiswal4
ย 
Digital electronics k map comparators and their function
Digital electronics k map comparators and their functionDigital electronics k map comparators and their function
Digital electronics k map comparators and their functionkumarankit06875
ย 
MATHS LAB MANUAL 22mats11.pdf
MATHS LAB MANUAL 22mats11.pdfMATHS LAB MANUAL 22mats11.pdf
MATHS LAB MANUAL 22mats11.pdfssuser8f6b1d1
ย 

Similar to Polynomials.pdf (20)

Python Lab manual program for BE First semester (all department
Python Lab manual program for BE First semester (all departmentPython Lab manual program for BE First semester (all department
Python Lab manual program for BE First semester (all department
ย 
Ning_Mei.ASSIGN03
Ning_Mei.ASSIGN03Ning_Mei.ASSIGN03
Ning_Mei.ASSIGN03
ย 
Kolev skalna2018 article-exact_solutiontoa_parametricline
Kolev skalna2018 article-exact_solutiontoa_parametriclineKolev skalna2018 article-exact_solutiontoa_parametricline
Kolev skalna2018 article-exact_solutiontoa_parametricline
ย 
1. Regression_V1.pdf
1. Regression_V1.pdf1. Regression_V1.pdf
1. Regression_V1.pdf
ย 
SPU Optimizations - Part 2
SPU Optimizations - Part 2SPU Optimizations - Part 2
SPU Optimizations - Part 2
ย 
Optimization Techniques
Optimization TechniquesOptimization Techniques
Optimization Techniques
ย 
An efficient algorithm for the computation of Bernoulli numbers
 An efficient algorithm for the computation of Bernoulli numbers An efficient algorithm for the computation of Bernoulli numbers
An efficient algorithm for the computation of Bernoulli numbers
ย 
5.2 divide and conquer
5.2 divide and conquer5.2 divide and conquer
5.2 divide and conquer
ย 
03 dc
03 dc03 dc
03 dc
ย 
Recursion Algorithms Derivation
Recursion Algorithms DerivationRecursion Algorithms Derivation
Recursion Algorithms Derivation
ย 
Unit-1 Basic Concept of Algorithm.pptx
Unit-1 Basic Concept of Algorithm.pptxUnit-1 Basic Concept of Algorithm.pptx
Unit-1 Basic Concept of Algorithm.pptx
ย 
Practical Volume Estimation of Zonotopes by a new Annealing Schedule for Cool...
Practical Volume Estimation of Zonotopes by a new Annealing Schedule for Cool...Practical Volume Estimation of Zonotopes by a new Annealing Schedule for Cool...
Practical Volume Estimation of Zonotopes by a new Annealing Schedule for Cool...
ย 
Applied Algorithms and Structures week999
Applied Algorithms and Structures week999Applied Algorithms and Structures week999
Applied Algorithms and Structures week999
ย 
14th Athens Colloquium on Algorithms and Complexity (ACAC19)
14th Athens Colloquium on Algorithms and Complexity (ACAC19)14th Athens Colloquium on Algorithms and Complexity (ACAC19)
14th Athens Colloquium on Algorithms and Complexity (ACAC19)
ย 
01 - DAA - PPT.pptx
01 - DAA - PPT.pptx01 - DAA - PPT.pptx
01 - DAA - PPT.pptx
ย 
Mathematical preliminaries in Automata
Mathematical preliminaries in AutomataMathematical preliminaries in Automata
Mathematical preliminaries in Automata
ย 
Unit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdfUnit-1 DAA_Notes.pdf
Unit-1 DAA_Notes.pdf
ย 
Recursion in C
Recursion in CRecursion in C
Recursion in C
ย 
Digital electronics k map comparators and their function
Digital electronics k map comparators and their functionDigital electronics k map comparators and their function
Digital electronics k map comparators and their function
ย 
MATHS LAB MANUAL 22mats11.pdf
MATHS LAB MANUAL 22mats11.pdfMATHS LAB MANUAL 22mats11.pdf
MATHS LAB MANUAL 22mats11.pdf
ย 

Recently uploaded

CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kesar Bagh Lucknow best Night Fun service ๐Ÿชก
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kesar Bagh Lucknow best Night Fun service  ๐ŸชกCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kesar Bagh Lucknow best Night Fun service  ๐Ÿชก
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kesar Bagh Lucknow best Night Fun service ๐Ÿชกanilsa9823
ย 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfmuntazimhurra
ย 
Caco-2 cell permeability assay for drug absorption
Caco-2 cell permeability assay for drug absorptionCaco-2 cell permeability assay for drug absorption
Caco-2 cell permeability assay for drug absorptionPriyansha Singh
ย 
Types of different blotting techniques.pptx
Types of different blotting techniques.pptxTypes of different blotting techniques.pptx
Types of different blotting techniques.pptxkhadijarafiq2012
ย 
Lucknow ๐Ÿ’‹ Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow ๐Ÿ’‹ Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow ๐Ÿ’‹ Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow ๐Ÿ’‹ Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...anilsa9823
ย 
Call Us โ‰ฝ 9953322196 โ‰ผ Call Girls In Mukherjee Nagar(Delhi) |
Call Us โ‰ฝ 9953322196 โ‰ผ Call Girls In Mukherjee Nagar(Delhi) |Call Us โ‰ฝ 9953322196 โ‰ผ Call Girls In Mukherjee Nagar(Delhi) |
Call Us โ‰ฝ 9953322196 โ‰ผ Call Girls In Mukherjee Nagar(Delhi) |aasikanpl
ย 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Nistarini College, Purulia (W.B) India
ย 
Nanoparticles synthesis and characterizationโ€‹ โ€‹
Nanoparticles synthesis and characterizationโ€‹  โ€‹Nanoparticles synthesis and characterizationโ€‹  โ€‹
Nanoparticles synthesis and characterizationโ€‹ โ€‹kaibalyasahoo82800
ย 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bSรฉrgio Sacani
ย 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoSรฉrgio Sacani
ย 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisDiwakar Mishra
ย 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: โ€œEg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: โ€œEg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: โ€œEg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: โ€œEg...Sรฉrgio Sacani
ย 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTSรฉrgio Sacani
ย 
A relative description on Sonoporation.pdf
A relative description on Sonoporation.pdfA relative description on Sonoporation.pdf
A relative description on Sonoporation.pdfnehabiju2046
ย 
Boyles law module in the grade 10 science
Boyles law module in the grade 10 scienceBoyles law module in the grade 10 science
Boyles law module in the grade 10 sciencefloriejanemacaya1
ย 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PPRINCE C P
ย 
Biopesticide (2).pptx .This slides helps to know the different types of biop...
Biopesticide (2).pptx  .This slides helps to know the different types of biop...Biopesticide (2).pptx  .This slides helps to know the different types of biop...
Biopesticide (2).pptx .This slides helps to know the different types of biop...RohitNehra6
ย 
G9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptG9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptMAESTRELLAMesa2
ย 

Recently uploaded (20)

CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kesar Bagh Lucknow best Night Fun service ๐Ÿชก
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kesar Bagh Lucknow best Night Fun service  ๐ŸชกCALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kesar Bagh Lucknow best Night Fun service  ๐Ÿชก
CALL ON โžฅ8923113531 ๐Ÿ”Call Girls Kesar Bagh Lucknow best Night Fun service ๐Ÿชก
ย 
Engler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomyEngler and Prantl system of classification in plant taxonomy
Engler and Prantl system of classification in plant taxonomy
ย 
Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdf
ย 
Caco-2 cell permeability assay for drug absorption
Caco-2 cell permeability assay for drug absorptionCaco-2 cell permeability assay for drug absorption
Caco-2 cell permeability assay for drug absorption
ย 
The Philosophy of Science
The Philosophy of ScienceThe Philosophy of Science
The Philosophy of Science
ย 
Types of different blotting techniques.pptx
Types of different blotting techniques.pptxTypes of different blotting techniques.pptx
Types of different blotting techniques.pptx
ย 
Lucknow ๐Ÿ’‹ Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow ๐Ÿ’‹ Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...Lucknow ๐Ÿ’‹ Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
Lucknow ๐Ÿ’‹ Russian Call Girls Lucknow Finest Escorts Service 8923113531 Availa...
ย 
Call Us โ‰ฝ 9953322196 โ‰ผ Call Girls In Mukherjee Nagar(Delhi) |
Call Us โ‰ฝ 9953322196 โ‰ผ Call Girls In Mukherjee Nagar(Delhi) |Call Us โ‰ฝ 9953322196 โ‰ผ Call Girls In Mukherjee Nagar(Delhi) |
Call Us โ‰ฝ 9953322196 โ‰ผ Call Girls In Mukherjee Nagar(Delhi) |
ย 
Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...Bentham & Hooker's Classification. along with the merits and demerits of the ...
Bentham & Hooker's Classification. along with the merits and demerits of the ...
ย 
Nanoparticles synthesis and characterizationโ€‹ โ€‹
Nanoparticles synthesis and characterizationโ€‹  โ€‹Nanoparticles synthesis and characterizationโ€‹  โ€‹
Nanoparticles synthesis and characterizationโ€‹ โ€‹
ย 
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43bNightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
Nightside clouds and disequilibrium chemistry on the hot Jupiter WASP-43b
ย 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on Io
ย 
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral AnalysisRaman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
Raman spectroscopy.pptx M Pharm, M Sc, Advanced Spectral Analysis
ย 
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: โ€œEg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: โ€œEg...All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: โ€œEg...
All-domain Anomaly Resolution Office U.S. Department of Defense (U) Case: โ€œEg...
ย 
Disentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOSTDisentangling the origin of chemical differences using GHOST
Disentangling the origin of chemical differences using GHOST
ย 
A relative description on Sonoporation.pdf
A relative description on Sonoporation.pdfA relative description on Sonoporation.pdf
A relative description on Sonoporation.pdf
ย 
Boyles law module in the grade 10 science
Boyles law module in the grade 10 scienceBoyles law module in the grade 10 science
Boyles law module in the grade 10 science
ย 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C P
ย 
Biopesticide (2).pptx .This slides helps to know the different types of biop...
Biopesticide (2).pptx  .This slides helps to know the different types of biop...Biopesticide (2).pptx  .This slides helps to know the different types of biop...
Biopesticide (2).pptx .This slides helps to know the different types of biop...
ย 
G9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.pptG9 Science Q4- Week 1-2 Projectile Motion.ppt
G9 Science Q4- Week 1-2 Projectile Motion.ppt
ย 

Polynomials.pdf

  • 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