# How to Compute the Sign and Natural Logarithm of the Determinant of an Array in Python?

Let us see how to use the NumPy module in python to compute the sign and natural logarithm of the determinant of an array here.

Python numpy.linalg.slogdet() Function:
The numpy.linalg.slogdet() method in Python helps us to calculate the sign and natural logarithm of the determinant of an array.
If the determinant of an array is very tiny or very large, a call to slogdet may overflow or underflow. This approach is more resistant to such difficulties since it computes the logarithm of the determinant rather than the determinant of itself.

Syntax:

numpy.linalg.slogdet()

Parameters
x: It is the 2-Dimensional array given as input.

Return Value:
The logarithm of the determinant of an array is returned by this method

The method with a sign returns an integer representing the determinant’s sign. This is 1, 0, or -1 for a real matrix. This is a complex number having an absolute value 1 or 0 for a complex matrix. The method logdet returns the natural log of the determinant’s absolute value.

## Python Program to Compute the Sign and Natural Logarithm of the Determinant of an Array

Here the numpy.lib.linalg.slogdet() function is used to calculate the log of the determinant of an array and sign.
It returns the sign and logarithm of the determinant of an array. and logarithm.

Approach:

• Import numpy module using the import keyword
• Pass some random 2-dimensional array as an argument to the array() function of the numpy module to create an array.
• Store it in a variable
• Print the given array
• Apply the shape attribute on the given array to get the shape of the given array and print the result
• Apply ndim attribute on the given array to get the dimension of the given array and print the result
• Apply dtype attribute on the given array to get the datatype of all the elements of the
given array and print the result
• Pass the given array as an argument to the slogdet() function of linalg of the numpy module
to calculate the sign and logarithm of the determinant of the given input 2D array.
• Store them in two separate variables.
• Print the sign of the given 2D array
• Print the logarithm of the determinant of the given input 2D array.
• The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np

# Pass some random 2-dimensional array as an argument to the array() function
# of the numpy module to create an array.
# Store it in a variable
gvn_arry = np.array([[5, 12],[8, 10]])

# Print the given array
print("The given array is:")
print(gvn_arry)

# Apply shape attribute on the given array to get the shape of the given array and
# print the result
print("The Shape of the given array = ", gvn_arry.shape)

# Apply ndim attribute on the given array to get the dimension of the given array and
# print the result
print("The dimension of the given array = ",gvn_arry.ndim)

# Apply dtype attribute on the given array to get the datatype of all the elements of the
# given array and print the result
print("The datatype of the given array = ",gvn_arry.dtype)

# Pass the given array as an argument to the slogdet() function of linalg of the numpy module
# to calculate the sign and logarithm of determinant of the given input 2D array
# Store them in two separate variables.
arry_sign, det = (np.linalg.slogdet(gvn_arry))

# Print the sign of the given 2D array
print("The sign of the given 2D array = ", arry_sign)

# Print the logarithm of determinant of the given input 2D array
print("The logarithm determinant of the given input 2D array = ", det)

Output:

The given array is:
[[ 5 12]
[ 8 10]]
The Shape of the given array = (2, 2)
The dimension of the given array = 2
The datatype of the given array = int64
The sign of the given 2D array = -1.0
The logarithm determinant of the given input 2D array = 3.828641396489095

Another Example:

If we additionally wish to find the determinant of an array, we can use sign*exp(logdet). The numpy.lib.linalg.slogdet() method returns sign and logdet.

# Import numpy module using the import keyword
import numpy as np

# Pass some random 2-diemsional array as an argument to the array() function
# of the numpy module to create an array.
# Store it in a variable
gvn_arry = np.array([[4, 8],[3, 1]])

# Print the given array
print("The given array is:")
print(gvn_arry)

# Apply shape attribute on the given array to get the shape of the given array and
# print the result
print("The Shape of the given array = ", gvn_arry.shape)

# Apply ndim attribute on the given array to get the dimension of the given array and
# print the result
print("The dimension of the given array = ",gvn_arry.ndim)

# Apply dtype attribute on the given array to get the datatype of all the elements of the
# given array and print the result
print("The datatype of the given array = ",gvn_arry.dtype)

# Pass the given array as an argument to the slogdet() function of linalg of the numpy module
# to calculate the sign and logarithm of determinant of the given input 2D array
# Store them in two separate variables.
arry_sign, det = (np.linalg.slogdet(gvn_arry))

# Print the sign of the given 2D array
print("The sign of the given 2D array = ", arry_sign)

# Print the logarithm of determinant of the given input 2D array
print("The logarithm determinant of the given input 2D array = ", det)

# Print the determinant of the given array with the np.exp() function
print("The determinant of the given array with the np.exp() function = ", arry_sign*np.exp(det))

Output:

The given array is:
[[4 8]
[3 1]]
The Shape of the given array = (2, 2)
The dimension of the given array = 2
The datatype of the given array = int64
The sign of the given 2D array = -1.0
The logarithm determinant of the given input 2D array = 2.995732273553991
The determinant of the given array with the np.exp() function = -19.999999999999996