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


To Compute the sign and natural logarithm of the determinant of an array, use the numpy.linalg.slogdet() method in Python. The 1st parameter, s is an input array, has to be a square 2-D array.

The method, with sign returns a number representing 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, or else 0. The method, with logdet returns 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).

Steps

At first, import the required libraries −

import numpy as np

Create an array −

arr = np.array([[ 1, 2],
   [ 3, 4]])

Display the array −

print("Our Array...\n",arr)

Check the Dimensions −

print("\nDimensions of our Array...\n",arr.ndim)

Get the Datatype −

print("\nDatatype of our Array object...\n",arr.dtype)

Get the Shape −

print("\nShape of our Array object...\n",arr.shape)

The determinant of an array in linear algebra −

print("\nDeterminant...\n",np.linalg.det(arr))

To compute the sign and natural logarithm of the determinant of an array, use the numpy.linalg.slogdet() method in Python. 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) −

(sign, logdet) = np.linalg.slogdet(arr)
print("\nResult....\n",(sign, logdet))

Example

import numpy as np

# Create an array
arr = np.array([[ 1, 2],
   [ 3, 4]])

# Display the array
print("Our Array...\n",arr)

# Check the Dimensions
print("\nDimensions of our Array...\n",arr.ndim)

# Get the Datatype
print("\nDatatype of our Array object...\n",arr.dtype)

# Get the Shape
print("\nShape of our Array object...\n",arr.shape)

# The determinant of an array in linear algebra
print("\nDeterminant...\n",np.linalg.det(arr))

# To Compute the sign and natural logarithm of the determinant of an array, use the numpy.linalg.slogdet() method in Python
(sign, logdet) = np.linalg.slogdet(arr)
print("\nResult....\n",(sign, logdet))

Output

Our Array...
[[1 2]
[3 4]]

Dimensions of our Array...
2

Datatype of our Array object...
int64

Shape of our Array object...
(2, 2)

Determinant...
-2.0000000000000004

Result....
(-1.0, 0.6931471805599455)

Updated on: 02-Mar-2022

114 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements