linalg.slogdet
Description
Compute the sign and (natural) logarithm of the determinant of an array.
If an array has a very small or very large determinant, a call to det may overflow or underflow. linalg.slogdet is more robust against such issues, because it computes the logarithm of the determinant rather than the determinant itself.
Mandatory Input Parameters
Parameter |
Type |
Description |
|---|---|---|
a |
array_like |
Input array, which has to be a square 2D array |
Optional Input Parameters
None
Return Value
Parameter |
Type |
Description |
|---|---|---|
sign |
ndarray |
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 (i.e., it is on the unit circle), or else 0. |
logdet |
ndarray |
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).
Examples
>>> import numpy as np
>>> # The determinant of a 2-D array [[a, b], [c, d]] is ad - bc.
>>> a = np.array([[1,2],[3,4]])
>>> a
array([[1, 2],
[3, 4]])
>>>
>>> (sign, logdet) = np.linalg.slogdet(a)
>>> (sign, logdet)
(-1.0, 0.6931471805599455)
>>>
>>> sign * np.exp(logdet)
-2.0000000000000004
>>>
>>>
>>> a = np.array([[[1,2],[3,4]], [[1,2],[2,1]], [[1,3],[3,1]]])
>>> a
array([[[1, 2],
[3, 4]],
[[1, 2],
[2, 1]],
[[1, 3],
[3, 1]]])
>>>
>>> (sign, logdet) = np.linalg.slogdet(a)
>>> (sign, logdet)
(array([-1., -1., -1.]), array([0.69314718, 1.09861229, 2.07944154]))
>>> sign * np.exp(logdet)
array([-2., -3., -8.])
>>>
>>>
>>> np.linalg.det(np.eye(500) * 0.1)
0.0
>>> np.linalg.slogdet(np.eye(500) * 0.1)
(1.0, -1151.2925464970251)
>>>