university of hawaii ics141: discrete mathematics for ...janst/141/lecture/19-matrices.pdf · ics...

24
13-1 ICS 141: Discrete Mathematics I – Fall 2011 University of Hawaii University of Hawaii ICS141: Discrete Mathematics for Computer Science I Dept. Information & Computer Sci., University of Hawaii Jan Stelovsky based on slides by Dr. Baek and Dr. Still Originals by Dr. M. P. Frank and Dr. J.L. Gross Provided by McGraw-Hill

Upload: others

Post on 03-Apr-2020

20 views

Category:

Documents


0 download

TRANSCRIPT

13-1 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii

ICS141: Discrete Mathematics for Computer Science I

Dept. Information & Computer Sci., University of Hawaii

Jan Stelovsky based on slides by Dr. Baek and Dr. Still

Originals by Dr. M. P. Frank and Dr. J.L. Gross Provided by McGraw-Hill

13-2 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii

Lecture 19

Chapter 3. The Fundamentals 3.8 Matrices

13-3 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii 3.8 Matrices

n  A matrix is a rectangular array of objects (usually numbers).

n  An m × n (“m by n”) matrix has exactly m horizontal rows, and n vertical columns.

⎥⎥⎥

⎢⎢⎢

071532

A 3×2 matrix

n  Plural of matrix = matrices (say MAY-trih-sees) n  An n × n matrix is called a square matrix

13-4 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Applications of Matrices

n  Tons of applications, including:

n  Solving systems of linear equations

n  Computer Graphics, Image Processing

n  Games

n  Models within many areas of Computational Science & Engineering

n  Quantum Mechanics, Quantum Computing

n  Many, many more…

13-5 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Row and Column Order n  The rows in a matrix are usually indexed 1 to m from top

to bottom. n  The columns are usually indexed 1 to n from left to right. n  Elements are indexed by row, then by column.

⎥⎥⎥⎥

⎢⎢⎢⎢

==

mnmm

n

n

ij

aaa

aaaaaa

a

21

22221

