6

I would like to figure out a way to see the row operations RowReduce uses to arrive at the reduced row echelon form of a matrix. I am aware of the step-by-step solutions in Wolfram Alpha and Wolfram Alpha Style Notebooks available from the File tab. I want to compute a programmatic sequence of row operations. I think there might be a way to use Reap and Sow to arrive at my goal. My grasp of Reap and Sow is weak, unfortunately.

A similar question covers row reduced function for square matrices. I would like to take any matrix and find the row echelon form.

Step by Step Solutions

Peter Burbery
  • 1,695
  • 4
  • 15

1 Answers1

7

Here is an updated version which shows step by step the forward Gaussian Elimination steps for any size matrix.

This does not do the reduced echelon part (reverse direction). This can be easily added if needed.

Example 1

mat = {{1, 2, 1}, {-2, -3, 1}, {3, 5, 0}};
displayRREF[mat]

Mathematica graphics

Example 2

mat = {{1, 2, 2, 4}, {1, 3, 3, 5}, {2, 6, 5, 6}};
displayRREF[mat]

Mathematica graphics

Example 3

mat = {{-7, -6, -12, -33}, {5, 5, 7, 24}, {1, 0, 4, 5}};
displayRREF[mat]

Mathematica graphics

Example 4

mat = {{1, -1, 2, 1}, {2, 1, 1, 8}, {1, 1, 0, 5}};
displayRREF[mat]

Mathematica graphics

Example 5

mat = {{2, 1, 7, -7, 2}, {-3, 4, -5, -6, 3}, {1, 1, 4, -5, 2}};
displayRREF[mat]

Mathematica graphics

Example 6

mat = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
displayRREF[mat]

Mathematica graphics

Code

Note, code was written in a code cell. Not an input cell. So formating can get messed up when using this back in an input cell. I've also put the notebook in case hard to copy this here

displayRREF[matIn_?(MatrixQ[#] &)] := 
    Module[{mat = matIn, nRows,nCols,p, newp, tmp,scale},
    (*version Nov 21, 2021*)
{nRow,nCol} = Dimensions[mat];
Print[">>>>>>Starting forward Gaussian elimination phase using ", MatrixForm[mat]];

Do[
    If[p<nRow,Print["pivot now is (", p, ",", p, ")" ]];
    If[ mat[[p,p]] === 0 && p<nRow,
        newp = FirstPosition[mat[[p;;nRow, p]], _?(# != 0 &)];
        If[ newp===Missing["NotFound"],
            Print["Unable to continue. Can not find non-zero pivot"];
            Return[Module];
        ];

        newp            = p+First@newp-1;
        tmp             = mat[[p,All]];
        mat[[p,All]]    = mat[[newp,All]];
        mat[[newp,All]] = tmp;
        Print["Since pivot is zero, then we exchange row ",newp," with row ",p,". ===> ",MatrixForm[mat]];            
    ];

    If[ mat[[p,p]] != 1,
        newp = FirstPosition[mat[[ p;;nRow, p]], _?(Abs[#] == 1 &)];

        If[ newp=!=Missing["NotFound"],
             newp            = p+First@newp-1;
             tmp             = mat[[p,All]];
             mat[[p,All]]    = mat[[newp,All]];
             mat[[newp,All]] = tmp;
             Print["Swapping row ",p," with row ",newp," ===> ",MatrixForm[mat]];
             If[mat[[p,p]]==-1,
                mat[[p,All]] =- mat[[p,All]];
                Print["Scaling row ",p," by -1 ===>",MatrixForm[mat]]
             ]
             ,
             If[mat[[p,p]]=!=0,
                 mat[[p,All]] = mat[[p,All]]/mat[[p,p]];
                 Print["Scaling pivot row so that the pivot element is one. ===>  ",MatrixForm[mat]]
             ]
       ]
    ];

    (*now elimination is done to zero all rows below the pivot row*)    
    Do[
        scale =  mat[[j,p]]*mat[[p,p]];
        mat[[j,All]] = mat[[j,All]] - scale*mat[[p,All]];
        Print["R(",j,") ->  R(",j,") - (", scale,") * R(",p,")   ===> ", MatrixForm[mat]]
        ,   
        {j, p+1, nRow}
    ]
    ,
    {p, 1, nRow}
]

]

Nasser
  • 143,286
  • 11
  • 154
  • 359
  • 1
    This is amazing. Underrated answer – Manuel Hernandez Dec 24 '22 at 01:12
  • @Nasser

    The code fails if there is a symbolic term in the matrix mat.

    – granular_bastard Apr 29 '23 at 19:57
  • @granularbastard yes, this is not supposed to work on symbolic terms in mat, because it needs to check for pivot is zero or not and such things in the code where the terms have to be numerical. So this is meant to work only on numerical matrices not symbolic I am afraid. – Nasser Apr 29 '23 at 20:16
  • Could you extend your code to symbolic matrices? – granular_bastard Apr 29 '23 at 20:17
  • @granularbastard It is probably possible, but I really do not have the spare time to do it as this time as I am working on other things and this will take much more time. You could post a question asking for this, may be someone else can do it, or may be there is package out there that does RREF on symbolic matrices showing step by step that someone can point you to. – Nasser Apr 29 '23 at 20:19
  • a new question was posed: https://mathematica.stackexchange.com/q/284439/69288 – granular_bastard Apr 29 '23 at 20:29