Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Compute the determinant of an array in Python using the NumPy linear algebra package
| ResourceFunction["NumPyDet"][array] computes the determinant of array in Python using the package NumPy. | |
| ResourceFunction["NumPyDet"][array,session] uses the specified running ExternalSessionObject session. | 
Compute the determinant of a matrix in NumPy:
| In[1]:= |   | 
| In[2]:= | ![ResourceFunction["NumPyDet"][m]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/16548ecdef58bfb0.png)  | 
| Out[2]= |   | 
Or using the built-in Wolfram Language function:
| In[3]:= | ![Det[m]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/5400a7d6fb022bf7.png)  | 
| Out[3]= |   | 
Compute the determinant of a real-valued matrix:
| In[4]:= | ![RandomReal[1, {3, 3}]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/60957f419eb30ea4.png)  | 
| Out[4]= |   | 
| In[5]:= | ![ResourceFunction["NumPyDet"][%]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/67a40553c6e6ff22.png)  | 
| Out[5]= |   | 
Sparse array:
| In[6]:= | ![ResourceFunction["NumPyDet"][
 SparseArray[{{1, 1} -> 1, {2, 2} -> 2, {3, 3} -> 3, {1, 3} -> 4}]]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/7be38415479e5b51.png)  | 
| Out[6]= |   | 
NumericArray object:
| In[7]:= | ![ResourceFunction["NumPyDet"][
 NumericArray[RandomReal[1, {3, 3}], "Real64"]]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/6995efaf934b7f21.png)  | 
| Out[7]= |   | 
Complex-valued array:
| In[8]:= | ![ResourceFunction["NumPyDet"][RandomComplex[1 + I, {3, 3}]]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/3767091b5483f131.png)  | 
| Out[8]= |   | 
A tensor representing a list of matrices:
| In[9]:= | ![t = RandomReal[1, {3, 2, 2}]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/3a58b045e16b6094.png)  | 
| Out[9]= |   | 
| In[10]:= | ![ResourceFunction["NumPyDet"][%]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/05c709920213cf4d.png)  | 
| Out[10]= |   | 
| In[11]:= | ![Normal[%]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/2dfdaff604568f82.png)  | 
| Out[11]= |   | 
Compare with the built-in Det:
| In[12]:= |   | 
| Out[12]= |   | 
Make several calls to NumPyDet in the same external session:
| In[13]:= | ![session = StartExternalSession["Python"]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/542eb62aa8b5ec04.png)  | 
| Out[13]= |   | 
| In[14]:= | ![ResourceFunction["NumPyDet"][RandomReal[1, {2, 2}], session]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/00db28e9aa105c27.png)  | 
| Out[14]= |   | 
| In[15]:= | ![ResourceFunction["NumPyDet"][RandomReal[1, {3, 3}], session]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/5befb82b69cd4b17.png)  | 
| Out[15]= |   | 
End the session:
| In[16]:= | ![DeleteObject[session]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/40b6415c7cbb78a9.png)  | 
For relatively large matrices, the determinant cannot be computed using machine arithmetic:
| In[17]:= | ![rr = RandomReal[1, {1000, 1000}];](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/03bf70326513ae40.png)  | 
| In[18]:= | ![ResourceFunction["NumPyDet"][rr]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/564da5dae1d1d71a.png)  | 
| Out[18]= |   | 
Use the resource function NumPySignLogDet instead:
| In[19]:= | ![ResourceFunction["NumPySignLogDet"][rr]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/4216d58793c67301.png)  | 
| Out[20]= |   | 
| In[21]:= | ![%[[1]] Exp[%[[2]]]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/01ae695ef15238a6.png)  | 
| Out[21]= |   | 
Or the built-in Det:
| In[22]:= | ![Det[rr]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/02eac77a0fa75326.png)  | 
| Out[22]= |   | 
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}]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/722d409ac3fa31c5.png)  | 
| Out[23]= |   | 
| In[24]:= | ![ResourceFunction["NumPyDet"][a]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/693f6a187bcba740.png)  | 
| Out[24]= |   | 
Convert the array to a NumericArray before passing it to NumPyDet:
| In[25]:= | ![ResourceFunction["NumPyDet"][NumericArray[a, "Real32"]]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/63997fcdfcf7b73f.png)  | 
| 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]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/05866afcb2cb6d03.png)  | 
| Out[26]= |   | 
| In[27]:= | ![ResourceFunction["NumPyDet"][m]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/5d9612e880fb4ef7.png)  | 
| Out[27]= |   | 
Convert the array to a NumericArray before passing it to NumPyDet:
| In[28]:= | ![ResourceFunction["NumPyDet"][NumericArray[m, "Real64"]]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/517edf55f2563199.png)  | 
| Out[28]= |   | 
Or use the machine-precision array:
| In[29]:= | ![ResourceFunction["NumPyDet"][N[m]]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/22fba2c95c12b6bb.png)  | 
| Out[29]= |   | 
NumPyDet does not issue a warning when precision is lost:
| In[30]:= | ![m = HilbertMatrix[30];](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/62455ad5283c992c.png)  | 
| In[31]:= | ![ResourceFunction["NumPyDet"][NumericArray[m, "Real64"]]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/44b6c47ac9c44c91.png)  | 
| Out[31]= |   | 
Compare with the built-in Det computed with machine precision:
| In[32]:= | ![Det[N[m]]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/061053bf5f85ae3e.png)  | 
| 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]]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/714a303026236b75.png)  | 
| Out[33]= |   | 
Verify the result by using still higher precision:
| In[34]:= | ![Det[N[m, 100]]](https://www.wolframcloud.com/obj/resourcesystem/images/076/0762deb8-4049-4c1d-a9d5-21a7c8e44a6e/40d9d1344a54088c.png)  | 
| Out[34]= |   | 
This work is licensed under a Creative Commons Attribution 4.0 International License