11211

][A

13-6 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Matrix Equality

n  Two matrices A and B are considered equal iff they have the same number of rows, the same number of columns, and all their corresponding elements are equal.

⎥⎦

⎤⎢⎣

−≠⎥

⎤⎢⎣

− 061023

6123

13-7 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Matrix Sums

n  The sum A + B of two matrices A, B (which must have the same number of rows, and the same number of columns) is the matrix (also with the same shape) given by adding corresponding elements of A and B. A + B = [aij + bij]

⎥⎦

⎤⎢⎣

−−=⎥

⎤⎢⎣

−+⎥⎦

⎤⎢⎣

− 511911

31139

8062

13-8 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Matrix Products n  For an m × k matrix A and a k × n matrix B,

the product AB is the m × n matrix:

n  I.e., the element of AB indexed (i, j) is given by the vector dot product of the i-th row of A and the j-th column of B (considered as vectors).

n  Note: Matrix multiplication is not commutative!

∑=

=+++=

==k

jikjikjijiij

ij

babababac

c

12211

][

CAB

13-9 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Matrix Product Example

⎥⎦

⎤⎢⎣

−=

⎥⎦

⎤⎢⎣

⋅+⋅+−⋅⋅+⋅+⋅

⋅−+⋅+−⋅⋅−+⋅+⋅=

⎥⎥⎥

⎢⎢⎢

⎡ −

⋅⎥⎦

⎤⎢⎣

⎡ −

2301

0300)1(21320020)1(01)1(01)1(2100

010210

302110

13-10 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Matrix Product Example

⎥⎦

⎤⎢⎣

⎡=⎥

⎤⎢⎣

⎡=

1112

113201

BA and

n  Because A is a 2×3 matrix and B is a 2×2 matrix, the product AB is not defined.

13-11 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Matrix Multiplication: Non-Commutative

n  Matrix multiplication is not commutative! n  A: m × n matrix and B: r × s matrix

n  AB is defined when n = r n  BA is defined when s = m n  When both AB and BA are defined, generally they

are not the same size unless m = n = r = s n  If both AB and BA are defined and are the same size,

then A and B must be square and of the same size n  Even when A and B are both n × n matrices,

AB and BA are not necessarily equal

13-12 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Matrix Product Example

⎥⎦

⎤⎢⎣

⎡=

3523

AB ⎥⎦

⎤⎢⎣

⎡=

2334

BA

⎥⎦

⎤⎢⎣

⎡=⎥

⎤⎢⎣

⎡=

1112

1211

BA and

13-13 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Matrix Multiplication Algorithm procedure matmul(matrices A: m × k, B: k × n)

for i := 1 to m

for j := 1 to n begin

cij := 0

for q := 1 to k

cij := cij + aiqbqj

end {C = [cij] is the product of A and B}

What’s the Θ of its time complexity?

Θ(m)·{

Θ(n)·(

Θ(1)+

Θ(k) ·

Θ(1))}

Answer: Θ(mnk)

13-14 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii

n

n

Identity Matrices n  The identity matrix of order n, In, is the rank-n

square matrix with 1’s along the upper-left to lower-right diagonal, and 0’s everywhere else.

⎥⎥⎥⎥

⎢⎢⎢⎢

=⎥⎦

⎤⎢⎣

⎩⎨⎧

===

100

010001

if 0 if 1

][

jiji

ijn δI

Kronecker Delta

∀1 ≤ i, j ≤ n

13-15 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Matrix Inverses

n  For some (but not all) square matrices A, there exists a unique multiplicative inverse A−1 of A, a matrix such that A−1A = In.

n  If the inverse exists, it is unique, and A−1A = AA−1.

n  We won’t go into the algorithms for matrix inversion...

13-16 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Powers of Matrices If A is an n × n square matrix and p ≥ 0, then: n  Ap = AAA···A (and A0 = In)

n  Example:

p times

⎥⎦

⎤⎢⎣

−−=

⎥⎦

⎤⎢⎣

−−⋅⎥⎦

⎤⎢⎣

−=

⎥⎦

⎤⎢⎣

−⋅⎥⎦

⎤⎢⎣

−⋅⎥⎦

⎤⎢⎣

−=⎥

⎤⎢⎣

2334

1223

0112

0112

0112

0112

0112 3

13-17 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Matrix Transposition

n  If A = [aij] is an m × n matrix, the transpose of A (often written At or AT) is the n × m matrix given by At = B = [bij] = [aji] (1 ≤ i ≤ n,1 ≤ j ≤ m)

⎥⎥⎥

⎢⎢⎢

−=⎥⎦

⎤⎢⎣

−−231102

210312 t

13-18 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Symmetric Matrices n  A square matrix A is symmetric iff A = At.

I.e., ∀i, j ≤ n: aij = aji .

n  Which of the below matrices is symmetric?

⎥⎥⎥

⎢⎢⎢

211120103

⎥⎥⎥

⎢⎢⎢

213101312

⎥⎥⎥

⎢⎢⎢

111111

13-19 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Zero-One Matrices

n  Useful for representing other structures. n  E.g., relations, directed graphs (later on)

n  All elements of a zero-one matrix are either 0 or 1. n  E.g., representing False & True respectively.

n  The join of A, B (both m × n zero-one matrices): n  A ∨ B = [aij ∨ bij]

n  The meet of A, B: n  A ∧ B = [aij ∧ bij] = [aij bij]

13-20 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Join and Meet Example

⎥⎦

⎤⎢⎣

⎡=⎥

⎤⎢⎣

∨∨∨

∨∨∨=∨

011111

001110011001

BA

⎥⎦

⎤⎢⎣

⎡=⎥

⎤⎢⎣

∧∧∧

∧∧∧=∧

010000

001110011001

BA

and ⎥⎦

⎤⎢⎣

⎡=⎥

⎤⎢⎣

⎡=

011010

010101

BA

13-21 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Boolean Products n  Let A = [aij] be an m × k zero-one matrix and

B = [bij] be a k × n zero-one matrix,

n  The boolean product of A and B is like normal matrix multiplication, but using ∨ instead of +, and ∧ instead of × in the row-column “vector dot product”:

⎥⎦

⎤⎢⎣

⎡∧=== ∨

=ji

k

ij bac 1

][CA⊙B

13-22 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii

⎥⎥⎥

⎢⎢⎢

=

⎥⎥⎥

⎢⎢⎢

∨∨∨

∨∨∨

∨∨∨

=

⎥⎥⎥

⎢⎢⎢

∧∨∧∧∨∧∧∨∧

∧∨∧∧∨∧∧∨∧

∧∨∧∧∨∧∧∨∧

=

011110011

000101101000000101

)10()01()10()11()00()11()11()00()11()10()01()10()10()01()10()11()00()11(

Boolean Products Example n  Find the Boolean product of A and B, where

⎥⎥⎥

⎢⎢⎢

=

011001

A ⎥⎦

⎤⎢⎣

⎡=

110011

B

A⊙B

13-23 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Boolean Powers n  For a square zero-one matrix A, and any k ≥ 0,

the k-th Boolean power of A is simply the Boolean product of k copies of A.

A[k] = A ⊙ A ⊙ ··· ⊙ A

A[0] = In

k times

13-24 ICS 141: Discrete Mathematics I – Fall 2011

University of Hawaii University of Hawaii Matrices as Functions n  An m × n matrix A = [aij] of members of a set S

can be encoded as a partial function fA: N×N → S,

such that for i < m, j < n, fA(i, j) = aij.

n  By extending the domain over which fA is defined, various types of infinite and/or multidimensional matrices can be obtained.