0

I'm trying to convert some algorithms from Matlab to Python 3.8. In the algorithm I'm trying to inverse some matrix, the result is that Matlab inverse the matrix as it should do but Python (using numpy.linalg) says that it cannot inverse singular matrix. After some debugging, we found out that in Matlab the determinant of the matrix was 5.79913020654461e-35 but in python, it was 0. Thanks a lot!

Im using Python 3.8 with numpy version 1.20.0 and Matlab 2017a

This is the data:

My matrix:

[[3.0322662511118286, 3.645196880210743, 1.3326781661192055, -4.925254309001175],
[3.645196880210743, 4.382022947889959, 1.6020606012651588, -5.920826258432845],
[1.3326781661192055, 1.6020606012651588, 0.5857108009982133, -2.164644637608797],
[-4.925254309001175, -5.920826258432845, -2.164644637608797, 8.]]

My Python script:

import numpy as np

np.set_printoptions(20)  # Matrix dtype is float 64, meaning that there will be up to 15 digits after decimal point
matrix = np.array([[3.0322662511118286, 3.645196880210743, 1.3326781661192055, -4.925254309001175],
                   [3.645196880210743, 4.382022947889959, 1.6020606012651588, -5.920826258432845],
                   [1.3326781661192055, 1.6020606012651588, 0.5857108009982133, -2.164644637608797],
                   [-4.925254309001175, -5.920826258432845, -2.164644637608797, 8.]])
print(f"Matrix is: {matrix}")
print(f"dtype is: {matrix.dtype}")
print(f"Matrix det is: {np.linalg.det(matrix)}")
try:
    print(f"Inverse matrix is: {np.linalg.inv(matrix)}")
except np.linalg.LinAlgError as e:
    print(f"Failed to inverse matrix. Error code: '{e}")

Output of Python script:

Matrix is: [[ 3.0322662511118286  3.645196880210743   1.3326781661192055
  -4.925254309001175 ]
 [ 3.645196880210743   4.382022947889959   1.6020606012651588
  -5.920826258432845 ]
 [ 1.3326781661192055  1.6020606012651588  0.5857108009982133
  -2.164644637608797 ]
 [-4.925254309001175  -5.920826258432845  -2.164644637608797
   8.                ]]
dtype is: float64
Matrix det is: 0.0
Failed to inverse matrix. Error code: 'Singular matrix

Matlab script + output:

>> format longg
>> matrix = 
3.0322662511118286      3.645196880210743       1.3326781661192055      -4.925254309001175
3.645196880210743       4.382022947889959       1.6020606012651588      -5.920826258432845
1.3326781661192055      1.6020606012651588      .5857108009982133       -2.164644637608797
-4.925254309001175      -5.920826258432845      -2.164644637608797      8.
>> det (matrix)
ans = 5.79913020654461e-35
>> inv (matrix)
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.480881e-17.
ans =
463831092542248     557587200641207     203852870968935     753391506264877
557587200641207     670315847520521     245068621497740     905696113927055
203852870968935     245068621497740     89607103131709.8    331125436963685
753391506264877     905696113927055     331125436963685     1.2237353746994e+15

Alredy tried to use other data types but with no success

Kfir Levy
  • 1
  • 1
  • 3
    Even the Matlab code warns you that the results may be meaningless. A determinant of 10**-35 when your elements are in the 10**0 range is essentially zero. Doing a couple of operations in a different order could easily produce this. Face it -- this matrix cannot be inverted. – Tim Roberts Apr 12 '23 at 18:47
  • FWIW, with numpy 1.21.5, I get `-5.825914864122132e-35` for `np.linalg.det(matrix)`. What libraries is your Numpy installation using for LA computations (`np.show_config()`)? – Brian61354270 Apr 12 '23 at 18:48
  • You should try Singular Value Decomposition. It will do its best to solve the system even if one of the eigenvalues is close to zero. – duffymo Apr 12 '23 at 18:56
  • This seems to be a repost of https://stackoverflow.com/questions/75933596/python-vs-matlab-why-numpy-is-not-accurate – James Tursa Apr 12 '23 at 20:10
  • 1
    And you still haven't answered the question of why you are trying to invert this matrix. What do you hope to do with this (obviously garbage) inverse downstream in your code? – James Tursa Apr 12 '23 at 20:14
  • Please don't repost the same question. If you address the issues in the other post, we'll try to get it opened again. But as it stands, we can only tell you "you're doing it wrong", which is not a terribly useful answer, and was already given to you in comments to the other post. – Cris Luengo Apr 12 '23 at 20:30
  • I can't detail what exactly my use of the matrix is in the rest of the code because of the policy of the company I work for. In any case, it shouldn't change. I have a Matlab script that works great and does what it's supposed to do (among other things it performs the conversion of the singular matrix you saw here) and for some reason when I copy it to Python, it doesn't work because of this conversion. – Kfir Levy Apr 15 '23 at 11:10
  • In the previous post, I was missing the numerical information that I had attached, but unfortunately, no one didn't go over the post and no one approved of opening it, so in order to solve this problem, I published a new post with more information and parameters. I don't think it's a good idea to directly slander me and my code, calling calculations garbage for no reason is just plain shaming. If you want to help and treat my problem in a matter-of-fact manner - thank you very much. Otherwise, just slandering is a waste of both your time writing these comments and my time reading them. – Kfir Levy Apr 15 '23 at 11:10

1 Answers1

3

Both MATLAB and Python are telling you that the inverse is garbage. So why are you trying to calculate it? What would you be using this garbage result for downstream in your code? You need to rethink what you are doing in both MATLAB and Python with this, because right now neither makes sense. The following shows that even though MATLAB gave you the best answer it could, you should heed the warning that MATLAB thinks the answer is garbage because it is. The inverse times the original isn't very close to the identity matrix.

enter image description here

James Tursa
  • 2,242
  • 8
  • 9