This example shows two classical ways to find the determinant, det (A) of a square matrix. They each work by reducing the problem to a series of smaller ones which can be more easily calculated.
det()
by cofactor expansionSet up a 3 × 3 matrix, and find its determinant (so we know what the answer should be).
## [1] 50
The cofactor Ai, j of element ai, j is the signed determinant of what is left when row i, column j of the matrix A are deleted. NB: In R, negative subscripts delete rows or columns.
## 18 == 18
## -8 == -8
## -6 == -6
In symbols: det (A) = a1, 1 * A1, 1 + a1, 2 * A1, 2 + a1, 3 * A1, 3
rowCofactors()
is a convenience function, that
calculates these all together
## [1] 18 -8 -6
Voila: Multiply row 1 times the cofactors of its elements. NB: In R, this multiplication gives a 1 × 1 matrix.
## [,1]
## [1,] 50
## [1] TRUE
det()
by Gaussian elimination
(pivoting)This example follows Green and Carroll, Table 2.2. Start with a 4 x 4
matrix, M, and save
det(M)
.
M <- matrix(c(2, 3, 1, 2,
4, 2, 3, 4,
1, 4, 2, 2,
3, 1, 0, 1), nrow=4, ncol=4, byrow=TRUE)
(dsave <- det(M))
## [1] 15
det()
will be the product of the ‘pivots’, the leading
diagonal elements. This step reduces row 1 and column 1 to 0, so it may
be discarded. NB: In R, dropping a row/column can change a matrix to a
vector, so we use drop = FALSE
inside the subscript.
## [1] 2
## [,1] [,2] [,3] [,4]
## [1,] 1 1.5 0.5 1
## [,1] [,2] [,3] [,4]
## [1,] 0 0.0 0.0 0
## [2,] 0 -4.0 1.0 0
## [3,] 0 2.5 1.5 1
## [4,] 0 -3.5 -1.5 -2
## [,1] [,2] [,3]
## [1,] 1 -0.25 0
## [,1] [,2] [,3]
## [1,] 0 0.000 0
## [2,] 0 2.125 1
## [3,] 0 -2.375 -2