Open In App

Compute the sign and natural logarithm of the determinant of an array in Python

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will cover how to compute the sign and natural logarithm of the determinant of an array in Python using NumPy.

numpy.linalg.slogdet() method

The numpy.linalg.slogdet() method provides us to compute the sign and natural logarithm of the determinant of an array in Python. A call to slogdet may overflow or underflow if the determinant of an array is very small or very large. Because it calculates the logarithm of the determinant rather than the determinant of itself, this procedure is more resistant to such problems.

Syntax: numpy.linalg.slogdet()

Parameters:

  • a: array like object. The input array must be a two-dimensional array.

Return: logarithm of the determinant of an array.

Example 1:

In this example, a 2-d array is created using numpy.array() method and numpy.lib.linalg.slogdet() is used to compute the log of the determinant of the array and sign. The method returns the sign and logarithm of the determinant of the array. The shape, datatype, and dimensions of the array can be found by .shape , .dtype, and .ndim attributes.

Python3




# import packages
import numpy as np
  
# Creating an array
array = np.array([[10,20],[30,40]])
print(array)
  
# shape of the array is
print("Shape of the array is : ",array.shape)
  
# dimension of the array
print("The dimension of the array is : ",array.ndim)
  
# Datatype of the array
print("Datatype of our Array is : ",array.dtype)
  
# computing sign and natural logarithm of the 
# determinant of the array
sign, determinant= (np.linalg.slogdet(array))
  
print('sign of the array is : '+str(sign))
  
print('logarithm of the determinant of the array is : '+ str(determinant))


Output:

[[10 20]

 [30 40]]

Shape of the array is :  (2, 2)

The dimension of the array is :  2

Datatype of our Array is :  int64

sign of the array is : -1.0

logarithm of the determinant of the array is : 5.298317366548037

Example 2:

If we also want to compute the determinant of the array we can use sign*exp(logdet) and find it. sign and logdet are returned by the numpy.lib.linalg.slogdet() method.

Python3




import numpy as np
  
# Creating an array
array = np.array([[2,3],[5,6]])
print(array)
  
# shape of the array is
print("Shape of the array is : ",array.shape)
  
# dimension of the array
print("The dimension of the array is : ",array.ndim)
  
# Datatype of the array
print("Datatype of our Array is : ",array.dtype)
  
# computing sign and natural logarithm of the
# determinant of the array
sign, determinant= (np.linalg.slogdet(array))
  
print('sign of the array is : '+str(sign))
  
print('logarithm of the determinant of the array is : '+ str(determinant))
  
print('computing the determinant of the array using np.exp(): '
      + str(sign*np.exp(determinant)))


Output:

[[2 3]

 [5 6]]

Shape of the array is :  (2, 2)

The dimension of the array is :  2

Datatype of our Array is :  int32

sign of the array is : -1.0

logarithm of the determinant of the array is : 1.0986122886681091

computing the determinant of the array using np.exp(): -2.9999999999999982



Last Updated : 22 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads