Lecture 3 Scientific Computing: Numerical Linear Algebra

Lecture 3 Scientific Computing: Numerical Linear Algebra

Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Lecture 3 Scientific Computing: Numerical Linear Algebra Matthew J. Zahr CME 292 Advanced MATLAB for Scientific Computing Stanford University 9th April 2014 CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers 1 Dense vs. Sparse Matrices 2 Direct Solvers and Matrix Decompositions Background Types of Matrices Matrix Decompositions Backslash 3 Spectral Decompositions 4 Iterative Solvers Preconditioners Solvers CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Outline 1 Dense vs. Sparse Matrices 2 Direct Solvers and Matrix Decompositions Background Types of Matrices Matrix Decompositions Backslash 3 Spectral Decompositions 4 Iterative Solvers Preconditioners Solvers CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Assignment Create the following matrix (1000 rows/columns) 2−2 1 3 6 1 −2 1 7 6 7 6 7 6 1 −2 1 7 A = 6 . 7 6 .. .. .. 7 6 7 6 7 4 1 −2 1 5 1 −2 Then, run the following lines of code >> s = whos('A'); >> s.bytes How much storage does your matrix need? CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Sparse matrix storage formats Sparse matrix = matrix with relatively small number of non zero entries, compared to its size. m×n Let A 2 R be a sparse matrix with nz nonzeros. Dense storage requires mn entries. CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Sparse matrix storage formats (continued) Triplet format Store nonzero values and corresponding row/column Storage required = 3nz (2nz ints and nz doubles) Simplest but most inefficient storage format General in that no assumptions are made about sparsity structure Used by MATLAB (column-wise) 2 3 1 9 0 0 1 row = 1 2 1 2 5 3 3 4 1 5 68 2 0 0 07 6 7 6 7 col = 1 1 2 2 2 3 4 4 5 5 60 0 3 5 07 6 7 40 0 0 7 05 val = 1 8 9 2 4 3 5 7 1 1 0 4 0 0 1 CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Other sparse storage formats Compressed Sparse Row (CSR) format Store nonzero values, corresponding column, and pointer into value array corresponding to first nonzero in each row Storage required = 2nz + m Compressed Sparse Column (CSC) format Storage required = 2nz + n Diagonal Storage format Useful for banded matrices Skyline Storage format Block Compressed Sparse Row (BSR) format CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Break-even point for sparse storage m×n For A 2 R with nz nonzeros, there is a value of nz where sparse vs dense storage is more efficient. For the triplet format, the cross-over point is defined by 3nz = mn mn Therefore, if nz ≤ 3 use sparse storage, otherwise use dense format Cross-over point depends not only on m; n; nz but also on the data types of row, col, val Storage efficiency not only important consideration Data access for linear algebra applications Ability to exploit symmetry in storage CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Create Sparse Matrices Allocate space for m × n sparse matrix with nz nnz S = spalloc(m; n; nz) Convert full matrix A to sparse matrix S S = sparse(A) Create m × n sparse matrix with spare for nz nonzeros from triplet (row,col,val) S = spalloc(row,col,val,m; n; nz) Create matrix of 1s with sparsity structure defined by sparse matrix S R = spones(S) Sparse identity matrix of size m × n I = speye(m; n) CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Create Sparse Matrices Create sparse uniformly distributed random matrix From sparsity structure of sparse matrix S R = sprand(S) Matrix of size m × n with approximately mnρ nonzeros and condition number roughly κ (sum of rank 1 matrices) R = sprand(m; n; ρ, κ−1) Create sparse normally distributed random matrix R = sprandn(S) R = sprandn(m; n; ρ, κ−1) Create sparse symmetric uniformly distributed random matrix R = sprandn(S) R = sprandn(m; n; ρ, κ−1) Import from sparse matrix external format spconvert CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Create Sparse Matrices (continued) Create sparse matrices from diagonals (spdiags) Far superior to using diags More general Doesn't require creating unnecessary zeros Extract nonzero diagonals from matrix [B,d] = spdiags(A) Extract diagonals of A specified by d B = spdiags(A,d) Replaces the diagonals of A specified by d with the columns of B A = spdiags(B,d,A) Create an m × n sparse matrix from the columns of B and place them along the diagonals specified by d A = spdiags(B,d,m,n) CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Assignment Create the following matrix (1000 rows/columns) 2−2 1 3 6 1 −2 1 7 6 7 6 7 6 1 −2 1 7 A = 6 . 7 6 .. .. .. 7 6 7 6 7 4 1 −2 1 5 1 −2 using spdiags Then, run the following lines of code >> s = whos('A'); >> s.bytes How much storage does your matrix need? CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Sparse storage information m×n Let S 2 R sparse matrix Determine if matrix is stored in sparse format issparse(S) Number of nonzero matrix elements nz = nnz(S) Amount of nonzeros allocated for nonzero matrix elements nzmax(S) Extract nonzero matrix elements If (row, col, val) is sparse triplet of S val = nonzeros(S) [row,col,val] = find(S) CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Sparse and dense matrix functions 0 500 m×n Let S 2 R sparse matrix 1000 Convert sparse matrix to dense matrix 1500 A = full(S) 2000 Apply function (described by function handle func) to nonzero 2500 elements of sparse matrix F = spfun(func, S) 3000 Not necessarily the same as func(S) 3500 Consider func = @exp 4000 0 500 1000 1500 2000 2500 3000 3500 4000 Plot sparsity structure of matrix nz = 27538 spy(S) Figure: spy plot CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Reordering Functions Command Description amd Approximate minimum degree permutation Column approximate minimum degree colamd permutation Sparse column permutation based on nonzero colperm count dmperm Dulmage-Mendelsohn decomposition randperm Random permutation Symmetric approximate minimum degree symamd permutation symrcm Sparse reverse Cuthill-McKee ordering CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Sparse Matrix Tips Don't change sparsity structure (pre-allocate) Dynamically grows triplet Each component of triplet must be stored contiguously Accessing values (may be) slow in sparse storage as location of row/columns is not predictable If S(i,j) requested, must search through row, col to find i, j Component-wise indexing to assign values is expensive Requires accessing into an array If S(i,j) previously zero, then S(i,j)= c changes sparsity structure CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Rank m×n Rank of a matrix A 2 R Defined as the number of linearly independent columns rank A ≤ minfm; ng Full rank =) rank A = minfm; ng MATLAB: rank Rank determined using SVD >> [rank(rand(100,34)), rank(rand(100,1)*rand(1,34))] ans = 34 1 CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Direct Solvers and Matrix Decompositions Spectral Decompositions Iterative Solvers Norms Gives some notion of size/distance Defined for both vectors and matrices m Common examples forp vector, v 2 R T 2-norm: jjvjj2 = v v Pm p 1=p p-norm: jjvjjp = ( i=1 jvij ) 1-norm: jjvjj1 = max jvij MATLAB: norm(X,type) m×n Common examples for matrices, A 2 R 2-norm: jjAjj2 = σmax(A) qPm Pn 2 Frobenius-norm: jjAjjF = i=1 j=1 jAijj MATLAB: norm(X,type) Result depends on whether X is vector or matrix and on value of type MATLAB: normest Estimate matrix 2-norm For sparse matrices or large, full matrices CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Background Direct Solvers and Matrix Decompositions Types of Matrices Spectral Decompositions Matrix Decompositions Iterative Solvers Backslash Outline 1 Dense vs. Sparse Matrices 2 Direct Solvers and Matrix Decompositions Background Types of Matrices Matrix Decompositions Backslash 3 Spectral Decompositions 4 Iterative Solvers Preconditioners Solvers CME 292: Advanced MATLAB for SC Lecture 3 Dense vs. Sparse Matrices Background Direct Solvers and Matrix Decompositions Types of Matrices Spectral Decompositions Matrix Decompositions Iterative Solvers Backslash Determined System of Equations Solve linear system Ax = b (1) n×n by factorizing A 2 R For a general matrix, A, (1) is difficult to solve If A can be decomposed as A = BC then (1) becomes By = b (2) Cx = y If B and C are such that (2) are easy to solve, then the difficult problem in (1) has been reduced to two easy problems Examples of types of matrices that are \easy" to solve with Diagonal, triangular, orthogonal CME 292: Advanced MATLAB for SC Lecture 3 Dense vs.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    65 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