Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Compute the sign and natural logarithm of the determinant of an array in Python using the NumPy linear algebra package
ResourceFunction["NumPySignLogDet"][array] computes the sign and natural logarithm of the determinant of array in Python using the package NumPy. | |
ResourceFunction["NumPySignLogDet"][array,session] uses the specified running ExternalSessionObject session. |
Compute the sign and natural logarithm of the determinant of a matrix in NumPy:
In[1]:= |
![]() |
In[2]:= |
![]() |
Out[2]= |
![]() |
The determinant:
In[3]:= |
![]() |
Out[3]= |
![]() |
Or using the built-in Wolfram Language function:
In[4]:= |
![]() |
Out[4]= |
![]() |
Compute the sign and natural logarithm of the determinant of a real-valued matrix:
In[5]:= |
![]() |
Out[5]= |
![]() |
In[6]:= |
![]() |
Out[6]= |
![]() |
Sparse array:
In[7]:= |
![]() |
Out[7]= |
![]() |
NumericArray object:
In[8]:= |
![]() |
Out[8]= |
![]() |
Complex-valued array:
In[9]:= |
![]() |
Out[9]= |
![]() |
A tensor representing a list of matrices:
In[10]:= |
![]() |
Out[10]= |
![]() |
In[11]:= |
![]() |
Out[11]= |
![]() |
In[12]:= |
![]() |
Out[12]= |
![]() |
In[13]:= |
![]() |
Out[13]= |
![]() |
Compare with the built-in Det:
In[14]:= |
![]() |
Out[14]= |
![]() |
Use a singular matrix:
In[15]:= |
![]() |
Out[15]= |
![]() |
In[16]:= |
![]() |
Out[16]= |
![]() |
In[17]:= |
![]() |
Out[17]= |
![]() |
Compare:
In[18]:= |
![]() |
Out[18]= |
![]() |
Large matrix:
In[19]:= |
![]() |
In[20]:= |
![]() |
Out[20]= |
![]() |
In[21]:= |
![]() |
Out[21]= |
![]() |
Compare:
In[22]:= |
![]() |
Out[22]= |
![]() |
Make several calls to NumPySignLogDet in the same external session:
In[23]:= |
![]() |
Out[23]= |
![]() |
In[24]:= |
![]() |
Out[24]= |
![]() |
In[25]:= |
![]() |
Out[25]= |
![]() |
End the session:
In[26]:= |
![]() |
For real matrices, NumPySignLogDet returns the signs as -1 or 1 depending on whether the determinant is negative or positive:
In[27]:= |
![]() |
Out[27]= |
![]() |
In[28]:= |
![]() |
Out[28]= |
![]() |
The sign is zero if the determinant is 0:
In[29]:= |
![]() |
Out[29]= |
![]() |
In[30]:= |
![]() |
Out[30]= |
![]() |
For complex matrices, the signs are complex numbers with absolute values of 1:
In[31]:= |
![]() |
Out[31]= |
![]() |
In[32]:= |
![]() |
Out[32]= |
![]() |
Or complex zero for singular matrices:
In[33]:= |
![]() |
Out[33]= |
![]() |
In[34]:= |
![]() |
Out[34]= |
![]() |
NumPySignLogDet can give more accurate results than the resource function NumPyDet for small determinants:
In[35]:= |
![]() |
In[36]:= |
![]() |
Out[36]= |
![]() |
In[37]:= |
![]() |
Out[37]= |
![]() |
In[38]:= |
![]() |
Out[38]= |
![]() |
And determinants with large absolute values:
In[39]:= |
![]() |
In[40]:= |
![]() |
Out[40]= |
![]() |
In[41]:= |
![]() |
Out[41]= |
![]() |
In[42]:= |
![]() |
Out[42]= |
![]() |
NumPySignLogDet may still give inaccurate results since it computes with machine precision:
In[43]:= |
![]() |
In[44]:= |
![]() |
Out[44]= |
![]() |
In[45]:= |
![]() |
Out[45]= |
![]() |
Automatic conversion of the input array to a NumericArray object can fail:
In[46]:= |
![]() |
Out[46]= |
![]() |
In[47]:= |
![]() |
Out[47]= |
![]() |
Convert the array to a NumericArray before passing it to NumPySignLogDet:
In[48]:= |
![]() |
Out[48]= |
![]() |
A call to NumPySignLogDet on an arbitrary precision array fails:
In[49]:= |
![]() |
Out[49]= |
![]() |
In[50]:= |
![]() |
Out[50]= |
![]() |
Convert the array to a NumericArray before passing it to NumPySignLogDet:
In[51]:= |
![]() |
Out[51]= |
![]() |
Or use the machine-precision array:
In[52]:= |
![]() |
Out[52]= |
![]() |
The logarithm value returned by NumPySignLogDet may be too small to compute the determinant with machine precision:
In[53]:= |
![]() |
In[54]:= |
![]() |
Out[54]= |
![]() |
In[55]:= |
![]() |
Out[55]= |
![]() |
Raise precision to get an estimate of the determinant:
In[56]:= |
![]() |
Out[56]= |
![]() |
In[57]:= |
![]() |
Out[57]= |
![]() |
Compare:
In[58]:= |
![]() |
Out[58]= |
![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License