Numpy linalg slogdet() Function in Python

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.

np.linalg.slogdet()

Numpy linalg slogdet() function is used to calculate a sign and (natural) logarithm of an array’s determinant.

Syntax

numpy.linalg.slogdet(array)

Parameters

The slogdet() function takes a single parameter,

  1. array: This is a 2D array, and it has to be square. 

Return Value

The slogdet() function returns two values,

sign: 

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.

logdet:

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)“.

Programming example

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)

Output

[[ 3  4  5]
 [ 7  8  6]
 [10 11  2]]
Sign is :  1.0
Logdet is : 2.944438979166441

Explanation

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)

Output

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]

Explanation

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 ].

See also

Numpy linalg solve()

Numpy linalg svd()

Numpy linalg qr()

Numpy linalg cholesky()

Numpy linalg matrix_power()

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.