# Row echelon form question

I am wondering if there is a Mathematica command that will put a matrix in row echelon form. That is, put

$$\begin{bmatrix} 1 & 2 & 3\\ 2 & 3 & 4\\ -1 & 0 & 2 \end{bmatrix}$$ in row echelon form: $$\begin{bmatrix} 1 & 2 & 3\\ 0 & 1 & 2\\ 0 & 0 & 1\end{bmatrix}$$ I am aware that I can do a sequence of elementary row operations. I am also aware of the RowReduce command which puts a matrix in reduced row echelon form. I even saw a Method -> "OneStepRowReduction" used by the RowReduce command which I thought might be the choice.

Just wondering if there is a command for this that I cannot find.

• Your second matrix is not in row echelon form. Commented Mar 22, 2015 at 13:21
• What @enzotib quite correctly observes leads me to wonder if perhaps you actually want the "echelon" form over the ring of integers. That would be found with HermiteDecomposition. Commented Mar 22, 2015 at 13:27
• Fixed the row echelon error. Sorry about that. No, I just want the ordinary row echelon form students have to do when first introduced to Gaussian elimination. Commented Mar 22, 2015 at 15:12
• Note that there is nothing unique about a row echelon form, even such a form whose leading entries are all 1. So the question needs to be restated in a more precise manner. E.g., do you want a command that follows a particular algorithm for obtaining a row echelon form? As you noted, it's easy to do a sequence of elementary row operations, and to write little functions to do such operations, then to write a main function that calls those little functions to follow a particular reduction algorithm. But then how do you want to handle roundoff errors? (Or do you want exact rational arithmetic?) Commented Mar 22, 2015 at 20:24
• Here's a question similar to yours. Commented Feb 25, 2021 at 6:07

I think LUDecomposition should be able to do this. Using code from help in Mathematica docs:

a = {{1, 2, 3}, {2, 3, 4}, {-1, 0, 2}};
{lu, p, c} = LUDecomposition[a]
(u = lu SparseArray[{i_, j_} /; j >= i -> 1, {3, 3}]) // MatrixForm


If you want all pivots to be +1, this can now be easily done.

d = Position[Diagonal[u], -1]
(u[[First@#]] = -u[[First@#]]) & /@ d


Here is a similar question for reference only. The following code uses Gaussian elimination method to obtain the row echelon form:

rref[A_?MatrixQ] := Module[{a, m, n, i, j, k, l, L, B},
{m, n} = Dimensions[A]; B = A;
Print[MatrixForm@A];
i = 1; k = 1; While[i <= m,(*i表示行*)
While[k < n && B[[i ;;, k]] == ConstantArray[0, m - i + 1], k++;
If[k == n && B[[i ;;, k]] == ConstantArray[0, m - i + 1],
Return[B]]];(*k表示列, 如果全是0, 考察下一列, 如果最后一列也是0, 退出函数*)
For[l = i, l <= m, l++,
If[(a = B[[l, k]]) != 0 && k < n &&
B[[l, (k + 1) ;;]] == ConstantArray[0, n - k], B[[l, k]] = 1;
Print["第(1)", l, "行除以", a, "->", MatrixForm@B];]];
If[Length[a = DeleteCases[B[[i ;;, k]], 0]] != 0,
If[Length[Cases[Abs[a], 1]] != 0, L = 1,
If[Length[Cases[Abs[a], GCD @@ a]] != 0, L = Abs[GCD @@ a],
L = Abs[First[a]];](*如果有公因式就选取这个数, 不然选第一个不为零的数*),
Return[B]]];(*选取第一个不为0的数, 如果下边都成了0, 退出函数*)
j = i; While[j < m && Abs[B[[j, k]]] != L, j++;];(*找到所在的行, 用j表示*)
If[i != j, If[B[[j, k]] != 0, B[[{i, j}, ;;]] = B[[{j, i}, ;;]];
Print["第", i, "行和第", j,
"行换行->", MatrixForm@B], Return[B]](*都是0, 返回*)];(*换行: i<->j*)
For[l = k, l <= m, l++,
If[(a = B[[l, k]]) != 0 && l != i && B[[i, k]] != 0,
B[[l, ;;]] = B[[l, ;;]] - B[[l, k]]/B[[i, k]] B[[i, ;;]];

Print["第", l, "行加第", i, "行的", -a, "倍-> " MatrixForm@B]]]; i++;];

Return[B];]

    A = {{1, 2, 3}, {2, 3, 4}, {-1, 0, 2}}
rref[A]


• Why are there foreign characters in the solution? Commented Jan 5, 2022 at 22:27