4

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.

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
David
  • 14,883
  • 4
  • 44
  • 117
  • 2
    Your second matrix is not in row echelon form. – enzotib Mar 22 '15 at 13:21
  • 1
    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. – Daniel Lichtblau Mar 22 '15 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. – David Mar 22 '15 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?) – murray Mar 22 '15 at 20:24
  • Here's a question similar to yours. – A little mouse on the pampas Feb 25 '21 at 06:07

2 Answers2

3

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

Mathematica graphics

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

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

Mathematica graphics

Nasser
  • 143,286
  • 11
  • 154
  • 359
-1

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[&quot;第&quot;, l, &quot;行加第&quot;, i, &quot;行的&quot;, -a, &quot;倍-&gt; &quot; MatrixForm@B]]]; i++;];

Return[B];]

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

enter image description here