Skip to main content

Scientific Computing

Sparse Matrices an...

Finite Differ... []

Scientific Computing

Sparse Matrices an...

Jacobi and Re... []

This material has been adapted from material by Martin Robinson from the "Scientific Computing" module of the SABS R³ Center for Doctoral Training.

This material has been adapted from material by Martin Robinson from the "Scientific Computing" module of the SABS R³ Center for Doctoral Training.

Creative Commons License
This course material was developed as part of UNIVERSE-HPC, which is funded through the SPF ExCALIBUR programme under grant number EP/W035731/1

This course material was developed as part of UNIVERSE-HPC, which is funded through the SPF ExCALIBUR programme under grant number EP/W035731/1

Creative Commons License

Scipy.sparse and problems

There are seven available sparse matrix types in scipy.sparse:

  • csc_matrix: Compressed Sparse Column format

  • csr_matrix: Compressed Sparse Row format

  • bsr_matrix: Block Sparse Row format

  • lil_matrix: List of Lists format

  • dok_matrix: Dictionary of Keys format

  • coo_matrix: COOrdinate format (aka IJV, triplet format)

  • dia_matrix: DIAgonal format

As indicated by the excellent documentation, the dok_matrix or lil_matrix formats are preferable to construct matrices as they support basic slicing and indexing similar to a standard NumPy array.

You will notice that the FD matrix we have constructed for the Poisson problem is composed entirely of diagonal elements, as is often the case. If you were constructing a similar matrix in MATLAB, you would use the spdiags function, and scipy.sparse has its own equivalent. However, all the scipy.sparse formats also have special methods setdiag which provide a more object-orientated method of doing the same thing.

Scipy has a few different direct solvers for sparse matrics, given below:

spsolve: This solves Ax=bAx=b where AA is converted into CSC or CSR form

spsolve_triangular: Solves Ax=bAx=b, where AA is assumed to be triangular.

factorized: This computes the LULU decomposition of the input matrix AA, returning a Python function that can be called to solve Ax=bAx = b

splu: This computes the LULU decomposition of the input matrix AA using the popular SuperLU library. It returns a Python object of class SuperLU, that has a solve method you can use to solve Ax=bAx = b

Note, scipy.sparse.linalg also has many iterative solvers, which we will investigate further in the next chapter.

Problems

Your goal for this problem is to construct the FD matrix AA given above, using scipy.sparse, and:

Visualise Poisson Matrix

Visualise the matrix AA using the Matplotlib spy plot

Solve Poisson problem

Solve the Poisson problem using:

  • f(x)=2cos(x)/exf(x) = 2 \cos(x) / e^x, with boundary conditions g(0)=0g(0) = 0 and g(2π)=0g(2 \pi)=0. The analytical solution is ua(x)=sin(x)/exu_{a}(x) = -\sin(x) / e^x.

  • f(x)=2sin(x)/exf(x) = 2 \sin(x) / e^x, with boundary conditions g(0)=1g(0) = 1 and g(2π)=1/e2πg(2 \pi)=1 / e^{2 \pi}. The analytical solution is ua(x)=cos(x)/exu_{a}(x) = \cos(x) / e^x

Sparse versus dense: matrix multiplication

Vary the number of discretisation points NN and calculate AAAA using both sparse and dense matrices. For each NN calculate the time to calculate the matix multiplicatiion using Python's time.perf_counter, and plot execution time versus NN for dense and sparse matrix multiplication. Comment on how the time varies with NN.

Sparse versus dense: solving Poisson problem

  1. Vary the number of discretisation points NN and solve the Poisson problem with varying NN, and with using both the sparse and direct LULU solvers. For each NN record the time taken for both the dense and sparse solvers, and record the numerical error vva2||\mathbf{v} - \mathbf{v}_a||_2. Generate plots of both error and time versus NN, and comment on how they vary with NN