Function Repository Resource:

NumPyDet

Source Notebook

Compute the determinant of an array in Python using the NumPy linear algebra package

Contributed by: Wolfram Staff

ResourceFunction["NumPyDet"][array]

computes the determinant of array in Python using the package NumPy.

ResourceFunction["NumPyDet"][array,session]

uses the specified running ExternalSessionObject session.

Details and Options

ResourceFunction["NumPyDet"] is a wrapper function that calls the Python function numpy.linalg.det().
array must be a numeric square matrix or a tensor object representing a list of numeric square matrices.
ResourceFunction["NumPyDet"] accepts array in the form of a NumericArray object or an expression that can be converted to NumericArray.
ResourceFunction["NumPyDet"][array,session] avoids the overhead of opening a new Python session for every successive call.

Examples

Basic Examples (2) 

Compute the determinant of a matrix in NumPy:

In[1]:=
m = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
In[2]:=
ResourceFunction["NumPyDet"][m]
Out[2]=

Or using the built-in Wolfram Language function:

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

Scope (6) 

Compute the determinant of a real-valued matrix:

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

Sparse array:

In[6]:=
ResourceFunction["NumPyDet"][
 SparseArray[{{1, 1} -> 1, {2, 2} -> 2, {3, 3} -> 3, {1, 3} -> 4}]]
Out[6]=

NumericArray object:

In[7]:=
ResourceFunction["NumPyDet"][
 NumericArray[RandomReal[1, {3, 3}], "Real64"]]
Out[7]=

Complex-valued array:

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

A tensor representing a list of matrices:

In[9]:=
t = RandomReal[1, {3, 2, 2}]
Out[9]=
In[10]:=
ResourceFunction["NumPyDet"][%]
Out[10]=
In[11]:=
Normal[%]
Out[11]=

Compare with the built-in Det:

In[12]:=
Det /@ t
Out[12]=

Make several calls to NumPyDet in the same external session:

In[13]:=
session = StartExternalSession["Python"]
Out[13]=
In[14]:=
ResourceFunction["NumPyDet"][RandomReal[1, {2, 2}], session]
Out[14]=
In[15]:=
ResourceFunction["NumPyDet"][RandomReal[1, {3, 3}], session]
Out[15]=

End the session:

In[16]:=
DeleteObject[session]

Properties and Relations (3) 

For relatively large matrices, the determinant cannot be computed using machine arithmetic:

In[17]:=
rr = RandomReal[1, {1000, 1000}];
In[18]:=
ResourceFunction["NumPyDet"][rr]
Out[18]=

Use the resource function NumPySignLogDet instead:

In[19]:=
ResourceFunction["NumPySignLogDet"][rr]
Out[20]=
In[21]:=
%[[1]] Exp[%[[2]]]
Out[21]=

Or the built-in Det:

In[22]:=
Det[rr]
Out[22]=

Possible Issues (3) 

Automatic conversion of the input array to a NumericArray object can fail:

In[23]:=
a = SparseArray[{{i_, i_} -> -2., {i_, j_} /; Abs[i - j] == 1 -> 1.}, {10, 10}]
Out[23]=
In[24]:=
ResourceFunction["NumPyDet"][a]
Out[24]=

Convert the array to a NumericArray before passing it to NumPyDet:

In[25]:=
ResourceFunction["NumPyDet"][NumericArray[a, "Real32"]]
Out[25]=

A call to NumPyDet on an arbitrary precision array fails:

In[26]:=
m = N[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, 20]
Out[26]=
In[27]:=
ResourceFunction["NumPyDet"][m]
Out[27]=

Convert the array to a NumericArray before passing it to NumPyDet:

In[28]:=
ResourceFunction["NumPyDet"][NumericArray[m, "Real64"]]
Out[28]=

Or use the machine-precision array:

In[29]:=
ResourceFunction["NumPyDet"][N[m]]
Out[29]=

NumPyDet does not issue a warning when precision is lost:

In[30]:=
m = HilbertMatrix[30];
In[31]:=
ResourceFunction["NumPyDet"][NumericArray[m, "Real64"]]
Out[31]=

Compare with the built-in Det computed with machine precision:

In[32]:=
Det[N[m]]
Out[32]=

Obtain a more accurate result by computing the determinant with a higher precision using the built-in Det:

In[33]:=
Det[N[m, 40]]
Out[33]=

Verify the result by using still higher precision:

In[34]:=
Det[N[m, 100]]
Out[34]=

Version History

  • 1.0.0 – 07 December 2020

License Information