Returns the (reduced) row-echelon form of the matrix A, using gaussianElimination.

echelon(A, B, reduced = TRUE, ...)

Arguments

A

coefficient matrix

B

right-hand side vector or matrix. If B is a matrix, the result gives solutions for each column as the right-hand side of the equations with coefficients in A.

reduced

logical; should reduced row echelon form be returned? If FALSE a non-reduced row echelon form will be returned

...

other arguments passed to gaussianElimination

Value

the reduced echelon form of X.

Details

When the matrix A is square and non-singular, the reduced row-echelon result will be the identity matrix, while the row-echelon from will be an upper triangle matrix. Otherwise, the result will have some all-zero rows, and the rank of the matrix is the number of not all-zero rows.

Author

John Fox

Examples

A <- matrix(c(2, 1, -1,
             -3, -1, 2,
             -2,  1, 2), 3, 3, byrow=TRUE)
b <- c(8, -11, -3)
echelon(A, b, verbose=TRUE, fractions=TRUE) # reduced row-echelon form
#> 
#> Initial matrix:
#>      [,1] [,2] [,3] [,4]
#> [1,]   2    1   -1    8 
#> [2,]  -3   -1    2  -11 
#> [3,]  -2    1    2   -3 
#> 
#> row: 1 
#> 
#>  exchange rows 1 and 2 
#>      [,1] [,2] [,3] [,4]
#> [1,]  -3   -1    2  -11 
#> [2,]   2    1   -1    8 
#> [3,]  -2    1    2   -3 
#> 
#>  multiply row 1 by -1/3 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1  1/3 -2/3 11/3
#> [2,]    2    1   -1    8
#> [3,]   -2    1    2   -3
#> 
#>  multiply row 1 by 2 and subtract from row 2 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1  1/3 -2/3 11/3
#> [2,]    0  1/3  1/3  2/3
#> [3,]   -2    1    2   -3
#> 
#>  multiply row 1 by 2 and add to row 3 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1  1/3 -2/3 11/3
#> [2,]    0  1/3  1/3  2/3
#> [3,]    0  5/3  2/3 13/3
#> 
#> row: 2 
#> 
#>  exchange rows 2 and 3 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1  1/3 -2/3 11/3
#> [2,]    0  5/3  2/3 13/3
#> [3,]    0  1/3  1/3  2/3
#> 
#>  multiply row 2 by 3/5 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1  1/3 -2/3 11/3
#> [2,]    0    1  2/5 13/5
#> [3,]    0  1/3  1/3  2/3
#> 
#>  multiply row 2 by 1/3 and subtract from row 1 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    0 -4/5 14/5
#> [2,]    0    1  2/5 13/5
#> [3,]    0  1/3  1/3  2/3
#> 
#>  multiply row 2 by 1/3 and subtract from row 3 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    0 -4/5 14/5
#> [2,]    0    1  2/5 13/5
#> [3,]    0    0  1/5 -1/5
#> 
#> row: 3 
#> 
#>  multiply row 3 by 5 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    0 -4/5 14/5
#> [2,]    0    1  2/5 13/5
#> [3,]    0    0    1   -1
#> 
#>  multiply row 3 by 4/5 and add to row 1 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    0    0    2
#> [2,]    0    1  2/5 13/5
#> [3,]    0    0    1   -1
#> 
#>  multiply row 3 by 2/5 and subtract from row 2 
#>      [,1] [,2] [,3] [,4]
#> [1,]  1    0    0    2  
#> [2,]  0    1    0    3  
#> [3,]  0    0    1   -1  
echelon(A, b, reduced=FALSE, verbose=TRUE, fractions=TRUE) # row-echelon form
#> 
#> Initial matrix:
#>      [,1] [,2] [,3] [,4]
#> [1,]   2    1   -1    8 
#> [2,]  -3   -1    2  -11 
#> [3,]  -2    1    2   -3 
#> 
#> row: 1 
#> 
#>  exchange rows 1 and 2 
#>      [,1] [,2] [,3] [,4]
#> [1,]  -3   -1    2  -11 
#> [2,]   2    1   -1    8 
#> [3,]  -2    1    2   -3 
#> 
#>  multiply row 1 by -1/3 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1  1/3 -2/3 11/3
#> [2,]    2    1   -1    8
#> [3,]   -2    1    2   -3
#> 
#>  multiply row 1 by 2 and subtract from row 2 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1  1/3 -2/3 11/3
#> [2,]    0  1/3  1/3  2/3
#> [3,]   -2    1    2   -3
#> 
#>  multiply row 1 by 2 and add to row 3 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1  1/3 -2/3 11/3
#> [2,]    0  1/3  1/3  2/3
#> [3,]    0  5/3  2/3 13/3
#> 
#> row: 2 
#> 
#>  exchange rows 2 and 3 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1  1/3 -2/3 11/3
#> [2,]    0  5/3  2/3 13/3
#> [3,]    0  1/3  1/3  2/3
#> 
#>  multiply row 2 by 3/5 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1  1/3 -2/3 11/3
#> [2,]    0    1  2/5 13/5
#> [3,]    0  1/3  1/3  2/3
#> 
#>  multiply row 2 by 1/3 and subtract from row 3 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1  1/3 -2/3 11/3
#> [2,]    0    1  2/5 13/5
#> [3,]    0    0  1/5 -1/5
#> 
#> row: 3 
#> 
#>  multiply row 3 by 5 
#>      [,1] [,2] [,3] [,4]
#> [1,]    1  1/3 -2/3 11/3
#> [2,]    0    1  2/5 13/5
#> [3,]    0    0    1   -1

