The elementary row operation rowadd
adds multiples of one or more rows to other rows of a matrix.
This is usually used as a means to solve systems of linear equations, of the form \(A x = b\), and rowadd
corresponds to adding equals to equals.
Details
The functions rowmult
and rowswap
complete the basic operations used in reduction
to row echelon form and Gaussian elimination. These functions are used for demonstration purposes.
Examples
A <- matrix(c(2, 1, -1,
-3, -1, 2,
-2, 1, 2), 3, 3, byrow=TRUE)
b <- c(8, -11, -3)
# using row operations to reduce below diagonal to 0
Ab <- cbind(A, b)
(Ab <- rowadd(Ab, 1, 2, 3/2)) # row 2 <- row 2 + 3/2 row 1
#> b
#> [1,] 2 1.0 -1.0 8
#> [2,] 0 0.5 0.5 1
#> [3,] -2 1.0 2.0 -3
(Ab <- rowadd(Ab, 1, 3, 1)) # row 3 <- row 3 + 1 row 1
#> b
#> [1,] 2 1.0 -1.0 8
#> [2,] 0 0.5 0.5 1
#> [3,] 0 2.0 1.0 5
(Ab <- rowadd(Ab, 2, 3, -4)) # row 3 <- row 3 - 4 row 2
#> b
#> [1,] 2 1.0 -1.0 8
#> [2,] 0 0.5 0.5 1
#> [3,] 0 0.0 -1.0 1
# multiply to make diagonals = 1
(Ab <- rowmult(Ab, 1:3, c(1/2, 2, -1)))
#> b
#> [1,] 1 0.5 -0.5 4
#> [2,] 0 1.0 1.0 2
#> [3,] 0 0.0 1.0 -1
# The matrix is now in triangular form
# Could continue to reduce above diagonal to zero
echelon(A, b, verbose=TRUE, fractions=TRUE)
#>
#> Initial matrix:
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,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
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,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
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,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
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,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
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,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
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,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
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,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
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,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
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,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
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,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
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,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
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,1] [,2] [,3] [,4]
#> [1,] 1 0 0 2
#> [2,] 0 1 0 3
#> [3,] 0 0 1 -1
# convenient use of pipes
I <- diag( 3 )
AA <- I |>
rowadd(3, 1, 1) |> # add 1 x row 3 to row 1
rowadd(1, 3, 1) |> # add 1 x row 1 to row 3
rowmult(2, 2) # multiply row 2 by 2