af2
DESCRIPTION
af2. The Queen Mother says “ Please be quite and let Patrick give the lecture. Thank you .”. Matrices/Arrays. Section 3.8. Matrices. A matrix is a rectangular array, possibly of numbers it has m rows and n columns if m = n then the matrix is square two matrices are equal if - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/1.jpg)
af2
![Page 2: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/2.jpg)
The Queen Mother says
“Please be quite and let Patrick give the lecture. Thank you.”
![Page 3: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/3.jpg)
Matrices/Arrays
Section 3.8
![Page 4: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/4.jpg)
Matrices
• A matrix is a rectangular array, possibly of numbers• it has m rows and n columns• if m = n then the matrix is square• two matrices are equal if
• they have same number of rows • they have same number of columns• corresponding entries are equal
4,33,32,31,3
4,23,22,21,2
4,13,12,11,1
aaaa
aaaa
aaaa
![Page 5: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/5.jpg)
.
...
.....
...
...
,2,1,
,22,21,2
,12,11,1
nmmm
n
n
aaa
aaa
aaa
A
],,,[ ,2,1, niii aaa
A is an m by n array
The ith row of A is a 1 by n matrix (a vector)
jm
j
j
a
a
a
,
,2
,1
.
.
.
The jth column of A is a m by 1 matrix (a vector)
![Page 6: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/6.jpg)
A is an m by n array
We also sometimes say that A is an array using the following shorthand
][ ijaA
i.e. A is an array with its (i,j)th element equal to ija
![Page 7: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/7.jpg)
addition
• To add two arrays/matrices A and B• they must both have the same number of rows• they must both have same number of columns
jijiji baC ,,,
![Page 8: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/8.jpg)
• for i := 1 to n do• for j := 1 to m do• C[i][j] := A[i][j] + B[i][j]
C = A + B
• How many array references are performed?• How is an array reference made? What is involved?• How many additions are performed?• Would it matter if the loops were the other way around?
![Page 9: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/9.jpg)
Multiplication
A B C
X =
Note: A is m k, B is k n, and C is m n
![Page 10: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/10.jpg)
jkkijijiji bababaC ,,,22,,11,, ......
• When • A is m k • B is k n • C = A.B• C is m n
• to compute an element of C
jx
k
xxiji baC ,
1,,
![Page 11: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/11.jpg)
• for i = 1 to m do• for j = 1 to n do
• C[i][j] := 0• for x = 1 to k do
• C[i][j] := C[i][j] + A[i][x] * B[x][j]
• Could we make it more efficient?• How many array access do we do?• How many multiplications and divisions are performed?• What is the complexity of array multiplication?• Is the ordering of the loops significant?
![Page 12: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/12.jpg)
• for i = 1 to m do• for j = 1 to n do
• tot := 0• for x = 1 to k do
• tot := tot + A[i][x] * B[x][j]• C[i][k] := tot
• Could we make it more efficient? • How many array access do we do? • How many multiplications and divisions are performed?• What is the complexity of array multiplication?• Is the ordering of the loops significant?
tot!
nmknm ....2 yes
square isarray when )( 3nO
You bet!
![Page 13: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/13.jpg)
Do an example on the blackboard!
ABCBA
03
11
42
220
013
112
401
![Page 14: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/14.jpg)
Is multiplication commutative?
• Does A x B = B x A?
• It might not be defined!
• Can you show that A x B might not be B x A?
![Page 15: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/15.jpg)
Show that A x B might not be same as B x A!
11
12
12
11BA
? Does BAAB
![Page 16: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/16.jpg)
(AB)C = A(BC)
Multiplication is associative
Does it matter?
• Assume • A is 30 x 20• B is 20 x 40• C is 40 x 10
• AB takes 30 x 40 x 20 operations = 24,000• the result array is 30 x 40
• call it R• RC takes 30 x 40 x 10 operations = 12,000• (AB)C takes 36,000 operations (mults)
• BC takes 20 x 10 x 40 operations = 8,000• the result array is 20 x 10
• call it R• AR takes 30 x 20 x 10 operations = 6,000• A(BC)takes 14,000 operations (mults)
![Page 17: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/17.jpg)
Identity Matrix
1000
0100
0010
0001
4 I
nI
AIAIA nn ..
0
1
][
ij
ij
ijn
ji
ji
I
No change!
![Page 18: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/18.jpg)
Raising a Matrix to a power
timesr
r AAAAA
AAAA
AAA
AA
IA
....
..
.3
2
1
0
![Page 19: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/19.jpg)
Transpose of a Matrix
Interchange rows with columns
fed
cbaX
fc
eb
da
X t
mjniabwherebA
aA
jiijijt
ij
11][
][
![Page 20: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/20.jpg)
Symmetric Matrix
tAA
jiij aa
Must be square
3847
8165
4693
7532
Think of distance
![Page 21: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/21.jpg)
Symmetric Matrix tAA jiij aa
Must be square
3847
8165
4693
7532
3847
165
93
2
Might represent as a triangular matrix and ensure thatwe never allow an access to element i,j where j > i
![Page 22: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/22.jpg)
Zero-One Matrices/arrays
• 1 might be considered as true• 0 might be considered as false• the array/matrix might then be considered as
• a relation• a graph• whatever
![Page 23: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/23.jpg)
Join of A and B (OR)
BAC
jijiji bac ,,,
So, it is like addition
![Page 24: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/24.jpg)
join
011
010
010
101BA
• Isn’t this like add?• Just replace x + y with max(x,y)?
011
111C
![Page 25: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/25.jpg)
join
• for i := 1 to n do• for j := 1 to m do• C[i][j] := A[i][j] + B[i][j]
• for i := 1 to n do• for j := 1 to m do• C[i][j] := max(A[i][j],B[i][j])
![Page 26: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/26.jpg)
meets of A and B (AND)
BAC
jijiji bac ,,,
So, it is like addition too?
![Page 27: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/27.jpg)
meets
011
010
010
101BA
• Isn’t this like add?• Just replace x + y with min(x,y)?
010
000C
![Page 28: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/28.jpg)
meets
• for i := 1 to n do• for j := 1 to m do• C[i][j] := A[i][j] + B[i][j]
• for i := 1 to n do• for j := 1 to m do• C[i][j] := min(A[i][j],B[i][j])
![Page 29: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/29.jpg)
Boolean product
)(...)()( ,,,22,,11,, jkkijijiji bababac
• Like multiplication but• replace times with and• replace plus with or
• The symbol is an O with a dot in the middle
![Page 30: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/30.jpg)
Boolean product
110
011
01
10
01
BA
011
110
011
BA
![Page 31: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/31.jpg)
• for i = 1 to m do• for j = 1 to n do
• C[i][j] := 0• for x = 1 to k do
• C[i][j] := C[i][j] + A[i][x] * B[x][j]
Boolean product
• for i = 1 to m do• for j = 1 to n do
• C[i][j] := 0• for x = 1 to k do
• C[i][j] := C[i][j] OR A[i][x] AND B[x][j]
Can we make this more efficient?
![Page 32: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/32.jpg)
• for i = 1 to m do• for j = 1 to n do
• C[i][j] := 0• for x = 1 to k do
• C[i][j] := C[i][j] + A[i][x] * B[x][j]
Boolean product
• for i = 1 to m do• for j = 1 to n do
• C[i][j] := 0• for x = 1 to k do
• C[i][j] := C[i][j] OR A[i][x] AND B[x][j]
])][[]][[(]][[:]][[ jxBxiAjiCjiC
Stop x loop whenever C[i][j] = 1
]))][[],][[min(],][[max(:]][[ jxBxiAjiCjiC
Replace with
![Page 33: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/33.jpg)
Raising a 0/1 array to a power
The rth boolean power
timesr
r AAAAA ][
![Page 34: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/34.jpg)
applications• matrices
• tables• weight and height• distance from a to b (and back again?)
• zero-one matrices• adjacency in a graph• relations• constraints
• Imagine matrix A • each row is an airline• column is flight numbers• A[i][j] gives us jth flight number for ith airline
• Imagine matrix B• each row is a flight number• each column is departure time
• join(A,B) gives • for each airline the departure times
• in constraint programming a zero-one matrix is a constraint• boolean product gives us path consistency
![Page 35: af2](https://reader035.vdocuments.site/reader035/viewer/2022062805/56814d4f550346895dba8196/html5/thumbnails/35.jpg)