COL100-Lecture 15 Lists As Polynomials
COL100-Lecture 15 Lists As Polynomials
and
Matrices
Representing Polynomials as lists
Representation: Little Endian and “dense”
def addInverse(L):
# INPUT list L==[a_0,…,a_m] represents p == a0 + a1.x
+ … + am.xm
# OUTPUT represents - p(x)
return [ - x for x in L ]
Canonical Forms
zeroPoly can also be represented as [ ] or [ 0 ]
• or as [0, 0] or [0, 0, 0 ] or …
Convert a polynomial into canonical form, with no trailing zeroes:
L==[a_0,…,a_m] where am =/= 0
def makeCanonical(L):
m = len(L)
i = m-1 # start from the highest coeff
highzero = True
while (i >= 0) and highzero: # Repeatedly
if (L[i] == 0):
del L[i] # delete redundant trailing zero
i -= 1
else:
highzero = False # stop at non-zero coefficient
So is addInverse:
def addInverseB(L):
# INPUT list L==[a_m,…,a_0] represents p == am.xm + … + a1.x + a0
# OUTPUT represents - p(x)
return [ - x for x in L ]
def addPolyB(L1,L2):
# Big-Endian
# INPUT list L1 == [a_m,…,a_0] represents p1 == am.xm + … + a1.x + a0
# INPUT list L2 ==[b_n,…, b_0] represents p2 == bn.xn + … + b1.x + b0
# OUTPUT L3 is the representation of p1 + p2
m, n =len(L1), len(L2)
diff = abs(m-n)
L3 = [ ]
if m < n:
for i in range(0,diff):
L3.append(L2[i])
for i in range(diff,n):
L3.append(L1[i-diff] + L2[i])
else:
for i in range(0,diff):
L3.append(L1[i])
for i in range(diff,m):
L3.append(L1[i] + L2[i-diff])
return L3
Big_Endian Canonical Forms
Convert a polynomial into canonical form, with no leading zeroes:
L==[a_m,…,a_0] where am =/= 0
def makeCanonicalB(L):
m = len(L)
i = 0 # start from the highest coeff
highzero = True
while (i < m) and highzero: # Repeatedly
if (L[0] == 0):
# Note: del modifies L by deleting elem at index
del L[0] # delete redundant leading zero coif
i += 1
else:
highzero = False # stop at first non-zero
coefficient
def multPolyB(L1,L2):
# Big-Endian
# INPUT L1 == [a_m,…,a_0] represents p1 == am.xm + … + a1.x + a0
# INPUT L2 ==[b_n,…, b_0] represents p2 == bn.xn + … + b1.x + b0
# OUTPUT L3 is the representation of p1 * p2
m, n =len(L1), len(L2)
L3 = [ ]
if n>0:
# multiply L1 by lowest term of L2
L4 = [ L2[n-1] * x for x in L1 ]
# recursively call multiply on L1 with remaining L2 terms
L5 = multPoly(L1,L2[0:n-1])
# shift the entire L5 by one place here at end
L5.append(0)
# add L4 to the shifted L5
L3 = addPolyB(L4,L5)
return L3
Evaluating Big-Endian Polynomials
Horner’s Rule
zeroPoly = [ ]
Scalar multiply a polynomial p(x) by a constant c is the same code
So is the code for addInverse
def createM(m,n,k):
if m == 0:
return [ ]
else:
R = [ ] # initialisation of a row
for j in range(0,n):
R.append(k) # k appears in each of the n positions
M = [ ]
for i in range(0,m):
r = R.copy() # fresh copy of the row; avoid shared refs
M.append(r) # copy of R appears as each of the m rows
return M
Identity Matrix
Representation:
[
[ 1, 0,….. , 0 ]
[ 0, 1,….. , 0 ]
: : :
[ 0, 0,….. , 1 ]
]
def createI(m):
if m == 0:
return [ ]
else:
M = [ ] # initialisation
for i in range(0,m):
R = [ ] # a different R each time
for j in range(0,m):
if i==j:
R.append(1)
else:
R.append(0)
r = R.copy() # can we directly append R?
M.append(r)
return M
Dimensions of a Matrix
Well-formedness: Need to check if all rows are the same length.
def dim(M):
if M==[ ]:
return(True, 0,0)
else:
m = len(M) # number of rows
n = len(M[0]) # number of cols in 1st row
proper = True
i = 0
while proper and i < m:
if len(M[i]) == n: # row has n columns
i += 1
else: # row has =/= n columns
proper = False
return(proper, m, n)
Addition of Matrices
def transposeM(L):
proper, m, n = dim(L) # get dimensions
if proper:
A = createM(n,m,0) # create a n x m matrix
for i in range (0,m):
for j in range (0,n):
A[j][i] = L[i][j]
return(True,A)
else:
return (False, [ ])
Dot Product of two Vectors