A <- matrix(c(1,2,3,4,5,6,7,8,10), 3, 3) # a nonsingular matrix
A
#>      [,1] [,2] [,3]
#> [1,]    1    4    7
#> [2,]    2    5    8
#> [3,]    3    6   10
echelon(A, reduced=FALSE) # the row-echelon form of A
#>      [,1] [,2]     [,3]
#> [1,]    1    2 3.333333
#> [2,]    0    1 1.833333
#> [3,]    0    0 1.000000
echelon(A) # the reduced row-echelon form of A
#>      [,1] [,2] [,3]
#> [1,]    1    0    0
#> [2,]    0    1    0
#> [3,]    0    0    1

b <- 1:3
echelon(A, b)  # solving the matrix equation Ax = b
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    0    0    1
#> [2,]    0    1    0    0
#> [3,]    0    0    1    0
echelon(A, diag(3)) # inverting A
#>      [,1] [,2] [,3]       [,4]       [,5] [,6]
#> [1,]    1    0    0 -0.6666667 -0.6666667    1
#> [2,]    0    1    0 -1.3333333  3.6666667   -2
#> [3,]    0    0    1  1.0000000 -2.0000000    1

B <- matrix(1:9, 3, 3) # a singular matrix
B
#>      [,1] [,2] [,3]
#> [1,]    1    4    7
#> [2,]    2    5    8
#> [3,]    3    6    9
echelon(B)
#>      [,1] [,2] [,3]
#> [1,]    1    0   -1
#> [2,]    0    1    2
#> [3,]    0    0    0
echelon(B, reduced=FALSE)
#>      [,1] [,2] [,3]
#> [1,]    1    2    3
#> [2,]    0    1    2
#> [3,]    0    0    0
echelon(B, b)
#>      [,1] [,2] [,3] [,4]
#> [1,]    1    0   -1    1
#> [2,]    0    1    2    0
#> [3,]    0    0    0    0
echelon(B, diag(3))
#>      [,1] [,2] [,3] [,4] [,5]       [,6]
#> [1,]    1    0   -1 -1.0    0  0.6666667
#> [2,]    0    1    2  0.5    0 -0.1666667
#> [3,]    0    0    0 -0.5    1 -0.5000000