Date post: | 01-Jan-2016 |
Category: |
Documents |
Upload: | adena-cantrell |
View: | 24 times |
Download: | 0 times |
CS 177 Week 9 Recitation Slides
1
Matrix EncodingMatrix Multiplication
Trees as ListsColumnar Transposition
04/20/23
Matrix Encoding: Row by Row
04/20/233
A
1 1 0
3 2 8
5 -3 5
0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,1
A = [1, 1, 0, 3, 2, 8, 5, -3, 5]rowLength = 3A[rowLength*y +x]
Matrix Encoding: Column by Column
04/20/234
A
1 1 0
3 2 8
5 -3 5
0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,1
A = [1, 3, 5, 1, 2, -3, 0, 8, 5]columnLength = 3A[columnLength*y +x]
Matrix Multiplication
04/20/235
0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,1
0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,1
0,0 0,1 0,2
1,0 1,1 1,2
2,0 2,1 2,1
= *
C A B
C A B
Matrix Multiplication
04/20/236
1 1 1
2 2 2
3 3 3
1 2 3
4 5 6
7 8 9
12
C A B
C0,0 = A
0,0 * B0,0 + A
0,1 * B1,0 + A
0,2 * B2,0
12 1 1 1 4 1 7
Matrix Multiplication
04/20/237
1 1 1
2 2 2
3 3 3
1 2 3
4 5 6
7 8 9
12 15
C A B
C0,1 = A
0,0 * B0,1 + A
0,1 * B1,1 + A
0,2 * B2,1
15 1 2 1 5 1 8
Matrix Multiplication
04/20/238
1 1 1
2 2 2
3 3 3
1 2 3
4 5 6
7 8 9
12 15 18
C A B
C0,2 = A
0,0 * B0,2 + A
0,1 * B1,2 + A
0,2 * B2,2
18 1 3 1 6 1 9
Matrix Multiplication
04/20/239
1 1 1
2 2 2
3 3 3
1 2 3
4 5 6
7 8 9
12 15 18
24
C A B
C1,0 = A
1,0 * B0,0 + A
1,1 * B1,0 + A
1,2 * B2,0
24 2 1 2 4 2 7
Matrix Multiplication
04/20/2310
1 1 1
2 2 2
3 3 3
1 2 3
4 5 6
7 8 9
12 15 18
24 30
C A B
C1,1 = A
1,0 * B0,1 + A
1,1 * B1,1 + A
1,2 * B2,1
30 2 2 2 5 2 8
Matrix Multiplication
04/20/2311
1 1 1
2 2 2
3 3 3
1 2 3
4 5 6
7 8 9
12 15 18
24 30 36
C A B
C1,2 = A
1,0 * B0,2 + A
1,1 * B1,2 + A
1,2 * B2,2
36 2 3 2 6 2 9
Matrix Multiplication
04/20/2312
1 1 1
2 2 2
3 3 3
1 2 3
4 5 6
7 8 9
12 15 18
24 30 36
36
C A B
C2,0 = A
2,0 * B0,0 + A
2,1 * B1,0 + A
2,2 * B2,0
36 3 1 3 4 3 7
Matrix Multiplication
04/20/2313
1 1 1
2 2 2
3 3 3
1 2 3
4 5 6
7 8 9
12 15 18
24 30 36
36 45
C A B
C2,1 = A
2,0 * B0,1 + A
2,1 * B1,1 + A
2,2 * B2,1
45 3 2 3 5 3 8
Matrix Multiplication
04/20/2314
1 1 1
2 2 2
3 3 3
1 2 3
4 5 6
7 8 9
12 15 18
24 30 36
36 45 54
C A B
C2,2 = A
2,0 * B0,2 + A
2,1 * B1,2 + A
2,2 * B2,2
54 3 3 3 6 3 9
04/20/2315
1,0 = 1,0 * 0,0 + 1,1 * 1,0 + 1,2 * 2,0
2,2 = 2,0 * 0,2 + 2,1 * 1,2 + 2,2 * 2,22,1 = 2,0 * 0,1 + 2,1 * 1,1 + 2,2 * 2,12,0 = 2,0 * 0,0 + 2,1 * 1,0 + 2,2 * 2,0
1,1 = 1,0 * 0,1 + 1,1 * 1,1 + 1,2 * 2,11,2 = 1,0 * 0,2 + 1,1 * 1,2 + 1,2 * 2,2
0,1 = 0,0 * 0,1 + 0,1 * 1,1 + 0,2 * 2,10,0 = 0,0 * 0,0 + 0,1 * 1,0 + 0,2 * 2,0
0,2 = 0,0 * 0,2 + 0,1 * 1,2 + 0,2 * 2,2
Indexes in Matrix Multiplication
Matrix Multiplication
04/20/2316
A = [[1, 1, 1] ,[2,2,2] ,[3,3,3]] B = [[1, 2, 3], [4,5,6] ,[7,8, 9]] C = [3*[0] for i in range(3)] for i in range(3): for j in range(3): for k in range(3): C[i][j] = C[i][j] + ( A[i][k] * B[k][j])
Trees as Lists Lists are powerful structures Lists can be used to build trees
04/20/2317
Root
Leaf1 Leaf2 Leaf3
Root
L1
L2
L3 L4 L5L0
L1
L2
L3 L4
L6
L0
L5
Root
Simple trees>>> Tree1 = ['L0', 'L1']
>>>Tree2 = ['L0', 'L1', 'L2']
>>>Tree3 = ['L0', 'L1', 'L2','L3', 'L4', 'L5']
04/20/2318
L1
Tree1
L0 L1 L2L0
Tree2
L0
Tree3
L1 L2 L3 L4 L5
>>> Tree = [['L0', 'L1'], ['L2','L3']]
What is tree[1][0]?
What is tree[0][1]?
04/20/2319
1
Tree
L1L0
0 1
0
L2 L30 1
QuestionBuild the list representing the following tree, (and call it
tree)
04/20/2320
Tree
BA E FC D
AnswerBuild the following tree
>>> Tree = [[['X','Y'],'B'],[['Z'],'D'] ,['E','F'] ]
04/20/2323
Tree
B E FD
X Y Z
0
1 2
0
0 1
10 0
0
11
Answer>>>Tree[0][0][0]
‘X’
>>>Tree[1][0][0]
‘Z’
>>>Tree[2][0]
‘E’
04/20/2325
Tree
B E FD
X Y Z
0
1 2
0
0 1
10 0
0
11
Columnar Transposition
S = [[‘P’, ‘R’, ‘O’], [‘G’, ‘R’, ‘A’], [‘M’, ‘~’, ‘~’]]
How to get “PROGRAM”? How to get “MGP~RP~AO”?
26
P R O
G R A
M ~ ~
D = ""for i in range(0,3): for j in range(0,3): D = D + S[i][j]print(D)
D = ""for i in range(0,3): for j in range(0,3): D = D + S[3-1-j][i]print(D)
27