Returns the determinant of a square matrix X
,
computed either by Gaussian elimination, expansion by cofactors, or as the product of the eigenvalues of the matrix.
If the latter, X
must be symmetric.
Usage
Det(
X,
method = c("elimination", "eigenvalues", "cofactors"),
verbose = FALSE,
fractions = FALSE,
...
)
Arguments
- X
a square matrix
- method
one of `"elimination"` (the default), `"eigenvalues"`, or `"cofactors"` (for computation by minors and cofactors)
- verbose
logical; if
TRUE
, print intermediate steps- fractions
logical; if
TRUE
, try to express non-integers as rational numbers, using thefractions
function; if you require greater accuracy, you can set thecycles
(default 10) and/ormax.denominator
(default 2000) arguments tofractions
as a global option, e.g.,options(fractions=list(cycles=100, max.denominator=10^4))
.- ...
arguments passed to
gaussianElimination
orEigen
See also
Examples
A <- matrix(c(1,2,3,2,5,6,3,6,10), 3, 3) # nonsingular, symmetric
A
#> [,1] [,2] [,3]
#> [1,] 1 2 3
#> [2,] 2 5 6
#> [3,] 3 6 10
Det(A)
#> [1] 1
Det(A, verbose=TRUE, fractions=TRUE)
#>
#> Initial matrix:
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,1] [,2] [,3]
#> [1,] 1 2 3
#> [2,] 2 5 6
#> [3,] 3 6 10
#>
#> row: 1
#>
#> exchange rows 1 and 3
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,1] [,2] [,3]
#> [1,] 3 6 10
#> [2,] 2 5 6
#> [3,] 1 2 3
#>
#> multiply row 1 by 1/3
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,1] [,2] [,3]
#> [1,] 1 2 10/3
#> [2,] 2 5 6
#> [3,] 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]
#> [1,] 1 2 10/3
#> [2,] 0 1 -2/3
#> [3,] 1 2 3
#>
#> subtract row 1 from row 3
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,1] [,2] [,3]
#> [1,] 1 2 10/3
#> [2,] 0 1 -2/3
#> [3,] 0 0 -1/3
#>
#> row: 2
#>
#> multiply row 2 by 2 and subtract from row 1
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,1] [,2] [,3]
#> [1,] 1 0 14/3
#> [2,] 0 1 -2/3
#> [3,] 0 0 -1/3
#>
#> row: 3
#>
#> multiply row 3 by -3
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,1] [,2] [,3]
#> [1,] 1 0 14/3
#> [2,] 0 1 -2/3
#> [3,] 0 0 1
#>
#> multiply row 3 by 14/3 and subtract from row 1
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,1] [,2] [,3]
#> [1,] 1 0 0
#> [2,] 0 1 -2/3
#> [3,] 0 0 1
#>
#> multiply row 3 by 2/3 and add to row 2
#> Warning: Function is deprecated. See latexMatrix() and Eqn() for more recent approaches
#> [,1] [,2] [,3]
#> [1,] 1 0 0
#> [2,] 0 1 0
#> [3,] 0 0 1
#>
#> det = (-1)^1 x 3 x 1 x -1/3 = 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
Det(B)
#> [1] 0
C <- matrix(c(1, .5, .5, 1), 2, 2) # square, symmetric, nonsingular
Det(C)
#> [1] 0.75
Det(C, method="eigenvalues")
#> [1] 0.75
Det(C, method="cofactors")
#> [1] 0.75