Difference between revisions of "Gauss-Jordan Elimination"
(10 intermediate revisions by 2 users not shown) | |||
Line 1: | Line 1: | ||
− | + | In mathematics, '''Gaussian elimination''', also known as '''row reduction''', is an algorithm for solving systems of linear equations. It consists of a sequence of operations performed on the corresponding matrix of coefficients. This method can also be used to compute the rank of a matrix, the determinant of a square matrix, and the inverse of an invertible matrix. The method is named after Carl Friedrich Gauss (1777–1855) although some special cases of the method—albeit presented without proof—were known to Chinese mathematicians as early as circa 179 CE. | |
− | + | To perform row reduction on a matrix, one uses a sequence of elementary row operations to modify the matrix until the lower left-hand corner of the matrix is filled with zeros, as much as possible. There are three types of elementary row operations: | |
− | + | * Swapping two rows, | |
− | + | * Multiplying a row by a nonzero number, | |
− | + | * Adding a multiple of one row to another row. (subtraction can be achieved by multiplying one row with -1 and adding the result to another row) | |
− | + | Using these operations, a matrix can always be transformed into an upper triangular matrix, and in fact one that is in row echelon form. Once all of the leading coefficients (the leftmost nonzero entry in each row) are 1, and every column containing a leading coefficient has zeros elsewhere, the matrix is said to be in reduced row echelon form. This final form is unique; in other words, it is independent of the sequence of row operations used. For example, in the following sequence of row operations (where two elementary operations on different rows are done at the first and third steps), the third and fourth matrices are the ones in row echelon form, and the final matrix is the unique reduced row echelon form. | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | 1 | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
+ | :<math>\begin{bmatrix} | ||
+ | 1 & 3 & 1 & 9 \\ | ||
+ | 1 & 1 & -1 & 1 \\ | ||
+ | 3 & 11 & 5 & 35 | ||
+ | \end{bmatrix}\to | ||
+ | \begin{bmatrix} | ||
+ | 1 & 3 & 1 & 9 \\ | ||
+ | 0 & -2 & -2 & -8 \\ | ||
+ | 0 & 2 & 2 & 8 | ||
+ | \end{bmatrix}\to | ||
+ | \begin{bmatrix} | ||
+ | 1 & 3 & 1 & 9 \\ | ||
+ | 0 & -2 & -2 & -8 \\ | ||
+ | 0 & 0 & 0 & 0 | ||
+ | \end{bmatrix}\to | ||
+ | \begin{bmatrix} | ||
+ | 1 & 0 & -2 & -3 \\ | ||
+ | 0 & 1 & 1 & 4 \\ | ||
+ | 0 & 0 & 0 & 0 | ||
+ | \end{bmatrix} </math> | ||
− | + | Using row operations to convert a matrix into reduced row echelon form is sometimes called '''Gauss–Jordan elimination'''. In this case, the term ''Gaussian elimination'' refers to the process until it has reached its upper triangular, or (unreduced) row echelon form. For computational reasons, when solving systems of linear equations, it is sometimes preferable to stop row operations before the matrix is completely reduced. | |
− | + | == Definitions and example of algorithm == | |
− | + | The process of row reduction makes use of elementary row operations, and can be divided into two parts. The first part (sometimes called forward elimination) reduces a given system to ''row echelon form'', from which one can tell whether there are no solutions, a unique solution, or infinitely many solutions. The second part (sometimes called back substitution) continues to use row operations until the solution is found; in other words, it puts the matrix into ''reduced'' row echelon form. | |
− | |||
− | |||
− | + | Another point of view, which turns out to be very useful to analyze the algorithm, is that row reduction produces a matrix decomposition of the original matrix. The elementary row operations may be viewed as the multiplication on the left of the original matrix by elementary matrices. Alternatively, a sequence of elementary operations that reduces a single row may be viewed as multiplication by a Frobenius matrix. Then the first part of the algorithm computes an LU decomposition, while the second part writes the original matrix as the product of a uniquely determined invertible matrix and a uniquely determined reduced row echelon matrix. | |
− | |||
− | |||
− | |||
− | + | ===Row operations=== | |
− | + | There are three types of '''elementary row operations''' which may be performed on the rows of a matrix: | |
+ | # Swap the positions of two rows. | ||
+ | # Multiply a row by a non-zero scalar. | ||
+ | # Add to one row a scalar multiple of another. | ||
− | + | If the matrix is associated to a system of linear equations, then these operations do not change the solution set. Therefore, if one's goal is to solve a system of linear equations, then using these row operations could make the problem easier. | |
− | + | ||
− | + | ===Echelon form=== | |
− | + | For each row in a matrix, if the row does not consist of only zeros, then the leftmost nonzero entry is called the ''leading coefficient'' (or ''pivot'') of that row. So if two leading coefficients are in the same column, then a row operation of type 3 could be used to make one of those coefficients zero. Then by using the row swapping operation, one can always order the rows so that for every non-zero row, the leading coefficient is to the right of the leading coefficient of the row above. If this is the case, then matrix is said to be in '''row echelon form'''. So the lower left part of the matrix contains only zeros, and all of the zero rows are below the non-zero rows. The word "echelon" is used here because one can roughly think of the rows being ranked by their size, with the largest being at the top and the smallest being at the bottom. | |
− | + | ||
− | + | For example, the following matrix is in row echelon form, and its leading coefficients are shown in red: | |
− | + | ||
+ | : <math>\begin{bmatrix} | ||
+ | 0 & \color{red}{\mathbf{2}} & 1 & -1 \\ | ||
+ | 0 & 0 & \color{red}{\mathbf{3}} & 1 \\ | ||
+ | 0 & 0 & 0 & 0 | ||
+ | \end{bmatrix}.</math> | ||
+ | |||
+ | It is in echelon form because the zero row is at the bottom, and the leading coefficient of the second row (in the third column), is to the right of the leading coefficient of the first row (in the second column). | ||
+ | |||
+ | A matrix is said to be in '''reduced row echelon form''' if furthermore all of the leading coefficients are equal to 1 (which can be achieved by using the elementary row operation of type 2), and in every column containing a leading coefficient, all of the other entries in that column are zero (which can be achieved by using elementary row operations of type 3). | ||
+ | |||
+ | ===Example of the algorithm=== | ||
+ | Suppose the goal is to find and describe the set of solutions to the following system of linear equations: | ||
+ | : <math> | ||
+ | \begin{alignat}{4} | ||
+ | 2x &{}+{}& y &{}-{}& z &{}={}& 8 & \qquad (L_1) \\ | ||
+ | -3x &{}-{}& y &{}+{}& 2z &{}={}& -11 & \qquad (L_2) \\ | ||
+ | -2x &{}+{}& y &{}+{}& 2z &{}={}& -3 & \qquad (L_3) | ||
+ | \end{alignat} | ||
+ | </math> | ||
+ | |||
+ | The table below is the row reduction process applied simultaneously to the system of equations and its associated augmented matrix. In practice, one does not usually deal with the systems in terms of equations, but instead makes use of the augmented matrix, which is more suitable for computer manipulations. The row reduction procedure may be summarized as follows: eliminate {{mvar|x}} from all equations below {{math|''L''<sub>1</sub>}}, and then eliminate {{mvar|y}} from all equations below {{math|''L''<sub>2</sub>}}. This will put the system into triangular form. Then, using back-substitution, each unknown can be solved for. | ||
+ | |||
+ | : {| style="background-color:white;" class="wikitable" | ||
+ | |- | ||
+ | ! System of equations !! Row operations !! Augmented matrix | ||
+ | |- align="center" | ||
+ | | <math> | ||
+ | \begin{alignat}{4} | ||
+ | 2x &{}+{}& y &{}-{}& z &{}={}& 8 & \\ | ||
+ | -3x &{}-{}& y &{}+{}& 2z &{}={}& -11 & \\ | ||
+ | -2x &{}+{}& y &{}+{}& 2z &{}={}& -3 & | ||
+ | \end{alignat} | ||
+ | </math> | ||
+ | | | ||
+ | | <math> | ||
+ | \left[\begin{array}{rrr|r} | ||
+ | 2 & 1 & -1 & 8 \\ | ||
+ | -3 & -1 & 2 & -11 \\ | ||
+ | -2 & 1 & 2 & -3 | ||
+ | \end{array}\right] | ||
+ | </math> | ||
+ | |- align="center" | ||
+ | | <math> | ||
+ | \begin{alignat}{4} | ||
+ | 2x &{}+{}& y &{}-{}& z &{}={}& 8 & \\ | ||
+ | & & \tfrac12 y &{}+{}& \tfrac12 z &{}={}& 1 & \\ | ||
+ | & & 2y &{}+{}& z &{}={}& 5 & | ||
+ | \end{alignat} | ||
+ | </math> | ||
+ | | <math> | ||
+ | \begin{align} | ||
+ | L_2 + \tfrac32 L_1 &\to L_2 \\ | ||
+ | L_3 + L_1 &\to L_3 | ||
+ | \end{align} | ||
+ | </math> | ||
+ | | <math> | ||
+ | \left[\begin{array}{rrr|r} | ||
+ | 2 & 1 & -1 & 8 \\ | ||
+ | 0 & \frac12 & \frac12 & 1 \\ | ||
+ | 0 & 2 & 1 & 5 | ||
+ | \end{array}\right] | ||
+ | </math> | ||
+ | |- align="center" | ||
+ | | <math> | ||
+ | \begin{alignat}{4} | ||
+ | 2x &{}+{}& y &{}-{}& z &{}={}& 8 & \\ | ||
+ | & & \tfrac12 y &{}+{}& \tfrac12 z &{}={}& 1 & \\ | ||
+ | & & & & -z &{}={}& 1 & | ||
+ | \end{alignat} | ||
+ | </math> | ||
+ | | <math> | ||
+ | L_3 + -4 L_2 \to L_3</math> | ||
+ | | <math> | ||
+ | \left[\begin{array}{rrr|r} | ||
+ | 2 & 1 & -1 & 8 \\ | ||
+ | 0 & \frac12 & \frac12 & 1 \\ | ||
+ | 0 & 0 & -1 & 1 | ||
+ | \end{array}\right] | ||
+ | </math> | ||
+ | |- | ||
+ | |colspan=3 align="center"| The matrix is now in echelon form (also called triangular form) | ||
+ | |- align="center" | ||
+ | | <math> | ||
+ | \begin{alignat}{4} | ||
+ | 2x &{}+{}& y & & &{}={} 7 & \\ | ||
+ | & & \tfrac12 y & & &{}={} \tfrac32 & \\ | ||
+ | & & &{}-{}& z &{}={} 1 & | ||
+ | \end{alignat} | ||
+ | </math> | ||
+ | | <math> | ||
+ | \begin{align} | ||
+ | L_2 + \tfrac12 L_3 &\to L_2 \\ | ||
+ | L_1 - L_3 &\to L_1 | ||
+ | \end{align} | ||
+ | </math> | ||
+ | | <math> | ||
+ | \left[\begin{array}{rrr|r} | ||
+ | 2 & 1 & 0 & 7 \\ | ||
+ | 0 & \frac12 & 0 & \frac32 \\ | ||
+ | 0 & 0 & -1 & 1 | ||
+ | \end{array}\right] | ||
+ | </math> | ||
+ | |- align="center" | ||
+ | | <math> | ||
+ | \begin{alignat}{4} | ||
+ | 2x &{}+{}& y &\quad& &{}={}& 7 & \\ | ||
+ | & & y &\quad& &{}={}& 3 & \\ | ||
+ | & & &\quad& z &{}={}& -1 & | ||
+ | \end{alignat} | ||
+ | </math> | ||
+ | | <math> | ||
+ | \begin{align} | ||
+ | 2 L_2 &\to L_2 \\ | ||
+ | -L_3 &\to L_3 | ||
+ | \end{align} | ||
+ | </math> | ||
+ | | <math> | ||
+ | \left[\begin{array}{rrr|r} | ||
+ | 2 & 1 & 0 & 7 \\ | ||
+ | 0 & 1 & 0 & 3 \\ | ||
+ | 0 & 0 & 1 & -1 | ||
+ | \end{array}\right] | ||
+ | </math> | ||
+ | |- align="center" | ||
+ | | <math> | ||
+ | \begin{alignat}{4} | ||
+ | x &\quad& &\quad& &{}={}& 2 & \\ | ||
+ | &\quad& y &\quad& &{}={}& 3 & \\ | ||
+ | &\quad& &\quad& z &{}={}& -1 & | ||
+ | \end{alignat} | ||
+ | </math> | ||
+ | | <math> | ||
+ | \begin{align} | ||
+ | L_1 - L_2 &\to L_1 \\ | ||
+ | \tfrac12 L_1 &\to L_1 | ||
+ | \end{align} | ||
+ | </math> | ||
+ | | <math> | ||
+ | \left[\begin{array}{rrr|r} | ||
+ | 1 & 0 & 0 & 2 \\ | ||
+ | 0 & 1 & 0 & 3 \\ | ||
+ | 0 & 0 & 1 & -1 | ||
+ | \end{array}\right] | ||
+ | </math> | ||
+ | |} | ||
+ | |||
+ | The second column describes which row operations have just been performed. So for the first step, the {{mvar|x}} is eliminated from {{math|''L''<sub>2</sub>}} by adding <math> \tfrac{3}{2}</math>''L''<sub>1</sub> to {{math|''L''<sub>2</sub>}}. Next, {{mvar|x}} is eliminated from {{math|''L''<sub>3</sub>}} by adding {{math|''L''<sub>1</sub>}} to {{math|''L''<sub>3</sub>}}. These row operations are labelled in the table as | ||
+ | |||
+ | : <math>\begin{align} | ||
+ | L_2 + \tfrac32 L_1 &\to L_2, \\ | ||
+ | L_3 + L_1 &\to L_3. | ||
+ | \end{align}</math> | ||
+ | |||
+ | Once {{mvar|y}} is also eliminated from the third row, the result is a system of linear equations in triangular form, and so the first part of the algorithm is complete. From a computational point of view, it is faster to solve the variables in reverse order, a process known as back-substitution. One sees the solution is ''z'' = −1, ''y'' = 3, and ''x'' = 2. So there is a unique solution to the original system of equations. | ||
+ | |||
+ | Instead of stopping once the matrix is in echelon form, one could continue until the matrix is in ''reduced'' row echelon form, as it is done in the table. The process of row reducing until the matrix is reduced is sometimes referred to as '''Gauss–Jordan elimination''', to distinguish it from stopping after reaching echelon form. | ||
+ | |||
+ | == Applications == | ||
+ | Historically, the first application of the row reduction method is for solving systems of linear equations. Here are some other important applications of the algorithm. | ||
+ | |||
+ | === Computing determinants === | ||
+ | To explain how Gaussian elimination allows the computation of the determinant of a square matrix, we have to recall how the elementary row operations change the determinant: | ||
+ | * Swapping two rows multiplies the determinant by −1 | ||
+ | * Multiplying a row by a nonzero scalar multiplies the determinant by the same scalar | ||
+ | * Adding to one row a scalar multiple of another does not change the determinant. | ||
+ | |||
+ | If Gaussian elimination applied to a square matrix {{mvar|A}} produces a row echelon matrix {{mvar|B}}, let {{mvar|d}} be the product of the scalars by which the determinant has been multiplied, using the above rules. Then the determinant of {{mvar|A}} is the quotient by {{mvar|d}} of the product of the elements of the diagonal of {{mvar|B}}: | ||
+ | :<math>\det(A) = \frac{\prod\operatorname{diag}(B)}{d}.</math> | ||
+ | |||
+ | Computationally, for an {{math|''n'' × ''n''}} matrix, this method needs only {{math|O(''n''<sup>3</sup>)}} arithmetic operations, while using Leibniz formula for determinants requires {{math|O(''n''!)}} operations (number of summands in the formula), and | ||
+ | recursive Laplace expansion requires {{math|O(2<sup>''n''</sup>)}} operations (number of sub-determinants to compute, if none is computed twice). Even on the fastest computers, these two methods are impractical or almost impracticable for {{math|''n''}} above 20.<math></math> | ||
+ | |||
+ | === Finding the inverse of a matrix === | ||
+ | A variant of Gaussian elimination called Gauss–Jordan elimination can be used for finding the inverse of a matrix, if it exists. If {{math|''A''}} is an {{math|''n'' × ''n''}} square matrix, then one can use row reduction to compute its inverse matrix, if it exists. First, the {{math|''n'' × ''n''}} identity matrix is augmented to the right of {{math|''A''}}, forming an {{math|''n'' × 2''n''}} block matrix {{math|[''A'' {{!}} ''I'']}}. Now through application of elementary row operations, find the reduced echelon form of this {{math|''n'' × 2''n''}} matrix. The matrix {{math|''A''}} is invertible if and only if the left block can be reduced to the identity matrix {{math|''I''}}; in this case the right block of the final matrix is {{math|''A''<sup>−1</sup>}}. If the algorithm is unable to reduce the left block to {{math|''I''}}, then {{math|''A''}} is not invertible. | ||
+ | |||
+ | For example, consider the following matrix: | ||
+ | : <math>A = | ||
+ | \begin{bmatrix} | ||
+ | 2 & -1 & 0 \\ | ||
+ | -1 & 2 & -1 \\ | ||
+ | 0 & -1 & 2 | ||
+ | \end{bmatrix}. | ||
+ | </math> | ||
+ | |||
+ | To find the inverse of this matrix, one takes the following matrix augmented by the identity and row-reduces it as a 3 × 6 matrix: | ||
+ | : <math>[ A | I ] = | ||
+ | \left[\begin{array}{ccc|ccc} | ||
+ | 2 & -1 & 0 & 1 & 0 & 0 \\ | ||
+ | -1 & 2 & -1 & 0 & 1 & 0 \\ | ||
+ | 0 & -1 & 2 & 0 & 0 & 1 | ||
+ | \end{array}\right]. | ||
+ | </math> | ||
+ | |||
+ | By performing row operations, one can check that the reduced row echelon form of this augmented matrix is | ||
+ | : <math>[ I | B ] = | ||
+ | \left[\begin{array}{rrr|rrr} | ||
+ | 1 & 0 & 0 & \frac34 & \frac12 & \frac14 \\ | ||
+ | 0 & 1 & 0 & \frac12 & 1 & \frac12 \\ | ||
+ | 0 & 0 & 1 & \frac14 & \frac12 & \frac34 | ||
+ | \end{array}\right]. | ||
+ | </math> | ||
+ | |||
+ | One can think of each row operation as the left product by an elementary matrix. Denoting by {{math|''B''}} the product of these elementary matrices, we showed, on the left, that ''BA'' = ''I'', and therefore, ''B'' = ''A''<sup>−1</sup>. On the right, we kept a record of ''BI'' = ''B'', which we know is the inverse desired. This procedure for finding the inverse works for square matrices of any size. | ||
+ | |||
+ | === Computing ranks and bases === | ||
+ | The Gaussian elimination algorithm can be applied to any {{math|''m'' × ''n''}} matrix {{mvar|A}}. In this way, for example, some 6 × 9 matrices can be transformed to a matrix that has a row echelon form like | ||
+ | :<math> T= | ||
+ | \begin{bmatrix} | ||
+ | a & * & * & *& * & * & * & * & * \\ | ||
+ | 0 & 0 & b & * & * & * & * & * & * \\ | ||
+ | 0 & 0 & 0 & c & * & * & * & * & * \\ | ||
+ | 0 & 0 & 0 & 0 & 0 & 0 & d & * & * \\ | ||
+ | 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & e \\ | ||
+ | 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 & 0 | ||
+ | \end{bmatrix}, | ||
+ | </math> | ||
+ | where the stars are arbitrary entries, and {{math|''a'', ''b'', ''c'', ''d'', ''e''}} are nonzero entries. This echelon matrix {{mvar|T}} contains a wealth of information about {{mvar|A}}: the rank of {{mvar|A}} is 5, since there are 5 nonzero rows in {{mvar|T}}; the vector space spanned by the columns of {{mvar|A}} has a basis consisting of its columns 1, 3, 4, 7 and 9 (the columns with {{math|''a'', ''b'', ''c'', ''d'', ''e''}} in {{mvar|T}}), and the stars show how the other columns of {{mvar|A}} can be written as linear combinations of the basis columns. This is a consequence of the distributivity of the dot product in the expression of a linear map as a matrix. | ||
+ | |||
+ | All of this applies also to the reduced row echelon form, which is a particular row echelon format. | ||
+ | |||
+ | == Computational efficiency == | ||
+ | The number of arithmetic operations required to perform row reduction is one way of measuring the algorithm's computational efficiency. For example, to solve a system of {{math|''n''}} equations for {{math|''n''}} unknowns by performing row operations on the matrix until it is in echelon form, and then solving for each unknown in reverse order, requires {{math|''n''(''n'' + 1)/2}} divisions, {{math|(2''n''<sup>3</sup> + 3''n''<sup>2</sup> − 5''n'')/6}} multiplications, and {{math|(2''n''<sup>3</sup> + 3''n''<sup>2</sup> − 5''n'')/6}} subtractions, for a total of approximately {{math|2''n''<sup>3</sup>/3}} operations. Thus it has arithmetic complexity of {{math|O(''n''<sup>3</sup>)}}; see Big O notation. | ||
+ | |||
+ | This arithmetic complexity is a good measure of the time needed for the whole computation when the time for each arithmetic operation is approximately constant. This is the case when the coefficients are represented by floating-point numbers or when they belong to a finite field. If the coefficients are integers or rational numbers exactly represented, the intermediate entries can grow exponentially large, so the bit complexity is exponential. | ||
+ | However, there is a variant of Gaussian elimination, called the Bareiss algorithm, that avoids this exponential growth of the intermediate entries and, with the same arithmetic complexity of {{math|O(''n''<sup>3</sup>)}}, has a bit complexity of {{math|O(''n''<sup>5</sup>)}}. | ||
+ | |||
+ | This algorithm can be used on a computer for systems with thousands of equations and unknowns. However, the cost becomes prohibitive for systems with millions of equations. These large systems are generally solved using iterative methods. Specific methods exist for systems whose coefficients follow a regular pattern (see system of linear equations). | ||
+ | |||
+ | To put an {{math|''n'' × ''n''}} matrix into reduced echelon form by row operations, one needs {{math|''n''<sup>3</sup>}} arithmetic operations, which is approximately 50% more computation steps. | ||
+ | |||
+ | One possible problem is numerical instability, caused by the possibility of dividing by very small numbers. If, for example, the leading coefficient of one of the rows is very close to zero, then to row-reduce the matrix, one would need to divide by that number. This means that any error existed for the number that was close to zero would be amplified. Gaussian elimination is numerically stable for diagonally dominant or positive-definite matrices. For general matrices, Gaussian elimination is usually considered to be stable, when using partial pivoting, even though there are examples of stable matrices for which it is unstable. | ||
+ | |||
+ | === Generalizations === | ||
+ | |||
+ | Gaussian elimination can be performed over any field, not just the real numbers. | ||
+ | |||
+ | Buchberger's algorithm is a generalization of Gaussian elimination to systems of polynomial equations. This generalization depends heavily on the notion of a monomial order. The choice of an ordering on the variables is already implicit in Gaussian elimination, manifesting as the choice to work from left to right when selecting pivot positions. | ||
+ | |||
+ | Computing the rank of a tensor of order greater than 2 is NP-hard. Therefore, if {{math|P ≠ NP}}, there cannot be a polynomial time analog of Gaussian elimination for higher-order tensors (matrices are array representations of order-2 tensors). | ||
+ | |||
+ | == Pseudocode == | ||
+ | As explained above, Gaussian elimination transforms a given {{math|''m'' × ''n''}} matrix {{mvar|A}} into a matrix in row-echelon form. | ||
+ | |||
+ | In the following pseudocode, <code>A[i, j]</code> denotes the entry of the matrix {{mvar|A}} in row {{mvar|i}} and column {{mvar|j}} with the indices starting from 1. The transformation is performed ''in place'', meaning that the original matrix is lost for being eventually replaced by its row-echelon form. | ||
+ | |||
+ | h := 1 /* ''Initialization of the pivot row'' */ | ||
+ | k := 1 /* ''Initialization of the pivot column'' */ | ||
+ | |||
+ | '''while''' h ≤ m '''and''' k ≤ n | ||
+ | /* ''Find the k-th pivot:'' */ | ||
+ | i_max := argmax (i = h ... m, abs(A[i, k])) | ||
+ | '''if''' A[i_max, k] = 0 | ||
+ | /* ''No pivot in this column, pass to next column'' */ | ||
+ | k := k + 1 | ||
+ | '''else''' | ||
+ | '''swap rows'''(h, i_max) | ||
+ | /* ''Do for all rows below pivot:'' */ | ||
+ | '''for''' i = h + 1 ... m: | ||
+ | f := A[i, k] / A[h, k] | ||
+ | /* ''Fill with zeros the lower part of pivot column:'' */ | ||
+ | A[i, k] := 0 | ||
+ | /* ''Do for all remaining elements in current row:'' */ | ||
+ | '''for''' j = k + 1 ... n: | ||
+ | A[i, j] := A[i, j] - A[h, j] * f | ||
+ | /* ''Increase pivot row and column'' */ | ||
+ | h := h + 1 | ||
+ | k := k + 1 | ||
+ | |||
+ | This algorithm differs slightly from the one discussed earlier, by choosing a pivot with largest absolute value. Such a ''partial pivoting'' may be required if, at the pivot place, the entry of the matrix is zero. In any case, choosing the largest possible absolute value of the pivot improves the numerical stability of the algorithm, when floating point is used for representing numbers. | ||
+ | |||
+ | Upon completion of this procedure the matrix will be in row echelon form and the corresponding system may be solved by back substitution. | ||
==Resources== | ==Resources== | ||
* [https://openstax.org/books/college-algebra/pages/7-6-solving-systems-with-gaussian-elimination Solving Systems with Gaussian Elimination], OpenStax | * [https://openstax.org/books/college-algebra/pages/7-6-solving-systems-with-gaussian-elimination Solving Systems with Gaussian Elimination], OpenStax | ||
+ | * [https://tutorial.math.lamar.edu/classes/alg/augmentedmatrix.aspx Augmented Matrices and Gauss-Jordan Elimination], Paul's Online Notes | ||
+ | |||
+ | == Licensing == | ||
+ | Content obtained and/or adapted from: | ||
+ | * [https://en.wikipedia.org/wiki/Gaussian_elimination Gaussian elimination, Wikipedia] under a CC BY-SA license |
Latest revision as of 14:35, 29 January 2022
In mathematics, Gaussian elimination, also known as row reduction, is an algorithm for solving systems of linear equations. It consists of a sequence of operations performed on the corresponding matrix of coefficients. This method can also be used to compute the rank of a matrix, the determinant of a square matrix, and the inverse of an invertible matrix. The method is named after Carl Friedrich Gauss (1777–1855) although some special cases of the method—albeit presented without proof—were known to Chinese mathematicians as early as circa 179 CE.
To perform row reduction on a matrix, one uses a sequence of elementary row operations to modify the matrix until the lower left-hand corner of the matrix is filled with zeros, as much as possible. There are three types of elementary row operations:
- Swapping two rows,
- Multiplying a row by a nonzero number,
- Adding a multiple of one row to another row. (subtraction can be achieved by multiplying one row with -1 and adding the result to another row)
Using these operations, a matrix can always be transformed into an upper triangular matrix, and in fact one that is in row echelon form. Once all of the leading coefficients (the leftmost nonzero entry in each row) are 1, and every column containing a leading coefficient has zeros elsewhere, the matrix is said to be in reduced row echelon form. This final form is unique; in other words, it is independent of the sequence of row operations used. For example, in the following sequence of row operations (where two elementary operations on different rows are done at the first and third steps), the third and fourth matrices are the ones in row echelon form, and the final matrix is the unique reduced row echelon form.
Using row operations to convert a matrix into reduced row echelon form is sometimes called Gauss–Jordan elimination. In this case, the term Gaussian elimination refers to the process until it has reached its upper triangular, or (unreduced) row echelon form. For computational reasons, when solving systems of linear equations, it is sometimes preferable to stop row operations before the matrix is completely reduced.
Contents
Definitions and example of algorithm
The process of row reduction makes use of elementary row operations, and can be divided into two parts. The first part (sometimes called forward elimination) reduces a given system to row echelon form, from which one can tell whether there are no solutions, a unique solution, or infinitely many solutions. The second part (sometimes called back substitution) continues to use row operations until the solution is found; in other words, it puts the matrix into reduced row echelon form.
Another point of view, which turns out to be very useful to analyze the algorithm, is that row reduction produces a matrix decomposition of the original matrix. The elementary row operations may be viewed as the multiplication on the left of the original matrix by elementary matrices. Alternatively, a sequence of elementary operations that reduces a single row may be viewed as multiplication by a Frobenius matrix. Then the first part of the algorithm computes an LU decomposition, while the second part writes the original matrix as the product of a uniquely determined invertible matrix and a uniquely determined reduced row echelon matrix.
Row operations
There are three types of elementary row operations which may be performed on the rows of a matrix:
- Swap the positions of two rows.
- Multiply a row by a non-zero scalar.
- Add to one row a scalar multiple of another.
If the matrix is associated to a system of linear equations, then these operations do not change the solution set. Therefore, if one's goal is to solve a system of linear equations, then using these row operations could make the problem easier.
Echelon form
For each row in a matrix, if the row does not consist of only zeros, then the leftmost nonzero entry is called the leading coefficient (or pivot) of that row. So if two leading coefficients are in the same column, then a row operation of type 3 could be used to make one of those coefficients zero. Then by using the row swapping operation, one can always order the rows so that for every non-zero row, the leading coefficient is to the right of the leading coefficient of the row above. If this is the case, then matrix is said to be in row echelon form. So the lower left part of the matrix contains only zeros, and all of the zero rows are below the non-zero rows. The word "echelon" is used here because one can roughly think of the rows being ranked by their size, with the largest being at the top and the smallest being at the bottom.
For example, the following matrix is in row echelon form, and its leading coefficients are shown in red:
It is in echelon form because the zero row is at the bottom, and the leading coefficient of the second row (in the third column), is to the right of the leading coefficient of the first row (in the second column).
A matrix is said to be in reduced row echelon form if furthermore all of the leading coefficients are equal to 1 (which can be achieved by using the elementary row operation of type 2), and in every column containing a leading coefficient, all of the other entries in that column are zero (which can be achieved by using elementary row operations of type 3).
Example of the algorithm
Suppose the goal is to find and describe the set of solutions to the following system of linear equations:
The table below is the row reduction process applied simultaneously to the system of equations and its associated augmented matrix. In practice, one does not usually deal with the systems in terms of equations, but instead makes use of the augmented matrix, which is more suitable for computer manipulations. The row reduction procedure may be summarized as follows: eliminate x from all equations below L1, and then eliminate y from all equations below L2. This will put the system into triangular form. Then, using back-substitution, each unknown can be solved for.
System of equations Row operations Augmented matrix The matrix is now in echelon form (also called triangular form)
The second column describes which row operations have just been performed. So for the first step, the x is eliminated from L2 by adding L1 to L2. Next, x is eliminated from L3 by adding L1 to L3. These row operations are labelled in the table as
Once y is also eliminated from the third row, the result is a system of linear equations in triangular form, and so the first part of the algorithm is complete. From a computational point of view, it is faster to solve the variables in reverse order, a process known as back-substitution. One sees the solution is z = −1, y = 3, and x = 2. So there is a unique solution to the original system of equations.
Instead of stopping once the matrix is in echelon form, one could continue until the matrix is in reduced row echelon form, as it is done in the table. The process of row reducing until the matrix is reduced is sometimes referred to as Gauss–Jordan elimination, to distinguish it from stopping after reaching echelon form.
Applications
Historically, the first application of the row reduction method is for solving systems of linear equations. Here are some other important applications of the algorithm.
Computing determinants
To explain how Gaussian elimination allows the computation of the determinant of a square matrix, we have to recall how the elementary row operations change the determinant:
- Swapping two rows multiplies the determinant by −1
- Multiplying a row by a nonzero scalar multiplies the determinant by the same scalar
- Adding to one row a scalar multiple of another does not change the determinant.
If Gaussian elimination applied to a square matrix A produces a row echelon matrix B, let d be the product of the scalars by which the determinant has been multiplied, using the above rules. Then the determinant of A is the quotient by d of the product of the elements of the diagonal of B:
Computationally, for an n × n matrix, this method needs only O(n3) arithmetic operations, while using Leibniz formula for determinants requires O(n!) operations (number of summands in the formula), and recursive Laplace expansion requires O(2n) operations (number of sub-determinants to compute, if none is computed twice). Even on the fastest computers, these two methods are impractical or almost impracticable for n above 20.
Finding the inverse of a matrix
A variant of Gaussian elimination called Gauss–Jordan elimination can be used for finding the inverse of a matrix, if it exists. If A is an n × n square matrix, then one can use row reduction to compute its inverse matrix, if it exists. First, the n × n identity matrix is augmented to the right of A, forming an n × 2n block matrix [A | I]. Now through application of elementary row operations, find the reduced echelon form of this n × 2n matrix. The matrix A is invertible if and only if the left block can be reduced to the identity matrix I; in this case the right block of the final matrix is A−1. If the algorithm is unable to reduce the left block to I, then A is not invertible.
For example, consider the following matrix:
To find the inverse of this matrix, one takes the following matrix augmented by the identity and row-reduces it as a 3 × 6 matrix:
By performing row operations, one can check that the reduced row echelon form of this augmented matrix is
One can think of each row operation as the left product by an elementary matrix. Denoting by B the product of these elementary matrices, we showed, on the left, that BA = I, and therefore, B = A−1. On the right, we kept a record of BI = B, which we know is the inverse desired. This procedure for finding the inverse works for square matrices of any size.
Computing ranks and bases
The Gaussian elimination algorithm can be applied to any m × n matrix A. In this way, for example, some 6 × 9 matrices can be transformed to a matrix that has a row echelon form like
where the stars are arbitrary entries, and a, b, c, d, e are nonzero entries. This echelon matrix T contains a wealth of information about A: the rank of A is 5, since there are 5 nonzero rows in T; the vector space spanned by the columns of A has a basis consisting of its columns 1, 3, 4, 7 and 9 (the columns with a, b, c, d, e in T), and the stars show how the other columns of A can be written as linear combinations of the basis columns. This is a consequence of the distributivity of the dot product in the expression of a linear map as a matrix.
All of this applies also to the reduced row echelon form, which is a particular row echelon format.
Computational efficiency
The number of arithmetic operations required to perform row reduction is one way of measuring the algorithm's computational efficiency. For example, to solve a system of n equations for n unknowns by performing row operations on the matrix until it is in echelon form, and then solving for each unknown in reverse order, requires n(n + 1)/2 divisions, (2n3 + 3n2 − 5n)/6 multiplications, and (2n3 + 3n2 − 5n)/6 subtractions, for a total of approximately 2n3/3 operations. Thus it has arithmetic complexity of O(n3); see Big O notation.
This arithmetic complexity is a good measure of the time needed for the whole computation when the time for each arithmetic operation is approximately constant. This is the case when the coefficients are represented by floating-point numbers or when they belong to a finite field. If the coefficients are integers or rational numbers exactly represented, the intermediate entries can grow exponentially large, so the bit complexity is exponential. However, there is a variant of Gaussian elimination, called the Bareiss algorithm, that avoids this exponential growth of the intermediate entries and, with the same arithmetic complexity of O(n3), has a bit complexity of O(n5).
This algorithm can be used on a computer for systems with thousands of equations and unknowns. However, the cost becomes prohibitive for systems with millions of equations. These large systems are generally solved using iterative methods. Specific methods exist for systems whose coefficients follow a regular pattern (see system of linear equations).
To put an n × n matrix into reduced echelon form by row operations, one needs n3 arithmetic operations, which is approximately 50% more computation steps.
One possible problem is numerical instability, caused by the possibility of dividing by very small numbers. If, for example, the leading coefficient of one of the rows is very close to zero, then to row-reduce the matrix, one would need to divide by that number. This means that any error existed for the number that was close to zero would be amplified. Gaussian elimination is numerically stable for diagonally dominant or positive-definite matrices. For general matrices, Gaussian elimination is usually considered to be stable, when using partial pivoting, even though there are examples of stable matrices for which it is unstable.
Generalizations
Gaussian elimination can be performed over any field, not just the real numbers.
Buchberger's algorithm is a generalization of Gaussian elimination to systems of polynomial equations. This generalization depends heavily on the notion of a monomial order. The choice of an ordering on the variables is already implicit in Gaussian elimination, manifesting as the choice to work from left to right when selecting pivot positions.
Computing the rank of a tensor of order greater than 2 is NP-hard. Therefore, if P ≠ NP, there cannot be a polynomial time analog of Gaussian elimination for higher-order tensors (matrices are array representations of order-2 tensors).
Pseudocode
As explained above, Gaussian elimination transforms a given m × n matrix A into a matrix in row-echelon form.
In the following pseudocode, A[i, j]
denotes the entry of the matrix A in row i and column j with the indices starting from 1. The transformation is performed in place, meaning that the original matrix is lost for being eventually replaced by its row-echelon form.
h := 1 /* Initialization of the pivot row */ k := 1 /* Initialization of the pivot column */ while h ≤ m and k ≤ n /* Find the k-th pivot: */ i_max := argmax (i = h ... m, abs(A[i, k])) if A[i_max, k] = 0 /* No pivot in this column, pass to next column */ k := k + 1 else swap rows(h, i_max) /* Do for all rows below pivot: */ for i = h + 1 ... m: f := A[i, k] / A[h, k] /* Fill with zeros the lower part of pivot column: */ A[i, k] := 0 /* Do for all remaining elements in current row: */ for j = k + 1 ... n: A[i, j] := A[i, j] - A[h, j] * f /* Increase pivot row and column */ h := h + 1 k := k + 1
This algorithm differs slightly from the one discussed earlier, by choosing a pivot with largest absolute value. Such a partial pivoting may be required if, at the pivot place, the entry of the matrix is zero. In any case, choosing the largest possible absolute value of the pivot improves the numerical stability of the algorithm, when floating point is used for representing numbers.
Upon completion of this procedure the matrix will be in row echelon form and the corresponding system may be solved by back substitution.
Resources
- Solving Systems with Gaussian Elimination, OpenStax
- Augmented Matrices and Gauss-Jordan Elimination, Paul's Online Notes
Licensing
Content obtained and/or adapted from:
- Gaussian elimination, Wikipedia under a CC BY-SA license