This chapter describes functions for solving sparse linear systems of equations. The library provides linear algebra routines which operate directly on the gsl_spmatrix and gsl_vector objects.

The functions described in this chapter are declared in the header file gsl_splinalg.h.

Overview
========

This chapter is primarily concerned with the solution of the linear system

     A x = b

where A is a general square n-by-n non-singular sparse matrix, x is an unknown n-by-1 vector, and b is a given n-by-1 right hand side vector. There exist many methods for solving such sparse linear systems, which broadly fall into either direct or iterative categories. Direct methods include LU and QR decompositions, while iterative methods start with an initial guess for the vector x and update the guess through iteration until convergence. GSL does not currently provide any direct sparse solvers.

Sparse Iterative Solvers
========================

Overview
--------

Many practical iterative methods of solving large n-by-n sparse linear systems involve projecting an approximate solution for X onto a subspace of \bf R^n. If we define a m-dimensional subspace \cal K as the subspace of approximations to the solution X, then m constraints must be imposed to determine the next approximation. These m constraints define another m-dimensional subspace denoted by \cal L. The subspace dimension m is typically chosen to be much smaller than n in order to reduce the computational effort needed to generate the next approximate solution vector. The many iterative algorithms which exist differ mainly in their choice of \cal K and \cal L.

Types of Sparse Iterative Solvers
---------------------------------

The sparse linear algebra library provides the following types of iterative solvers:

 -- Sparse Iterative Type: gsl_splinalg_itersolve_gmres
     This specifies the Generalized Minimum Residual Method (GMRES). This is a projection method using \cal K = \cal K_m and \cal L = A \cal K_m where \cal K_m is the m-th Krylov subspace

          K_m = span( r_0, A r_0, ..., A^(m-1) r_0)

     and r_0 = b - A x_0 is the residual vector of the initial guess x_0. If m is set equal to n, then the Krylov subspace is \bf R^n and GMRES will provide the exact solution X.  However, the goal is for the method to arrive at a very good approximation to X using a much smaller subspace \cal K_m. By default, the GMRES method selects m = MIN(n,10) but the user may specify a different value for m. The GMRES storage requirements grow as O(n(m+1)) and the number of flops grow as O(4 m^2 n - 4 m^3 / 3).

     In the below function gsl_splinalg_itersolve_iterate, one GMRES iteration is defined as projecting the approximate solution vector onto each Krylov subspace \cal K_1, ..., \cal K_m, and so m can be kept smaller by "restarting" the method and calling gsl_splinalg_itersolve_iterate multiple times, providing the updated approximation X to each new call. If the method is not adequately converging, the user may try increasing the parameter m.

     GMRES is considered a robust general purpose iterative solver, however there are cases where the method stagnates if the matrix is not positive-definite and fails to reduce the residual until the very last projection onto the subspace \cal K_n = \bf R^n. In these cases, preconditioning the linear system can help, but GSL does not currently provide any preconditioners.

Iterating the Sparse Linear System
----------------------------------

The following functions are provided to allocate storage for the sparse linear solvers and iterate the system to a solution.

 -- Function: gsl_splinalg_itersolve * gsl_splinalg_itersolve_alloc (const gsl_splinalg_itersolve_type * T, const size_t N, const size_t M)
     This function allocates a workspace for the iterative solution of N-by-N sparse matrix systems. The iterative solver type is specified by T. The argument M specifies the size of the solution candidate subspace \cal K_m. The dimension M may be set to 0 in which case a reasonable default value is used.

 -- Function: void gsl_splinalg_itersolve_free (gsl_splinalg_itersolve * W)
     This function frees the memory associated with the workspace W.

 -- Function: const char * gsl_splinalg_itersolve_name (const gsl_splinalg_itersolve * W)
     This function returns a string pointer to the name of the solver.

 -- Function: int gsl_splinalg_itersolve_iterate (const gsl_spmatrix *A, const gsl_vector *B, const double TOL, gsl_vector *X, gsl_splinalg_itersolve *W)
     This function performs one iteration of the iterative method for the sparse linear system specfied by the matrix A, right hand side vector B and solution vector X. On input, X must be set to an initial guess for the solution. On output, X is updated to give the current solution estimate. The parameter TOL specifies the relative tolerance between the residual norm and norm of B in order to check for convergence. When the following condition is satisfied:

          || A x - b || <= tol * || b ||

     the method has converged, the function returns GSL_SUCCESS and the final solution is provided in X. Otherwise, the function returns GSL_CONTINUE to signal that more iterations are required. Here, || \cdot || represents the Euclidean norm. The input matrix A may be in triplet or compressed column format.

 -- Function: double gsl_splinalg_itersolve_normr (const gsl_splinalg_itersolve *W)
     This function returns the current residual norm ||r|| = ||A x - b||, which is updated after each call to gsl_splinalg_itersolve_iterate.

Examples
========

This example program demonstrates the sparse linear algebra routines on the solution of a simple 1D Poisson equation on [0,1]:

     u''(x) = f(x) = -\pi^2 \sin(\pi x)

with boundary conditions u(0) = u(1) = 0. The analytic solution of this simple problem is u(x) = \sin\pi x. We will solve this problem by finite differencing the left hand side to give

     1/h^2 ( u_(i+1) - 2 u_i + u_(i-1) ) = f_i

Defining a grid of N points, h = 1/(N-1). In the finite difference equation above, u_0 = u_N-1 = 0 are known from the boundary conditions, so we will only put the equations for i = 1, ..., N-2 into the matrix system. The resulting (N-2) \times (N-2) matrix equation is An example program which constructs and solves this system is given below. The system is solved using the iterative GMRES solver. Here is the output of the program:

     iter 0 residual = 4.297275996844e-11
     Converged

showing that the method converged in a single iteration. The calculated solution is shown in the following plot.

          [file poisson.c not included]

References and Further Reading
==============================

The implementation of the GMRES iterative solver closely follows the publications

  H. F. Walker, Implementation of the GMRES method using Householder transformations, SIAM J. Sci. Stat. Comput. 9(1), 1988.

  Y. Saad, Iterative methods for sparse linear systems, 2nd edition, SIAM, 2003.