3.7.8 Solving Linear Systems

Many calculations involve solving systems of linear equations. In many cases, you will find it convenient to write down the equations explicitly, and then solve them using Solve.
In some cases, however, you may prefer to convert the system of linear equations into a matrix equation, and then apply matrix manipulation operations to solve it. This approach is often useful when the system of equations arises as part of a general algorithm, and you do not know in advance how many variables will be involved.
A system of linear equations can be stated in matrix form as , where is the vector of variables.
Note that if your system of equations is sparse, so that most of the entries in the matrix are zero, then you will usually find it much more efficient to use Solve to work directly with your original equations, without ever converting them to matrix form.

Functions for solving linear systems.

If you have a square matrix with a non-zero determinant, then you can always find a unique solution to the matrix equation for any . If, however, the matrix has determinant zero, then there may be either no vector, or an infinite number of vectors which satisfy for a particular . This occurs when the linear equations embodied in are not independent.
When has determinant zero, it is nevertheless always possible to find non-zero vectors that satisfy . The set of vectors satisfying this equation form the null space or kernel of the matrix . Any of these vectors can be expressed as a linear combination of a particular set of basis vectors, which can be obtained using NullSpace[m].

An important feature of LinearSolve and NullSpace is that they work with rectangular, as well as square, matrices.
When you represent a system of linear equations by a matrix equation of the form , the number of columns in gives the number of variables, and the number of rows gives the number of equations. There are a number of cases.

Classes of linear systems represented by rectangular matrices.

You can find out the number of redundant equations corresponding to a particular matrix by evaluating Length[NullSpace[m]]. Subtracting this quantity from the number of columns in m gives the rank of the matrix m.

Efficient repeated solution of linear systems.

If you have a large number of numerical matrix equations to solve of the form , all with the same , then you can often save time by first preprocessing using LUDecomposition[m], and then taking the data you get and repeatedly applying LUBackSubstitution[data, b] for each .