Reduced row echelon form

You are encouraged to solve this task according to the task description, using any language you may know.


Task

Show how to compute the reduced row echelon form
(a.k.a. row canonical form) of a matrix.

The matrix can be stored in any datatype that is convenient
(for most languages, this will probably be a two-dimensional array).

Built-in functions or this pseudocode (from Wikipedia) may be used:

function ToReducedRowEchelonForm(Matrix M) is
    lead := 0
    rowCount := the number of rows in M
    columnCount := the number of columns in M
    for 0 ≤ r < rowCount do
        if columnCount ≤ lead then
            stop
        end if
        i = r
        while M[i, lead] = 0 do
            i = i + 1
            if rowCount = i then
                i = r
                lead = lead + 1
                if columnCount = lead then
                    stop
                end if
            end if
        end while
        Swap rows i and r
        If M[r, lead] is not 0 divide row r by M[r, lead]
        for 0 ≤ i < rowCount do
            if i ≠ r do
                Subtract M[i, lead] multiplied by row r from row i
            end if
        end for
        lead = lead + 1
    end for
end function

For testing purposes, the RREF of this matrix:

 1    2   -1   -4
 2    3   -1   -11
-2    0   -3    22

is:

 1    0    0   -8
 0    1    0    1
 0    0    1   -2



*        reduced row echelon form  27/08/2015
RREF     CSECT
         USING  RREF,R12
         LR     R12,R15
         LA     R10,1              lead=1
         LA     R7,1
LOOPR    CH     R7,NROWS           do r=1 to nrows
         BH     ELOOPR
         CH     R10,NCOLS          if lead>=ncols
         BNL    ELOOPR
         LR     R8,R7              i=r
WHILE    LR     R1,R8              do while m(i,lead)=0
         BCTR   R1,0
         MH     R1,NCOLS
         LR     R6,R10             lead
         BCTR   R6,0
         AR     R1,R6
         SLA    R1,2
         L      R6,M(R1)           m(i,lead)
         LTR    R6,R6
         BNZ    EWHILE             m(i,lead)<>0           
         LA     R8,1(R8)           i=i+1
         CH     R8,NROWS           if i=nrows
         BNE    EIF
         LR     R8,R7              i=r
         LA     R10,1(R10)         lead=lead+1
         CH     R10,NCOLS          if lead=ncols
         BE     ELOOPR
EIF      B      WHILE
EWHILE   LA     R9,1
LOOPJ1   CH     R9,NCOLS           do j=1 to ncols
         BH     ELOOPJ1
         LR     R1,R7              r
         BCTR   R1,0
         MH     R1,NCOLS
         LR     R6,R9              j
         BCTR   R6,0
         AR     R1,R6
         SLA    R1,2
         LA     R3,M(R1)           R3=@m(r,j)
         LR     R1,R8              i
         BCTR   R1,0
         MH     R1,NCOLS
         LR     R6,R9              j
         BCTR   R6,0
         AR     R1,R6
         SLA    R1,2
         LA     R4,M(R1)           R4=@m(i,j)
         L      R2,0(R3)
         MVC    0(2,R3),0(R4)      swap m(i,j),m(r,j)
         ST     R2,0(R4)
         LA     R9,1(R9)           j=j+1
         B      LOOPJ1
ELOOPJ1  LR     R1,R7              r
         BCTR   R1,0
         MH     R1,NCOLS
         LR     R6,R10             lead
         BCTR   R6,0
         AR     R1,R6
         SLA    R1,2
         L      R11,M(R1)          n=m(r,lead)
         CH     R11,=H'1'          if n^=1
         BE     ELOOPJ2
         LA     R9,1
LOOPJ2   CH     R9,NCOLS           do j=1 to ncols
         BH     ELOOPJ2
         LR     R1,R7              r
         BCTR   R1,0
         MH     R1,NCOLS
         LR     R6,R9              j
         BCTR   R6,0
         AR     R1,R6
         SLA    R1,2
         LA     R5,M(R1)           R5=@m(i,j)
         L      R2,0(R5)           m(r,j)
         LR     R1,R11             n
         SRDA   R2,32
         DR     R2,R1              m(r,j)/n
         ST     R3,0(R5)           m(r,j)=m(r,j)/n
         LA     R9,1(R9)           j=j+1
         B      LOOPJ2
ELOOPJ2  LA     R8,1
LOOPI3   CH     R8,NROWS           do i=1 to nrows
         BH     ELOOPI3
         CR     R8,R7              if i^=r
         BE     ELOOPJ3
         LR     R1,R8              i
         BCTR   R1,0
         MH     R1,NCOLS
         LR     R6,R10             lead
         BCTR   R6,0
         AR     R1,R6
         SLA    R1,2
         L      R11,M(R1)          n=m(i,lead)
         LA     R9,1
LOOPJ3   CH     R9,NCOLS           do j=1 to ncols
         BH     ELOOPJ3
         LR     R1,R8              i
         BCTR   R1,0
         MH     R1,NCOLS
         LR     R6,R9              j
         BCTR   R6,0
         AR     R1,R6
         SLA    R1,2
         LA     R4,M(R1)           R4=@m(i,j)
         L      R5,0(R4)           m(i,j)
         LR     R1,R7              r
         BCTR   R1,0
         MH     R1,NCOLS
         LR     R6,R9              j
         BCTR   R6,0
         AR     R1,R6
         SLA    R1,2
         L      R3,M(R1)           m(r,j)
         MR     R2,R11             m(r,j)*n
         SR     R5,R3              m(i,j)-m(r,j)*n
         ST     R5,0(R4)           m(i,j)=m(i,j)-m(r,j)*n
         LA     R9,1(R9)           j=j+1
         B      LOOPJ3
ELOOPJ3  LA     R8,1(R8)           i=i+1
         B      LOOPI3
ELOOPI3  LA     R10,1(R10)         lead=lead+1
         LA     R7,1(R7)           r=r+1
         B      LOOPR
ELOOPR   LA     R8,1
LOOPI4   CH     R8,NROWS           do i=1 to nrows
         BH     ELOOPI4
         SR     R10,R10            pgi=0
         LA     R9,1
LOOPJ4   CH     R9,NCOLS           do j=1 to ncols
         BH     ELOOPJ4
         LR     R1,R8              i
         BCTR   R1,0
         MH     R1,NCOLS
         LR     R6,R9              j
         BCTR   R6,0
         AR     R1,R6
         SLA    R1,2
         L      R6,M(R1)           m(i,j)
         LA     R3,PG
         AR     R3,R10
         XDECO  R6,0(R3)           edit m(i,j)
         LA     R10,12(10)         pgi=pgi+12
         LA     R9,1(R9)           j=j+1
         B      LOOPJ4
ELOOPJ4  XPRNT  PG,48              print m(i,j)
         LA     R8,1(R8)           i=i+1
         B      LOOPI4
ELOOPI4  XR     R15,R15
         BR     R14
NROWS    DC     H'3'
NCOLS    DC     H'4'
M        DC     F'1',F'2',F'-1',F'-4'
         DC     F'2',F'3',F'-1',F'-11'
         DC     F'-2',F'0',F'-3',F'22'
PG       DC     CL48' '
         YREGS
         END    RREF