Function Repository Resource:

SignLogDet

Source Notebook

Compute the sign and natural logarithm of the determinant of a matrix

Contributed by: Wolfram Staff

ResourceFunction["SignLogDet"][m]

gives the sign and natural logarithm of the determinant of the square matrix m.

Details

ResourceFunction["SignLogDet"] returns a result in the form {sign,ldet}.
For a real matrix, the returned value sign is -1, 0 or 1, depending on whether the determinant is negative, zero or positive; for a complex matrix, sign is a complex number with an absolute value of 1 (i.e. it is on the unit circle) or else 0.
The returned value ldet is the natural logarithm of the absolute value of the determinant.
If the determinant is zero, then sign will be 0 and ldet will be -Infinity.
The determinant can be computed as sign Exp[ldet].

Examples

Basic Examples (3) 

Compute the sign and natural logarithm of the determinant of a matrix:

In[1]:=
m = {{1., 2.}, {3., 4.}};
In[2]:=
{sign, logdet} = ResourceFunction["SignLogDet"][m]
Out[2]=

The determinant:

In[3]:=
sign*Exp[logdet]
Out[3]=

Or using the built-in function Det:

In[4]:=
Det[m]
Out[4]=

Scope (6) 

Compute the sign and natural logarithm of the determinant of a real-valued matrix:

In[5]:=
RandomReal[1, {3, 3}]
Out[5]=
In[6]:=
ResourceFunction["SignLogDet"][%]
Out[6]=

Complex-valued array:

In[7]:=
ResourceFunction["SignLogDet"][RandomComplex[1 + I, {3, 3}]]
Out[7]=

A SparseArray object:

In[8]:=
ResourceFunction["SignLogDet"][
 SparseArray[{{1, 1} -> 1., {2, 2} -> 2., {3, 3} -> 3., {1, 3} -> 4.}]]
Out[8]=

A SymmetrizedArray object:

In[9]:=
ResourceFunction["SignLogDet"][
 SymmetrizedArray[{{1, 1} -> 2., {2, 3} -> -3, {3, 1} -> 1.}, {3, 3}, Symmetric[{1, 2}]]]
Out[9]=

Use a singular matrix:

In[10]:=
m = {{1., 2., 3.}, {1., 5., 6.}, {1., 8., 9.}};
In[11]:=
ResourceFunction["SignLogDet"][m]
Out[11]=

Use a large matrix:

In[12]:=
rr = RandomReal[1, {1000, 1000}];
In[13]:=
ResourceFunction["SignLogDet"][rr]
Out[13]=

Compute the determinant:

In[14]:=
%[[1]] Exp[%[[2]]]
Out[14]=

Properties and Relations (4) 

For real matrices, SignLogDet returns the signs as -1 or 1, depending on whether the determinant is negative or positive:

In[15]:=
ResourceFunction["SignLogDet"][RandomReal[2, {3, 3}]]
Out[15]=

The sign is zero if the determinant is 0:

In[16]:=
Join[{#[[1]]}, #] &@RandomReal[1, {2, 3}]
Out[16]=
In[17]:=
ResourceFunction["SignLogDet"][%]
Out[17]=

For complex matrices, the sign is a complex number with magnitude 1:

In[18]:=
ResourceFunction["SignLogDet"][RandomComplex[1 + I, {5, 5}]]
Out[18]=
In[19]:=
Abs[First[%]]
Out[19]=

Or complex zero for singular matrices:

In[20]:=
Join[{#[[1]]}, #] &@RandomComplex[1, {2, 3}]
Out[20]=
In[21]:=
ResourceFunction["SignLogDet"][%]
Out[21]=

SignLogDet can give more accurate results than Det for small determinants:

In[22]:=
m = IdentityMatrix[500]/10;
In[23]:=
Det[N[m]]
Out[23]=
In[24]:=
ResourceFunction["SignLogDet"][m // N]
Out[24]=
In[25]:=
{#[[1]] Exp[#[[2]]] &@SetPrecision[%, 20], N[Det[m], 20]}
Out[25]=

SignLogDet may give inaccurate results with machine-precision computation:

In[26]:=
m = HilbertMatrix[30];
In[27]:=
ResourceFunction["SignLogDet"][N[m]]
Out[27]=
In[28]:=
{#[[1]] Exp[#[[2]]] &@SetPrecision[%, 20], N[Det[m], 20]}
Out[28]=

Possible Issues (1) 

The logarithm value returned by SignLogDet may be too small to compute the determinant with machine precision:

In[29]:=
m = IdentityMatrix[200]/100;
In[30]:=
ResourceFunction["SignLogDet"][m // N]
Out[30]=
In[31]:=
%[[1]]*Exp[%[[2]]]
Out[31]=

Version History

  • 1.0.0 – 14 September 2021

License Information