SciPy - det()Function



The determinant of a square matrix A is computed with the scipy.linalg.det() function, which returns a scalar number that encompasses the most important properties of the matrix. It is suited for both small and large matrices because it performs LU decomposition to compute the determinant in an efficient way.

The determinant is used to find out whether a matrix is invertible (det0), whether rows or columns are linearly dependent, and the scaling transformations. It is also widely used in stability analysis, eigenvalue problems, and linear system solutions.

A singular matrix is non-invertible since its determinant is always 0. The determinant for any size identity matrix is 1. The determinants of complex number matrices include both real and imaginary components. The determinants of unitary or orthogonal matrices are either +1 or -1.

Syntax

Following is the syntax of the SciPy det() method −

.det(a, overwrite_a=False, check_finite=True)

Parameters

This method accepts the following parameters −

  • a (array_like) − A square matrix whose determinant is to be calculated.

  • overwrite_a (bool, optional) − If True, the input matrix a may be overwritten during the computation for better performance. Default is False.

  • check_finite (bool, optional) − If True, it checks that the input matrix contains only finite numbers. Performance could be better if False, but invalid inputs - such as NaNs or infinities - might lead to unexpected behavior. True is the default.

Return Value

det: (float or complex) The determinant of the matrix a.

Example 1

The determinant of an identity matrix is always 1, because every diagonal item contributes to the product by 1 and every off-diagonal entry is 0.

In the code below we have created a 44 identity matrix and let's compute the determinant of A using det() method. Following is the code −

import numpy as np
import scipy.linalg
from scipy.linalg import det

A = np.eye(4)  # 4x4 Identity matrix
result = scipy.linalg.det(A)
print(result)

When we run the above program, it produces the following result

1.0

Example 2

In the below code we have created a 2x2 matrix with fractional values and we will use the det() function to compute the determinant of A..

import numpy as np
from scipy.linalg import det
A = np.array([[0.5, 1.2],
              [3.4, 2.1]])
result = det(A)
print(result)

Following is an output of the above code

-3.03

Example 3

A matrix with complex numbers is multi-dimensional in complex space, its determinant also consists of the real and imaginary parts.

In the below code we have created a 2x2 matrix with complex numbers and we will use det() function to calculate the det of A.

import numpy as np
from scipy.linalg import det
A = np.array([[2+3j, 4-1j],
              [1+2j, 3+4j]])
result = det(A)
print(result)

Output of the above code is as follows

(-12+9.999999999999998j)

Example 4

The determinant has properties such as matrix invertibility and rank deficiency with det=0 for singular matrices and if the det is 0 the matrix is non- invertible.

This example computes the determinant of a matrix to check for singularity.

import numpy as np
from scipy.linalg import det

# Define a singular matrix (det = 0)
A = np.array([[1, 2], [2, 4]])
determinant = det(A)

if determinant == 0:
    print("Matrix is singular (non-invertible).")
else:
    print("Matrix is invertible.")

Output of the above code is as follows

Matrix is singular (non-invertible).
scipy_linalg.htm
Advertisements