Numpy linalg.slogdet – Compute logarithmic determinant of arrays

Numpy Linalg Slogdet Computing The Logarithmic Determinant Of An Array

Numpy contains a multitude of functions for easier mathematical calculations. One such function is the numpy linalg.slogdet() which is used to compute the sine and natural logarithm of the determinant of an array.

In multivariate statistics, the log determinant of a matrix is useful in a high-dimensional space. It is used to determine the convexity or concavity of a function.

If the determinant of a matrix is very large or extremely small, then the det() function may cause overflow or underflow for matrices of dimensions 1000×1000 or an even larger one. In these cases, the logarithm of the determinant, that is, the slogdet() function becomes useful. Hence, this method is more resistant to such problems.

This is the central mathematical concept of the function linalg. slogdet(). This function is mainly used in logistic regression problems.

Understanding the numpy linalg.slogdet() function

The numpy linalg.slogdet() function is a predefined matrix function that calculates the log of the determinant of a square, two-dimensional input matrix.

Mathematically, the log determinant function of a matrix is defined as:

Log Determinant Of Matrix X
Log Determinant Of Matrix X

It can also be expressed in terms of the real and positive eigenvalues of matrix X. One more important feature of this function is that it doesn’t depend on the eigenvectors of a matrix. This is a great writeup on the log determinant of matrices, if you’re new to the concept.

Also read: How to compute the eigenvalues and right eigenvectors of a square matrix.

Syntax of the numpy linalg.slogdet() function

The syntax of the numpy linalg.slogdet() function is given below:

linalg.slogdet(A)

The parameters of the numpy linalg.slogdet() functions are:

A: (array_like): The input array must be square and 2 dimensional in nature.

This function returns two values:

  • Sign : (array_like): One of the numbers among -1, 0, or 1 in the case of a real input matrix. This variable represents the sign of the determinant of the input matrix. For complex matrices, this variable has a complex value whose absolute value is 1 or else 0.
  • LOGDET : (array_like) : The logarithm of the determinant of the input matrix.

If the input matrix is non-singular or non-invertible, that is, if its determinant is 0 , the sign = 0 and LOGDET= -Inf.

The determinant of the matrix can be found by using the formula, Sign * np.exp(LOGDET)

Implementing the Numpy.slogdet() function with examples

In order to use the slogdet() function, you must have numpy installed in your system. If you don’t already have it, run the following command in your command prompt in administrator mode:

pip install numpy

Let us look at some simple examples to gain a more practical knowledge about how to implement the function in our code:

Example 1: Calculating the sign and natural logarithm of the determinant of a square matrix

This example demonstrates the simple working of the function on a predefined square matrix.

#importing required modules
import numpy as np
#the predefined matrix
A=([[2,6],[10,14]])
#display the original matrix
print("The original matrix is=",A)
#using the slogdet function
(Sign, LOGDET) = np.linalg.slogdet(A)
#displaying the result
print("The sign of the logarithm of the determinant of the matrix is=",Sign)
print("the log of the determinant of the matrix is=",LOGDET)
#calculating the determinant of the matrix
DET = Sign * np.exp(LOGDET)
#displaying the determinant of the matrix
print("The determinant of the matrix is=",DET)

The output of the above code would be:

The original matrix is= [[2, 6], [10, 14]]
The sign of the logarithm of the determinant of the matrix is= -1.0
the log of the determinant of the matrix is= 3.465735902799727
The determinant of the matrix is= -32.000000000000014
Example 1 For Slogdet Function
Example 1 For Slogdet Function

Example 2: Taking a 3X3 matrix and finding the log of its determinant.

Let us modify the code for the previous example to accommodate a 3X3 matrix instead of a 2X2 matrix. Also, let us display its various properties.

#importing required modules
import numpy as np
#the predefined matrix
A= np.array([[68,65,62],[59,56,53],[50,47,44]])
#display the original matrix
print("The original matrix is=",A)
#displaying the properties of the matrix
print("Shape of the matrix is : ",A.shape)#shape
print("The dimension of the matrix is : ",A.ndim)#dimensions of the matrix
print("Datatype of the matrix is : ",A.dtype)
#using the slogdet function
(Sign, LOGDET) = np.linalg.slogdet(A)
#displaying the result
print("The sign of the logarithm of the determinant of the matrix is=",Sign)
print("the log of the determinant of the matrix is=",LOGDET)
#calculating the determinant of the matrix
DET = Sign * np.exp(LOGDET)
#displaying the determinant of the matrix
print("The determinant of the matrix is=",DET)

The output of the above code is:

The original matrix is= [[68 65 62]
 [59 56 53]
 [50 47 44]]
Shape of the matrix is :  (3, 3)
The dimension of the matrix is :  2
Datatype of the matrix is :  int64
The sign of the logarithm of the determinant of the matrix is= 1.0
the log of the determinant of the matrix is= -28.588933439753163
The determinant of the matrix is= 3.836930773104512e-13
Example 2 For Slogdet Function Log Of A 3X3 Matrix
Example 2 For Slogdet Function-Log Of A 3X3 Matrix .

Also read: Numpy linalg.tensorinv( ): Computing Inverse of an N-Dimensional Array.

Summary:

This tutorial encapsulates the working of the numpy function, linalg. slogdet() which is useful in calculating the sign and natural logarithm of the determinant of a two-dimensional matrix. This function is robust against overflow and underflow of determinant values and is an integral tool for mathematical modeling problems. To know more about the slogdet() function, visit the official documentation.