If most lists have too little or too little index, then the calling det() function can overflow or underflow. This process ( slogdet() ) is much more robust than such problems because it includes a shortcut logarithm rather than the shortcut itself.
Numpy linalg slogdet() function is used to calculate a sign and (natural) logarithm of an array’s determinant.
The slogdet() function takes a single parameter,
- array: This is a 2D array, and it has to be square.
The slogdet() function returns two values,
A number represents the sign of the determinant. For a real matrix, this is 1, 0, or -1. For a complex matrix, this is a complex number with absolute value 1 (i.e., it is on the unit circle), or else 0.
This is the natural log of the absolute value of the determinant.
If the determinant is zero, then `sign` will be 0, and `logdet` will be
-Inf. In all cases, the determinant is equal to “sign * np.exp(logdet)“.
Program to calculate the determinant of one 2D array
# Program to calculate determinant of one 2D array import numpy as np # creating a 2D array arr = np.array([[3, 4, 5], [7, 8, 6], [10, 11, 2]]) # printing the array print(arr) # calculating the slogdet (sign, logdet) = np.linalg.slogdet(arr) # Printing those values print("Sign is : ", sign) print("Logdet is :", logdet)
[[ 3 4 5] [ 7 8 6] [10 11 2]] Sign is : 1.0 Logdet is : 2.944438979166441
In this program, we have created a square matrix of size 3×3, and then we have printed it.
Then we have called slogdet() to get the sign and logdet of the array.
We can see that we got sign 1.
Program to compute log-determinants for a stack of matrices.
See the following code.
# Computing log-determinants for a stack of matrices import numpy as np # creating the stack matrices arr = np.array([[[2, 1], [3, 4]], [[3, 4], [5, 6]], [[7, 8], [9, 10]]]) # printing the values print("The array is:\n", arr) print("Shape of the array is: ", arr.shape) # Calculating the slogdet (sign, logdet) = np.linalg.slogdet(arr) # Printing those values print("Sign is : ", sign) print("Logdet is :", logdet)
The array is: [[[ 2 1] [ 3 4]] [[ 3 4] [ 5 6]] [[ 7 8] [ 9 10]]] Shape of the array is: (3, 2, 2) Sign is : [ 1. -1. -1.] Logdet is : [1.60943791 0.69314718 0.69314718]
In this program, we have created a square matrix of size 3×2, and then we have printed it.
Then we have called the slogdet() function to get the sign and logdet of the array.
We can see that we got a sign [ 1. -1. -1 ].