Arrays: computing with many numbers Some perspective We have so far - - PowerPoint PPT Presentation

arrays computing with many numbers some perspective
SMART_READER_LITE
LIVE PREVIEW

Arrays: computing with many numbers Some perspective We have so far - - PowerPoint PPT Presentation

Arrays: computing with many numbers Some perspective We have so far (mostly) looked at what we can do with single numbers (and functions that return single numbers). Things can get much more interesting once we allow not just one, but many


slide-1
SLIDE 1

Arrays: computing with many numbers

slide-2
SLIDE 2

Some perspective

— We have so far (mostly) looked at what we can do with single

numbers (and functions that return single numbers).

— Things can get much more interesting once we allow not just one,

but many numbers together.

— It is natural to view an array of numbers as one object with its

  • wn rules.

— The simplest such set of rules is that of a vector.

Demo “Sound as Vectors”

slide-3
SLIDE 3

A vector is an element of a Vector Space

x = 8 > > > < > > > : x1 x2 . . . xn 9 > > > = > > > ; = [x1 x2 · · · xn]T

Vector space 𝓦: A vector space is a set 𝒲 of vectors and a field ℱ of scalars with two

  • perations:

1) addition: 𝑣 + 𝑤 ∈ 𝒲, and 𝑣, 𝑤 ∈ 𝒲 2) multiplication : α * 𝑣 ∈ 𝒲, and 𝑣 ∈ 𝒲 , α ∈ ℱ

Vectors

𝑜-vector:

slide-4
SLIDE 4

The addition and multiplication operations must satisfy: (for 𝛽, 𝛾 ∈ ℱ and 𝑣,𝑤 ∈ 𝒲) Associativity: 𝑣 + 𝑤 + 𝑥 = 𝑣 + 𝑤 + 𝑥 Commutativity: 𝑣 + 𝑤 = 𝑤 + 𝑣 Additive identity: 𝑤 + 0 = 𝑤 Additive inverse: 𝑤 + −𝑤 = 0 Associativity wrt scalar multiplication: 𝛽 * 𝛾 * 𝑤 = 𝛽 * 𝛾 * 𝑤 Distributive wrt scalar addition: 𝛽 + 𝛾 * 𝑤 = 𝛽 * 𝑤 + 𝛾 * 𝑤 Distributive wrt vector addition: 𝛽 * 𝑣 + 𝑤 = 𝛽 * 𝑣 + 𝛽 * 𝑤 Scalar multiplication identity: 1 * 𝑣 = 𝑣

Vector Space

slide-5
SLIDE 5

Linear Functions

Function: The function 𝑔 takes vectors 𝒚 ∈ 𝒴 and transforms into vectors 𝒛 ∈ 𝒵 A function f is a linear function if (1) f (u+v) = f (u)+ f (v) (2) f (au) = a f (u) for any scalar a set 𝒴 (“input data”) set 𝒵 (“output data”)

slide-6
SLIDE 6

Iclicker question

2) Is 𝑔 𝑦 = 𝑏 𝑦 + 𝑐, 𝑔: ℛ → ℛ, 𝑏, 𝑐 ∈ ℛ and 𝑏, 𝑐 ≠ 0 a linear function? A) YES B) NO 1) Is 𝑔 𝑦 = |"|

" , 𝑔: ℛ → ℛ

a linear function? A) YES B) NO

slide-7
SLIDE 7

Matrices

  • 𝑛 ×𝑜-matrix
  • Linear functions 𝑔(𝒚) can be represented by a Matrix-Vector

multiplication.

  • Think of a matrix 𝑩 as a linear function that takes vectors 𝒚

and transforms them into vectors 𝒛 𝒛 = 𝑔(𝒚) → 𝒛 = 𝑩 𝒚

  • Hence we have:

𝑩 𝒗 + 𝒘 = 𝑩 𝒗 + 𝑩 𝒘 𝑩 𝛽 𝒗 = 𝛽 𝑩 𝒗 𝑩 = 𝐵## 𝐵#$ … 𝐵#% 𝐵$# 𝐵$$ … 𝐵$% ⋮ ⋮ ⋱ ⋮ 𝐵&# 𝐵&$ … 𝐵&%

slide-8
SLIDE 8

Matrix-Vector multiplication

  • Recall summation notation for matrix-vector multiplication 𝒛 = 𝑩 𝒚
  • You can think about matrix-vector multiplication as:

Linear combination of column vectors of A Dot product of 𝒚 with rows of A 𝒛 = 𝑦! 𝐁 : , 1 + 𝑦" 𝐁 : , 2 + ⋯ + 𝑦# 𝐁[: , 𝑜] 𝒛 = 𝐁 1, : 4 𝒚 ⋮ 𝐁 𝑛, : 4 𝒚

slide-9
SLIDE 9

Matrices operating on data

Data set: 𝒚 Data set: 𝒛 Rotation

𝒛 = 𝒈 𝒚

  • r

𝒛 = 𝑩 𝒚

slide-10
SLIDE 10

Example: Shear operator

Demo “Matrix transformations”

Matrix-vector multiplication for each vector (point representation in 2D):

𝑧# 𝑧$ = 1 𝑐 𝑏 1 𝑦# 𝑦$

1 0.25 1 1 1 = 1.25 1

𝑦' 𝑦(

1 0.25 1 1 = 0.25 1 1 0.25 1 1 = 1

We can do this better…

𝑧!! 𝑧"! ⋯ 𝑧!# 𝑧"# = 1 0.25 1 𝑦!! 𝑦"! … 𝑦!# 𝑦"#

