Skip to main content

Subsection 8.5.1 Additional homework

Homework 8.5.1.1.

When using iterative methods, the matrices are typically very sparse. The question then is how to store a sparse matrix and how to perform a matrix-vector multiplication with it. One popular way is known as compressed row storage that involves three arrays:

  • 1D array nzA (nonzero A) which stores the nonzero elements of matrix \(A \text{.}\) In this array, first all nonzero elements of the first row are stored, then the second row, etc. It has size nnzeroes (number of nonzeroes).

  • 1D array ir which is an integer array of size \(n + 1 \) such that ir( 1 ) equals the index in array nzA where the first element of the first row is stored. ir( 2 ) then gives the index where the first element of the second row is stored, and so forth. ir( n+1 ) equals nnzeroes + 1. Having this entry is convenient when you implement a matrix-vector multiplication with array nzA.

  • 1D array ic of size nnzeroes which holds the column indices of the corresponding elements in array nzA.

  1. Write a function

    [ nzA, ir, ic ] = Create_Poisson_problem_nzA( N )
    
    that creates the matrix \(A \) in this sparse format.

  2. Write a function

    y = SparseMvMult( nzA, ir, ic, x )
    
    that computes \(y = A x \) with the matrix \(A \) stored in the sparse format.