# 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 it is best to represent the matrix as a SparseArray object. As discussed in "Sparse Arrays: Linear Algebra", you can convert from symbolic equations to SparseArray objects using CoefficientArrays. All the functions described here work on SparseArray objects as well as ordinary matrices.

LinearSolve[m,b] | a vector that solves the matrix equation |

NullSpace[m] | a list of linearly independent vectors whose linear combinations span all solutions to the matrix equation |

MatrixRank[m] | the number of linearly independent rows or columns of |

RowReduce[m] | a simplified form of obtained by making linear combinations of rows |

Solving and analyzing linear systems.

If you have a square matrix with a nonzero 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 nonzero 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].

NullSpace and MatrixRank have to determine whether particular combinations of matrix elements are zero. For approximate numerical matrices, the Tolerance option can be used to specify how close to zero is considered good enough. For exact symbolic matrices, you may sometimes need to specify something like ZeroTest->(FullSimplify[#]==0&) to force more to be done to test whether symbolic expressions are zero.

An important feature of functions like 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.

Underdetermined | number of equations less than the number of variables; no solutions or many solutions may exist |

Overdetermined | number of independent equations more than the number of variables; solutions may or may not exist |

Nonsingular | number of independent equations equal to the number of variables, and determinant nonzero; a unique solution exists |

Consistent | at least one solution exists |

Inconsistent | no solutions exist |

Classes of linear systems represented by rectangular matrices.

The number of independent equations is the *rank* of the matrix MatrixRank[m]. The number of redundant equations is Length[NullSpace[m]]. Note that the sum of these quantities is always equal to the number of columns in m.

LinearSolve[m] | generate a function for solving equations of the form |

Generating LinearSolveFunction objects.

In some applications, you will want to solve equations of the form many times with the same , but different . You can do this efficiently in the Wolfram Language by using LinearSolve[m] to create a single LinearSolveFunction that you can apply to as many vectors as you want.

LeastSquares[m,b] | give a vector that solves the least-squares problem |

Solving least-squares problems.