Part II Arrays—Computing with Many Numbers

Part II Arrays—Computing with Many Numbers

Part II Arrays—Computing with Many Numbers 1 Part 2: Table of Contents i Modeling the World with Arrays Norms and Errors The ‘Undo’ Button for Linear Operations: LU LU: Applications Repeating Linear Operations: Eigenvalues and Steady States Eigenvalues: Applications 2 Part 2: Table of Contents ii Approximate Undo: SVD and Least Squares SVD: Applications 3 Modeling the World with Arrays Some Perspective i • 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 own rules. The simplest such set of rules is that of a vector. • A 2D array of numbers can also be looked at as a matrix. • So it’s natural to use the tools of computational linear algebra. 4 Some Perspective ii • ‘Vector’ and ‘matrix’ are just representations that come to life in many (many!) applications. The purpose of this section is to explore some of those applications. 5 Vectors i What’s a vector? An array that defines addition and scalar multiplication with reasonable rules such as u + (v + w) = (u + v) + w v + w = w + v α(u + v) = αu + αv These axioms generally follow from properties of “+” and “·” operators 6 Vectors from a CS Perspective i What would the concept of a vector look like in a programming language (e.g. Java)? In a sense, ‘vector’ is an abstract interface, like this: (Along with guarantees that add and multiply interact appropriately.) 7 Vectors in the ‘Real World’ i Demo: Images as Vectors (click to visit) Demo: Sounds as Vectors (click to visit) Demo: Shapes as Vectors (click to visit) 8 Matrices i What does a matrix do? It represents a linear function between two vector spaces f : U ! V in terms of bases u1;:::; un of U and v1;:::; vm of V. Let u = α1u1 + ··· + αnun and v = β1v1 + ··· + βmvm: 9 Matrices ii Then f can always be represented as a matrix that obtains the βs from the αs: 0 1 0 1 0 1 a11 ··· a1n α1 β1 B . .. C B . C B . C @ . A @ . A = @ . A : am1 ··· amn αn βm 10 Example: The ‘Frequency Shift’ Matrix Assume both u and v are linear combination of sounds of dif- ferent frequencies: u = α1u110 Hz + α2u220 Hz + ··· + α4u880 Hz (analogously for v, but with βs). What matrix realizes a ‘fre- quency doubling’ of a signal represented this way? 0 1 0 1 0 1 0 0 0 0 α1 β1 B C B α C B β C B 1 0 0 0 C B 2 C B 2 C B C B C = B C @ 0 1 0 0 A @ α3 A @ β3 A 0 0 1 0 α4 β4 11 Matrices in the ‘Real World’ i What are some examples of matrices in applications? Demo: Matrices for geometry transformation (click to visit) Demo: Matrices for image blurring (click to visit) In-class activity: Computational Linear Algebra 12 Graphs as Matrices i How could this (directed) graph be written as a matrix? 2 3 0 1 4 0 1 1 1 0 0 0 B C B 1 1 C B C B 1 1 1 C B C B C @ 1 A 1 1 13 Matrices for Graph Traversal: Technicalities i What is the general rule for turning a graph into a matrix? If there is an edge from node i to node j, then Aji = 1. (otherwise zero) What does the matrix for an undirected graph look like? Symmetric. 14 Matrices for Graph Traversal: Technicalities ii How could we turn a weighted graph (i.e. one where the edges have weights–maybe ‘pipe widths’) into a matrix? Allow values other than zero and one for the entries of the matrix. 15 Graph Matrices and Matrix-Vector Multiplication i If we multiply a graph matrix by the ith unit vector, what hap- pens? 2 3 0 1 4 16 Graph Matrices and Matrix-Vector Multiplication ii 0 1 0 1 0 1 1 1 0 0 0 1 1 B C B C B C B 1 1 C B 0 C B 1 C B C B C B C B 1 1 1 C B 0 C = B 1 C : B C B C B C B C B C B C @ 1 A @ 0 A @ 0 A 1 1 0 1 We get a vector that indicates (with a 1) all the nodes that are reachable from node i. 17 Demo: Matrices for graph traversal (click to visit) 18 Markov chains i 19 Markov chains ii Consider the following graph of states: Surf the web Eat Study Suppose this is an accurate model of the behavior of the av- erage student. :) Can this be described using a matrix? 20 Markov chains iii Important assumption: Only the most recent state matters to determine probability of next state. This is called the Markov property, and the model is called a Markov chain. Write transition probabilities into matrix as before: (Order: surf, study, eat–‘from’ state along columns) 0 1 :8 :6 :8 B C A = @ :2 :3 0 A 0 :1 :2 Observe: Columns add up to 1, to give sensible probability distribution of next states. Given probabilities of states 21 Markov chains iv p = (psurf ; pstudy; peat), Ap gives us the probabilities after one unit of time has passed. 22 Storing Sparse Matrices i Some types of matrices (including graph matrices) contain many zeros. Storing all those zero entries is wasteful. How can we store them so that we avoid storing tons of zeros? • Python dictionaries (easy, but not efficient) • Using arrays...? 23 Storing Sparse Matrices Using Arrays i How can we store a sparse matrix using just arrays? For ex- ample: 0 0 2 0 3 1 B C B 1 4 C B C @ 5 A 6 7 Idea: ‘Compressed Sparse Row’ (‘CSR’) format • Write all non-zero values from top-left to bottom-right • Write down what column each value was in • Write down the index where each row started 24 Storing Sparse Matrices Using Arrays ii RowStarts = 0 2 4 5 7 (zero-based) Columns = 1 3 0 1 2 0 3 (zero-based) Values = 2 3 1 4 5 6 7 25 Demo: Sparse Matrices in CSR Format (click to visit) 26 Norms and Errors Norms What’s a norm? • A generalization of ‘absolute value’ to vectors. Rn R+ • f (x): ! 0 , returns a ‘magnitude’ of the input vector • In symbols: Often written kxk. Define norm. Rn R+ A function kxk : ! 0 is called a norm if and only if 1. kxk > 0 , x 6= 0. 2. kγxk = jγj kxk for all scalars γ. 3. Obeys triangle inequality kx + yk 6 kxk + kyk 27 Examples of Norms i What are some examples of norms? The so-called p-norms: 0 1 x 1 q B C p p p @ A = jx1j + ··· + jxnj (p > 1) x n p p = 1; 2; 1 particularly important 28 Demo: Vector Norms (click to visit) 29 Norms and Errors i If we’re computing a vector result, the error is a vector. That’s not a very useful answer to ‘how big is the error’. What can we do? Apply a norm! How? Attempt 1: Magnitude of error 6= ktrue valuek−kapproximate valuek WRONG! Attempt 2: Magnitude of error = ktrue value − approximate valuek 30 Absolute and Relative Error i What are the absolute and relative errors in approximating the location of Siebel center (40:114; −88:224) as (40; −88) using the 2-norm? ! ! ! 40:114 40 0:114 − = −88:224 −88 −:224 Absolute magnitude; ! 40:114 ≈ 96:91 −88:224 2 31 Absolute and Relative Error ii Absolute error: ! 0:114 ≈ :2513 −:224 2 Relative error: :2513 ≈ :00259: 96:91 But: Is the 2-norm really the right norm here? 32 Demo: Calculate geographic distances using http://tripstance.com • Siebel Center is at 40.113813,-88.224671. (latitude, longitude) • Locations in that format are accepted in the location boxes. • What’s the distance to the nearest integer lat/lon intersection, 40,-88? • How does distance relate to lat/lon? Only lat? Only lon? 33 Matrix Norms i What norms would we apply to matrices? • Easy answer: ‘Flatten’ matrix as vector, use vector norm. This corresponds to an entrywise matrix norm called the Frobenius norm, s X 2 kAkF := aij: i;j 34 Matrix Norms ii • However, interpreting matrices as linear functions, what we are really interested in is the maximum amplification of the norm of any vector multiplied by the matrix, kAk := max kAxk : kxk=1 These are called induced matrix norms, as each is associated with a specific vector norm k·k. • The following are equivalent: k k x k k= Ax y 1 max = max A = max kAyk = kAk : kxk6=0 kxk kxk6=0 kxk kyk=1 |{z} y 35 Matrix Norms iii • Logically, for each vector norm, we get a different matrix norm, so that, e.g. for the vector 2-norm kxk2 we get a matrix 2-norm kAk2, and for the vector 1-norm kxk1 we get a matrix 1-norm kAk1. 36 Demo: Matrix norms (click to visit) In-class activity: Matrix norms 37 Properties of Matrix Norms i Matrix norms inherit the vector norm properties: 1. kAk > 0 , A 6= 0. 2. kγAk = jγj kAk for all scalars γ. 3. Obeys triangle inequality kA + Bk 6 kAk + kBk But also some more properties that stem from our definition: 1.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    162 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us