How to reduce a matrix in R to echelon form?

R ProgrammingServer Side ProgrammingProgramming

The echelon form of a matrix is the matrix that has the following characteristics:

1. The first non-zero element in each row, called the leading entry, is 1.

2. Each leading entry is in a column to the right of the leading entry in the previous row.

3. Rows with all zero elements, if any, are below rows having a non-zero element.

In R, we can use echelon function of matlib package to find the echelon form of the matrix.

Example

Live Demo

> M<-matrix(rpois(25,10),ncol=5)
> M

Output

   [,1] [,2] [,3] [,4] [,5]
[1,] 8 11 3 10 13
[2,] 9 9 7 15 11
[3,] 10 13 10 14 13
[4,] 7 11 11 12 17
[5,] 13 10 9 20 13

Example

Live Demo

> V<-rpois(5,2)
> V

Output

[1] 4 2 4 1 2

Loading matlib package and finding the echelon form of matrix M:

Example

> library(matlib)
> echelon(M,V,verbose=TRUE,fractions=TRUE)

Initial matrix:

Output

[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 8 11 3 10 13 4
[2,] 9 9 7 15 11 2
[3,] 10 13 10 14 13 4
[4,] 7 11 11 12 17 1
[5,] 13 10 9 20 13 2

row: 1

Output

exchange rows 1 and 5
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 13 10 9 20 13 2
[2,] 9 9 7 15 11 2
[3,] 10 13 10 14 13 4
[4,] 7 11 11 12 17 1
[5,] 8 11 3 10 13 4

multiply row 1 by 1/13
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 10/13 9/13 20/13 1 2/13
[2,] 9 9 7 15 11 2
[3,] 10 13 10 14 13 4
[4,] 7 11 11 12 17 1
[5,] 8 11 3 10 13 4

multiply row 1 by 9 and subtract from row 2
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 10/13 9/13 20/13 1 2/13
[2,] 0 27/13 10/13 15/13 2 8/13
[3,] 10 13 10 14 13 4
[4,] 7 11 11 12 17 1
[5,] 8 11 3 10 13 4

multiply row 1 by 10 and subtract from row 3
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 10/13 9/13 20/13 1 2/13
[2,] 0 27/13 10/13 15/13 2 8/13
[3,] 0 69/13 40/13 -18/13 3 32/13
[4,] 7 11 11 12 17 1
[5,] 8 11 3 10 13 4

multiply row 1 by 7 and subtract from row 4
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 10/13 9/13 20/13 1 2/13
[2,] 0 27/13 10/13 15/13 2 8/13
[3,] 0 69/13 40/13 -18/13 3 32/13
[4,] 0 73/13 80/13 16/13 10 -1/13
[5,] 8 11 3 10 13 4

multiply row 1 by 8 and subtract from row 5
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 10/13 9/13 20/13 1 2/13
[2,] 0 27/13 10/13 15/13 2 8/13
[3,] 0 69/13 40/13 -18/13 3 32/13
[4,] 0 73/13 80/13 16/13 10 -1/13
[5,] 0 63/13 -33/13 -30/13 5 36/13

row: 2

Output

exchange rows 2 and 4
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 10/13 9/13 20/13 1 2/13
[2,] 0 73/13 80/13 16/13 10 -1/13
[3,] 0 69/13 40/13 -18/13 3 32/13
[4,] 0 27/13 10/13 15/13 2 8/13
[5,] 0 63/13 -33/13 -30/13 5 36/13

multiply row 2 by 13/73
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 10/13 9/13 20/13 1 2/13
[2,] 0 1 80/73 16/73 130/73 -1/73
[3,] 0 69/13 40/13 -18/13 3 32/13
[4,] 0 27/13 10/13 15/13 2 8/13
[5,] 0 63/13 -33/13 -30/13 5 36/13

multiply row 2 by 10/13 and subtract from row 1
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 -11/73 100/73 -27/73 12/73
[2,] 0 1 80/73 16/73 130/73 -1/73
[3,] 0 69/13 40/13 -18/13 3 32/13
[4,] 0 27/13 10/13 15/13 2 8/13
[5,] 0 63/13 -33/13 -30/13 5 36/13

multiply row 2 by 69/13 and subtract from row 3
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 -11/73 100/73 -27/73 12/73
[2,] 0 1 80/73 16/73 130/73 -1/73
[3,] 0 0 -200/73 -186/73 -471/73 185/73
[4,] 0 27/13 10/13 15/13 2 8/13
[5,] 0 63/13 -33/13 -30/13 5 36/13

multiply row 2 by 27/13 and subtract from row 4
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 -11/73 100/73 -27/73 12/73
[2,] 0 1 80/73 16/73 130/73 -1/73
[3,] 0 0 -200/73 -186/73 -471/73 185/73
[4,] 0 0 -110/73 51/73 -124/73 47/73
[5,] 0 63/13 -33/13 -30/13 5 36/13

multiply row 2 by 63/13 and subtract from row 5
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 -11/73 100/73 -27/73 12/73
[2,] 0 1 80/73 16/73 130/73 -1/73
[3,] 0 0 -200/73 -186/73 -471/73 185/73
[4,] 0 0 -110/73 51/73 -124/73 47/73
[5,] 0 0 -573/73 -246/73 -265/73 207/73

row: 3

Output

exchange rows 3 and 5
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 -11/73 100/73 -27/73 12/73
[2,] 0 1 80/73 16/73 130/73 -1/73
[3,] 0 0 -573/73 -246/73 -265/73 207/73
[4,] 0 0 -110/73 51/73 -124/73 47/73
[5,] 0 0 -200/73 -186/73 -471/73 185/73

multiply row 3 by -73/573
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 -11/73 100/73 -27/73 12/73
[2,] 0 1 80/73 16/73 130/73 -1/73
[3,] 0 0 1 82/191 265/573 -69/191
[4,] 0 0 -110/73 51/73 -124/73 47/73
[5,] 0 0 -200/73 -186/73 -471/73 185/73

multiply row 3 by 11/73 and add to row 1
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 274/191 -172/573 21/191
[2,] 0 1 80/73 16/73 130/73 -1/73
[3,] 0 0 1 82/191 265/573 -69/191
[4,] 0 0 -110/73 51/73 -124/73 47/73
[5,] 0 0 -200/73 -186/73 -471/73 185/73

multiply row 3 by 80/73 and subtract from row 2
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 274/191 -172/573 21/191
[2,] 0 1 0 -48/191 730/573 73/191
[3,] 0 0 1 82/191 265/573 -69/191
[4,] 0 0 -110/73 51/73 -124/73 47/73
[5,] 0 0 -200/73 -186/73 -471/73 185/73

multiply row 3 by 110/73 and add to row 4
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 274/191 -172/573 21/191
[2,] 0 1 0 -48/191 730/573 73/191
[3,] 0 0 1 82/191 265/573 -69/191
[4,] 0 0 0 257/191 -574/573 19/191
[5,] 0 0 -200/73 -186/73 -471/73 185/73

multiply row 3 by 200/73 and add to row 5
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 274/191 -172/573 21/191
[2,] 0 1 0 -48/191 730/573 73/191
[3,] 0 0 1 82/191 265/573 -69/191
[4,] 0 0 0 257/191 -574/573 19/191
[5,] 0 0 0 -262/191 -2971/573 295/191

row: 4

Output

exchange rows 4 and 5
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 274/191 -172/573 21/191
[2,] 0 1 0 -48/191 730/573 73/191
[3,] 0 0 1 82/191 265/573 -69/191
[4,] 0 0 0 -262/191 -2971/573 295/191
[5,] 0 0 0 257/191 -574/573 19/191

multiply row 4 by -191/262
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 274/191 -172/573 21/191
[2,] 0 1 0 -48/191 730/573 73/191
[3,] 0 0 1 82/191 265/573 -69/191
[4,] 0 0 0 1 2971/786 -295/262
[5,] 0 0 0 257/191 -574/573 19/191

multiply row 4 by 274/191 and subtract from row 1
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 0 -2249/393 226/131
[2,] 0 1 0 -48/191 730/573 73/191
[3,] 0 0 1 82/191 265/573 -69/191
[4,] 0 0 0 1 2971/786 -295/262
[5,] 0 0 0 257/191 -574/573 19/191

multiply row 4 by 48/191 and add to row 2
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 0 -2249/393 226/131
[2,] 0 1 0 0 874/393 13/131
[3,] 0 0 1 82/191 265/573 -69/191
[4,] 0 0 0 1 2971/786 -295/262
[5,] 0 0 0 257/191 -574/573 19/191

multiply row 4 by 82/191 and subtract from row 3
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 0 -2249/393 226/131
[2,] 0 1 0 0 874/393 13/131
[3,] 0 0 1 0 -152/131 16/131
[4,] 0 0 0 1 2971/786 -295/262
[5,] 0 0 0 257/191 -574/573 19/191

multiply row 4 by 257/191 and subtract from row 5
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 0 -2249/393 226/131
[2,] 0 1 0 0 874/393 13/131
[3,] 0 0 1 0 -152/131 16/131
[4,] 0 0 0 1 2971/786 -295/262
[5,] 0 0 0 0 -1595/262 423/262

row: 5

Output

multiply row 5 by -262/1595
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 0 -2249/393 226/131
[2,] 0 1 0 0 874/393 13/131
[3,] 0 0 1 0 -152/131 16/131
[4,] 0 0 0 1 2971/786 -295/262
[5,] 0 0 0 0 1 -266/1003

multiply row 5 by 2249/393 and add to row 1
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 0 0 331/1595
[2,] 0 1 0 0 874/393 13/131
[3,] 0 0 1 0 -152/131 16/131
[4,] 0 0 0 1 2971/786 -295/262
[5,] 0 0 0 0 1 -266/1003

multiply row 5 by 874/393 and subtract from row 2
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 0 0 331/1595
[2,] 0 1 0 0 0 1099/1595
[3,] 0 0 1 0 -152/131 16/131
[4,] 0 0 0 1 2971/786 -295/262
[5,] 0 0 0 0 1 -266/1003

multiply row 5 by 152/131 and add to row 3
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 0 0 331/1595
[2,] 0 1 0 0 0 1099/1595
[3,] 0 0 1 0 0 -296/1595
[4,] 0 0 0 1 2971/786 -295/262
[5,] 0 0 0 0 1 -266/1003

multiply row 5 by 2971/786 and subtract from row 4
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 0 0 0 0 331/1595
[2,] 0 1 0 0 0 1099/1595
[3,] 0 0 1 0 0 -296/1595
[4,] 0 0 0 1 0 -197/1595
[5,] 0 0 0 0 1 -266/1003
raja
Published on 23-Nov-2020 10:25:10
Advertisements