如何将R中的矩阵简化为梯形形式?
矩阵的梯形形式是具有以下特征的矩阵:
1.每行中的第一个非零元素(称为前导条目)是1。
2.每个前导条目位于上一行中前导条目右侧的一栏中。
3.具有所有零元素(如果有)的行位于具有非零元素的行之下。
在R中,我们可以使用matlib包的梯形函数来查找矩阵的梯形形式。
示例
> M<-matrix(rpois(25,10),ncol=5) > M
输出结果
[,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
示例
> V<-rpois(5,2) > V
输出结果
[1] 4 2 4 1 2
加载matlib包并找到矩阵M的梯形形式:
示例
> library(matlib) > echelon(M,V,verbose=TRUE,fractions=TRUE)
初始矩阵:
输出结果
[,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
列数:1
输出结果
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
排:2
输出结果
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
行:3
输出结果
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
排:4
输出结果
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
行:5
输出结果
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