Skip to article frontmatterSkip to article content

Sparsity and structure

Very large matrices cannot be stored all within primary memory of a computer unless they are sparse. A sparse matrix has structural zeros, meaning entries that are known to be exactly zero.} For instance, the adjacency matrix of a graph has zeros where there are no links in the graph. To store and operate with a sparse matrix efficiently, it is not represented as an array of all of its values. There is a variety of sparse formats available; for the most part, you can imagine that the matrix is stored as triples (i,j,Aij)(i,j,A_{ij}) for all the nonzero (i,j)(i,j) locations.

8.1.1Computing with sparse matrices

Most graphs with real applications have many fewer edges than the maximum possible n2n^2 for nn nodes. Accordingly, their adjacency matrices have mostly zero elements and should be represented sparsely. Julia functions to deal with sparse matrices are found in the SparseArrays package in the standard library.

Arithmetic operations such as +, -, *, and ^ respect and exploit sparsity if the matrix operands are sparse. However, matrix operations may substantially decrease the amount of sparsity, a phenomenon known as fill-in.

8.1.2Banded matrices

A particularly important type of sparse matrix is a banded matrix. Recall from Exploiting matrix structure that A\mathbf{A} has upper bandwidth pp if ji>pj-i > p implies Aij=0A_{ij}=0, and lower bandwidth qq if ij>qi-j > q implies Aij=0A_{ij}=0. We say the total bandwidth is p+q+1p+q+1. Banded matrices appear naturally in many applications where each element interacts directly with only a few neighbors.

Without pivoting, an LU factorization preserves bandwidth, but pivoting can change or destroy bandedness.

8.1.3Systems and eigenvalues

If given a sparse matrix, the backslash operator will automatically try a form of sparse-aware Cholesky or pivoted LU factorization. Depending on the sparsity pattern of the matrix, the time taken to solve the linear system may be well below the O(n3)O(n^3) needed in the general case.

For very large matrices, it’s unlikely that you will want to find all of its eigenvalues and eigenvectors. In Krylov subspaces we describe some of the math behind an algorithm that can find a selected number of eigenvalues of largest magnitude, lying to the extreme left or right, or nearest a given complex number.

8.1.4Exercises

  1. ⌨ Use spdiagm to build the 50×5050\times 50 matrices

    A=[2112112112],B=[211121121112].\mathbf{A} = \begin{bmatrix} -2 & 1 & & & \\ 1 & -2 & 1 & & \\ & \ddots & \ddots & \ddots & \\ & & 1 & -2 & 1 \\ & & & 1 & -2 \end{bmatrix}, \qquad \mathbf{B} = \begin{bmatrix} -2 & 1 & & & 1 \\ 1 & -2 & 1 & & \\ & \ddots & \ddots & \ddots & \\ & & 1 & -2 & 1 \\ 1 & & & 1 & -2 \end{bmatrix}.

    For each matrix, use spy and an inspection of the 5×55\times 5 submatrices in the corners to verify the correctness of your matrices.

  2. ⌨ This problem requires the matrix used in Demo 8.1.2; you can load it via

    url = "https://tobydriscoll.net/fnc-julia/_static/resources/smallworld.jld2"
    datafile = download(url)
    @load datafile A

    (a) Find the density of A\mathbf{A} (number of nonzeros divided by total number of elements), A2\mathbf{A}^2, A4\mathbf{A}^4, and A8\mathbf{A}^8. (You should find that it increases with the power of AA.)

    (b) The LU factors tend to at least partially retain sparsity. Find the density of the L\mathbf{L} and U\mathbf{U} factors of A\mathbf{A} using the built-in lu. (See Demo 2.9.1 for usage of lu in the sparse case.)

    (c) Repeat part (b) for the QR factorization using qr.

  3. ⌨ One use of adjacency matrices is to analyze the links between members of a collection. Obtain the adjacency matrix A\mathbf{A} from Demo 8.1.1 via the following:

    url = "https://tobydriscoll.net/fnc-julia/_static/resources/roswell.jld2"
    datafile = download(url)
    @load datafile A

    The matrix catalogs the links between web sites related to the town of Roswell, NM, with Aij=1A_{ij}=1 if and only if site ii links to site jj.

    (a) Verify numerically that the matrix does not include any links from a site to itself.

    (b) Verify numerically that A\mathbf{A} is not symmetric. (Thus, its graph is a directed one.)

    (c) How many sites in the group are not pointed to by any other sites in the group?

    (d) Which site points to the most other sites?

    (e) Which site is pointed to the most by the other sites? This is a crude way to establish the most important site.

    (f) There are 27902 possible ways to connect ordered pairs of sites. What fraction of these pairs is connected by a walk of links that is no greater than three in length?

  4. ⌨ The graph Laplacian matrix is L=DA\mathbf{L}=\mathbf{D}-\mathbf{A}, where A\mathbf{A} is the adjacency matrix and D\mathbf{D} is the degree matrix, a diagonal matrix with diagonal entries djj=i=1naijd_{jj}=\sum_{i=1}^n a_{ij}.

    Follow the directions in Exercise 3 to obtain an adjacency matrix A\mathbf{A}. Then find the five eigenvalues of L\mathbf{L} having largest magnitude.

  5. ⌨ See Exercise 7.1.5 for instructions on loading a matrix A\mathbf{A} that contains information about the appearances of 392,400 actors in 127,823 movies, as given by the Internet Movie Database. Specifically, Aij=1A_{ij}=1 if actor jj appeared in movie ii, and all other elements are zero.

    (a) What is the maximum number of actors appearing in any one movie?

    (b) How many actors appeared in exactly three movies?

    (c) Define C=ATA\mathbf{C}=\mathbf{A}^T\mathbf{A}. How many nonzero entries does C\mathbf{C} have? What is the interpretation of CijC_{ij}?

  6. ⌨ A matrix that arises from the Helmholtz equation for wave propagation can be specified using

    A = FNC.poisson(n) - k^2*I;

    where kk is a real parameter. Let n=50n=50.

    (a) Let k=1k=1. What is the size of A\mathbf{A}? What is its density?

    (b) Still with k=1k=1, use eigs to find the four largest and four smallest (in magnitude) eigenvalues of A\mathbf{A}. (See Demo 8.1.4 for examples.)

    (c) The eigenvalues are all real. Find a value of kk so that A\mathbf{A} has exactly three negative eigenvalues.