det in R: How to Calculate Determinant of Matrix in R

The determinant in mathematics helps us find an inverse of a matrix. The determinant of a matrix is a number that is specifically defined only for square matrices. The determinant is used in many instances in calculus and other matrix-related algebra; it actually serves the matrix in terms of a real number which can be used in solving the system of linear equations and finding an inverse of a matrix.

det in R

To calculate the determinant of a matrix in R, use the det() function. The det() is a built-in R generic function that returns the determinant’s modulus separately, optionally on the logarithm scale and the determinant sign. The set() function takes data as required and logarithm as an optional argument and returns the determinant, never the determinant’s logarithm.

Syntax

det(data, logarithm = TRUE, ...)

Parameters

data: It is a matrix whose determinant we need to calculate.

Logarithm: It is a logical argument; If TRUE (default) returns the logarithm of the determinant’s modulus.

…: These are the further arguments passed to or from other methods.

Example

To create a matrix in R, use the matrix() function. The dimension of the Matrix is defined by nrow and ncol property.

vt <- c(-1, -3, -5, -7, -8, 9, 11, 13, 15)
mtrx <- matrix(vt, nrow = 3, ncol = 3)
print(det(mtrx))

Output

     [,1] [,2] [,3]
[1,]  -1   -7   11
[2,]  -3   -8   13
[3,]  -5    9   15

Now, let’s find the determinant of the matrix using the det() function.

vt <- c(-1, -3, -5, -7, -8, 9, 11, 13, 15)
mtrx <- matrix(vt, nrow = 3, ncol = 3)
print(det(mtrx))

Output

[1] -360

The determinant of the above matrix is -360. Now, if you transpose the matrix and then calculate the determinant, then also it will return the same output as above. Let’s see how to do that.

The Transpose of a Matrix is defined as “A Matrix which is formed by turning all the rows of a given matrix into columns and vice-versa.”

To transpose a matrix in R, use the t() method.

vt <- c(-1, -3, -5, -7, -8, 9, 11, 13, 15)
mtrx <- matrix(vt, nrow = 3, ncol = 3)
t_m <- t(mtrx)
print(t_m)

Output

     [,1] [,2] [,3]
[1,]  -1   -3   -5
[2,]  -7   -8    9
[3,]  11   13   15

Okay, now let’s find the determinant of this transposed matrix.

vt <- c(-1, -3, -5, -7, -8, 9, 11, 13, 15)
mtrx <- matrix(vt, nrow = 3, ncol = 3)
t_m <- t(mtrx)
print(det(t_m))

Output

[1] -360

That is it for det() function in R.

See also

Create an empty matrix in R

Convert Matrix to List

Convert Matrix to Vector

Leave a Comment