1. Home
2. FROWCANONICALMATRIX procedure

# FROWCANONICALMATRIX procedure

Puts a matrix into row canonical, or reduced row echelon, form (C.J. Brien).

### Option

`PRINT` = string token Controls printed output (`rowcanonicalmatrix`); default `*` i.e. none

### Parameters

`MATRIX` = matrices Matrix to be put into row canonical form Matrix in row canonical form

### Description

A matrix is in row canonical, or reduced row echelon, form if the following conditions apply:

1)   the leading coefficient in each row (i.e. its first non-zero element) is one,

2)   the other elements in the column of each leading coefficient are zero, and

3)   all rows that contain only zeros are at the bottom of the matrix.

The matrix to be put into row canonical form is specified by the `MATRIX` parameter. The resulting matrix, in that form, can be saved by the `ROWCANONICALMATRIX` parameter. It can be printed by setting option `PRINT=rowcanonicalmatrix`.

You can use the procedure to calculate the rank of a matrix, by counting the number of non-zero elements on the diagonal of its equivalent row canonical matrix. You can solve a set of consistent linear equations, by defining a matrix with the coefficients on the left-hand side of the assignments in its left-hand columns, and the values on the right-hand side of the assignments in its final column. You can also invert a square matrix, by appending the identity matrix to its right. These uses are demonstrated in the Example.

Option: `PRINT`.

Parameters: `MATRIX`, `ROWCANONICALMATRIX`.

### Method

The row canonical form is produced by Gauss-Jordan elimination, which involves performing elementary row operations on the matrix. These operations are:

1)   swap two rows,

2)   multiply a row by a non-zero number, and

3)   add a multiple of one row to another row.

The algorithm is based on that provided online by Rosetta Code
(see http://rosettacode.org/wiki/Reduced_row_echelon_form).

Commands for: Calculations and manipulation.

### Example

```CAPTION 'FROWCANONICALMATRIX examples',\
'1) form the inverse of a 3x3 matrix'; STYLE=meta,plain
MATRIX [ROWS=3; COLUMNS=3; VALUES= 1,  2,  1,\
2,  5, -1,\
3, -2, -1]  mat
" augment mat with the 3x3 identity matrix "
MATRIX [ROWS=3; COLUMNS=6] mati
CALCULATE mati\$[*;!(1...3)] = mat
&         mati\$[*;!(4...6)] = IDENTITY(3)
FROWCANONICALMATRIX mati; ROWCANONICALMATRIX=rcmat
CAPTION 'check the result - r.h.s of rcmat is the inverse of mat'
CALCULATE invmat = rcmat\$[*;!(4...6)]
PRINT rcmat & mat,invmat; FIELD=10 & mat *+ invmat
CAPTION '2) solve some (consistent) linear equations'
" the equations are:
x  + 2*y + z = 20
2*x + 5*y - z =  5
3*x - 2*y - z =  1 "
MATRIX [ROWS=3; COLUMNS=1; VALUES=20,5,1] rhs
" augment mat with the values on the right-hand side of the assignments "
MATRIX [ROWS=3; COLUMNS=4] mateqn
CALCULATE mateqn\$[*;!(1...3)] = mat
&         mateqn\$[*;4] = rhs
FROWCANONICALMATRIX mateqn; ROWCANONICALMATRIX=rcmat
CAPTION 'check the results, in the final column of the matrix'
PRINT rcmat
PRINT invmat *+ rhs
CAPTION '3) calculate the rank of a matrix',\
'(rank will be 3 as row 3 = 2 * row 1 + row 2)'
MATRIX [ROWS=4; COLUMNS=4; VALUES= 1,  2,  1, 2,\
2,  5, -1, 3,\
4,  9,  1, 7,\
3, -2, -1, 1]  mat
FROWCANONICALMATRIX mat; ROWCANONICALMATRIX=rcmat
CALCULATE rank = SUM(DIAGONAL(rcmat).NE.0)
PRINT     rcmat
PRINT     [IPRINT=*; STYLE=plain] 'Rank =',rank; DECIMALS=0; JUST=left
```
Updated on March 7, 2019