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) for all the nonzero (i,j) locations.
Most graphs with real applications have many fewer edges than the maximum possible n2 for n 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.
A particularly important type of sparse matrix is a banded matrix. Recall from Exploiting matrix structure that A has upper bandwidthp if j−i>p implies Aij=0, and lower bandwidthq if i−j>q implies Aij=0. We say the total bandwidth is p+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.
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) 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.
For each matrix, use spy and an inspection of the 5×5 submatrices in the corners to verify the correctness of your matrices.
⌨ 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 (number of nonzeros divided by total number of elements), A2, A4, and A8. (You should find that it increases with the power of A.)
(b) The LU factors tend to at least partially retain sparsity. Find the density of the L and U factors of 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.
⌨ One use of adjacency matrices is to analyze the links between members of a collection. Obtain the adjacency matrix 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=1 if and only if site i links to site j.
(a) Verify numerically that the matrix does not include any links from a site to itself.
(b) Verify numerically that 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?
⌨ The graph Laplacian matrix is L=D−A, where A is the adjacency matrix and D is the degree matrix, a diagonal matrix with diagonal entries djj=∑i=1naij.
Follow the directions in Exercise 3 to obtain an adjacency matrix A. Then find the five eigenvalues of L having largest magnitude.
⌨ See Exercise 7.1.5 for instructions on loading a matrix A that contains information about the appearances of 392,400 actors in 127,823 movies, as given by the Internet Movie Database. Specifically, Aij=1 if actor j appeared in movie i, 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. How many nonzero entries does C have? What is the interpretation of Cij?
⌨ A matrix that arises from the Helmholtz equation for wave propagation can be specified using
A = FNC.poisson(n) - k^2*I;
where k is a real parameter. Let n=50.
(a) Let k=1. What is the size of A? What is its density?
(b) Still with k=1, use eigs to find the four largest and four smallest (in magnitude) eigenvalues of A. (See Demo 8.1.4 for examples.)
(c) The eigenvalues are all real. Find a value of k so that A has exactly three negative eigenvalues.