1 / 35

Numerical Algs

Numerical Algs. Inner products, Matrix Multiplication, LU-Decomposition (factorization) Simplex Method, Iterative methods ( sqrt ). Optimization. Many problems in computing are of the form: Find the values that minimize (or maximizes) some function subject to constraints.

kirk
Download Presentation

Numerical Algs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Numerical Algs Inner products, Matrix Multiplication, LU-Decomposition (factorization) Simplex Method, Iterative methods (sqrt)

  2. Optimization • Many problems in computing are of the form: • Find the values that minimize (or maximizes) some function subject to constraints

  3. Example (unconstrained) • Find mean and standard deviation of a normal distribution that maximizes the probability of sampleswhere the probability is

  4. Example (unconstrained) • Find a quadratic polynomial where we have made measurementsso that the mean squared error in our measurements is minimized.

  5. Example • (Help me out here)

  6. Gradient Descent

  7. Newton Iteration

  8. Matrices • This REALLY should be review for you…. • A matrix is a 2D array of numbers (real or complex) • The number of rows is usually called • The number of columns is usually called • Matrices are represented by upper case letters • Matrix elements two subscripts • is the element in row and column of matrix • Indices start at 1, not zero • Sometimes elements are written lowercase, sometime uppercase. (sloppy) • Matrix multiplication is special

  9. Matrix + Matrix • Addition/Subtraction is done element by element

  10. Matrix * Scalar • Done in parallel • Symmetric ()

  11. Matrix Transpose • Use superscript to indicate transpose. • Exchange the order of subscripts (rows become columns)

  12. Slicing • In this class I will use slicing to indicate a sub-array. • My notation is something like Matlab’s. • Concatenation • I will use the | operator to concatenate matrices: , , or

  13. Vectors • A vector is a single-column matrix. • We write them with lowercase boldface letters. • A row-vector is a single-row matrix. We always write them as to indicate they are the transpose of a column matrix.

  14. Inner Product • Defined for two vectors Other notations for inner product: , , Also called the ‘dot’ product.

  15. Types of Matrix • Zero • Identity • Symmetric • Lower Triangular • Strict Lower Triangular • Upper & Strict Upper • Banded

  16. Accessing Matrix Elements If we know the dimensions of the array ahead of time we can use a fixed-size 2D array. But usually the dimensions are decided at runtime. I cannot pass this as a parameter to a function that operates on arrays with a variable size

  17. Writing functions for 2D arrays • We need to do some extra work to be able to pass matrices arount

  18. Accessing Matrix Elements • The layout is ‘contiguous’ in memory. • The is row major order. • The difference between items on the same row and different columns is _______ • The difference between items on the same column and different rows is _______ • The index of the item at row and column is ________

  19. Accessing Matrix Elements • Other API’s use different layout (Fortran, OpenGL, MatLAB, …) • This is column major order. • The difference between items on the same row and different columns is _______ • The difference between items on the same column and different rows is _______ • The index of the item at row and column is ________ • LaPACK is the most commonly used library for math. • It is implemented in Fortran, and called from c / c++.

  20. Representing a slice (MatLAB style) Let denote the numbers So Consider int A[] = Row 1 of has indices 0:1:2. Column 1 of A has indices 0:3:9.

  21. Basic idea of a slice • Keep track of the start index (or pointer) • Keep track of the number of values (or end index) • Keep track of the difference between consecutive values (stepsize)

  22. C++ Slice • std::slice • Start • Size (number of items) • Step (difference between consecutive items) • std::slice(0, 3, 2) => 0, 2, 4 Valarray<double> x; ….. x[slice(0,3,2)] => x[0], x[2], x[4] • I don’t use std::slice often (I write my own matrix class or use another library) • The idea is very important

  23. Matrix Views & Submatrices • What if we want a submatrix that is not a single row or column? How can we represent ? • We need a 2D version of a slice • Keep track of… • Linear index of in this case • Offset between consecutive rows is , in this case 3 • Offset between consecutive columns is • Keep track of the sizes (in this case )

  24. Matrix View • Imagine that library code & functions always operate on views of some other underlying data. • A vector view might need the following: • A pointer to the memory • Offset to the first item • Offset between consecutive items • The number of items in the vector. • A matrix view might need the following: • Pointer to the memory • Offset to the first item • Offset between consecutive rows • Offset between consecutive items • Number of rows • Number of columns In the STL these are called std::slice_array but do not seem to be used often. There are many custom implementations of this idea though. In the STL these are called std::gslice_array but do not seem to be used often. There are many custom implementations of this idea though.

  25. Matrix Multiplication If then Dimensions must agree: The number of columns in A must match the number of rows in B

  26. Given what is the cost of Matrix-Multiply?

  27. Inner Products

  28. Vector Norms

  29. Outer Products

  30. Valarrays

  31. Matrix Transpose

  32. Inverse matrix (2 x 2)

  33. LU Decomposition

More Related