𝒁 = 𝑩 𝒀

(2×𝑜) (2×2) (2×𝑜) 𝑜: number of data points in the set

slide-11
SLIDE 11

Matrices as operators

  • Data: grid of 2D points
  • Transform the data using matrix multiply

What can matrices do? 1. Shear 2. Rotate 3. Scale 4. Reflect 5. Can they translate?

slide-12
SLIDE 12

Rotation operator

Demo “Matrix transformations”

𝑧# 𝑧$ = cos(𝜄) −sin(𝜄) sin(𝜄) cos(𝜄) 𝑦# 𝑦$

𝜄 = 𝜌/6

slide-13
SLIDE 13

Scale operator

Demo “Matrix transformations”

𝑧# 𝑧$ = 𝑏 𝑐 𝑦# 𝑦$

3/2 1 3/2 3/2 3/2 2/3

slide-14
SLIDE 14

Reflection operator

Demo “Matrix transformations”

𝑧# 𝑧$ = −𝑏 −𝑐 𝑦# 𝑦$

−1 1

Reflect about y-axis Reflect about x and y-axis

−1 −1

slide-15
SLIDE 15

Translation (shift)

Demo “Matrix transformations”

𝑧# 𝑧$ = 1 1 𝑦# 𝑦$ + 𝑏 𝑐

𝑏 = 0.6; 𝑐 = 1.1

slide-16
SLIDE 16

Matrices operating on data

Demo “Matrices for geometry transformation”

Data set: 𝑩 Data set: 𝑪 Rotation

slide-17
SLIDE 17

Iclicker question

A triangle has vertices with coordinates (1,1), (2,1) and (2,4). The triangle is transformed by the matrix

1.5 0.75

What is the operation defined by the matrix above? a) Expand b) Shrink c) Scale d) Rotate e) Reflect What are the coordinates of the transformed triangle? a) (0.75,1.5), (0.75,3), (3,3) b) (1.5,0.75), (3,0.75), (3,3) c) (1.5,0.75), (1.5,1.5), (6,1.5) d) (0.75,1.5), (1.5,1.5), (1.5,6)

slide-18
SLIDE 18

Notation and special matrices

  • Square matrix: 𝑛 = 𝑜
  • Zero matrix:
  • Identity matrix
  • Symmetric matrix:
  • Permutation matrix:
  • Permutation of the identity matrix
  • Permutes (swaps) rows
  • Diagonal matrix: 𝐵$% = 0, ∀𝑗, 𝑘 | 𝑗 ≠ 𝑘
  • Triangular matrix:

⇢ 6 Aij = Aji [A] = [A]T

  • Aij = 0

[I] = [δij] ⇢

δij = ⇢ 1 i = j i 6= j

1 1 1 𝑏 𝑐 𝑑 = 𝑑 𝑏 𝑐 Lower triangular: 𝑀$% = ?𝑀$% , 𝑗 ≥ 𝑘 0, 𝑗 < 𝑘 Upper triangular: 𝑉$% = ?𝑉$% , 𝑗 ≤ 𝑘 0, 𝑗 > 𝑘

slide-19
SLIDE 19

More about matrices

  • Rank: the rank of a matrix 𝑩 is the dimension of the vector space generated

by its columns, which is equivalent to the number of linearly independent columns of the matrix.

  • Suppose 𝑩 has shape 𝑛×𝑜:
  • 𝑠𝑏𝑜𝑙 𝑩 ≤ min(𝑛, 𝑜)
  • Matrix 𝑩 is full rank: 𝑠𝑏𝑜𝑙 𝑩 = min(𝑛, 𝑜). Otherwise, matrix 𝑩 is

rank deficient.

  • Singular matrix: a square matrix 𝑩 is invertible if there exists a square matrix

𝐂 such that 𝑩𝑪 = 𝑪𝑩 = 𝑱. If the matrix is not invertible, it is called singular.

slide-20
SLIDE 20

Norms

slide-21
SLIDE 21

Example of Norms

Demo “Vector Norms”

slide-22
SLIDE 22

Unit Ball:

Set of vectors 𝒚 with norm 𝒚 = 1

Demo “Vector Norms”

slide-23
SLIDE 23

— Why should be 𝑞 ≥ 1 when calculating the p-norm?

𝑦' 𝑦(

(1,0) (0,1)

slide-24
SLIDE 24

Iclicker question

a) b) c) d) e)

slide-25
SLIDE 25

Norms and Errors

slide-26
SLIDE 26

Absolute and Relative Errors

Absolute error: a) 0.2240 b) 0.3380 c) 0.2513 Relative error: a) 2.59×10;< b) 2.81×10;<

slide-27
SLIDE 27

Matrix Norms

slide-28
SLIDE 28

Matrix Norms

slide-29
SLIDE 29

Matrix Norms

slide-30
SLIDE 30

Induced Matrix Norms

𝑩 # = max

1

X

23# %

𝐵21 𝑩 4 = max

2

X

13# %

𝐵21 𝑩 $ = max

5

𝜏5

𝜏$ are the singular value of the matrix 𝑩 Maximum absolute column sum of the matrix 𝑩 Maximum absolute row sum of the matrix 𝑩

slide-31
SLIDE 31

Properties of Matrix Norms

Demo “Matrix Norms”

slide-32
SLIDE 32

Iclicker question

Determine the norm of the following matrices: 1) 2)

a) 3 b) 4 c) 5 d) 6 e) 7

slide-33
SLIDE 33

Iclicker question

a) 90 b) 30 c) 20 d) 10 e) 5

slide-34
SLIDE 34
slide-35
SLIDE 35