1

I have to read multiple data from csv files, and when I want to invert matrix from csv data, I get this:

numpy.linalg.linalg.LinAlgError: singular matrix

and the process get stuck on this section :

J = np.mat([dtdx,dtdy,dtdz]).transpose()
dd = np.mat(ttcal-tt)
dm = (scipy.linalg.inv(J.transpose()*J))*((J.transpose())*(dd.transpose()))

and data from "J" like this :

[[-6.81477651e-03 -7.90320450e-03  6.50533437e-05]
 [-6.71080644e-03 -6.00135428e-03  6.50533437e-05]]

and data from "dd" like this :

[[0.00621772 0.00537531]]

i has check this data and i find this :

tes = J.transpose()*J

and the result like this :

[[ 9.14761019e-05  9.41324993e-05 -8.79884397e-07]
 [ 9.41324993e-05  9.84768945e-05 -9.04538042e-07]
 [-8.79884397e-07 -9.04538042e-07  8.46387506e-09]]

I need to invert this matrix but this data is singular matrix. I have to try on matlab r2017b and running well.

I need to solve this problem on python.

7
  • Show the result you obtained from matlab Commented Sep 12, 2018 at 7:15
  • 2
    isn't the definition of a Singular Matrix, a square matrix that does not have a matrix inverse? Commented Sep 12, 2018 at 7:17
  • 1
    @ReblochonMasque Yes it is. There is something called Psuedo Inverse for such matrices. I think he is referring to that Commented Sep 12, 2018 at 7:19
  • @WahyuHadinoto If you want a psuedo inverse use linalg.pinv from numpy Commented Sep 12, 2018 at 7:21
  • 1
    @WahyuHadinoto linalg.pinv works for the matrix you posted as you can see from my answer. There may be other cases in your problem where the generated matrices are such that psuedo inverse can'be determined. Also, make sure that your numpy and scipy are latest versions.. Commented Sep 12, 2018 at 7:40

2 Answers 2

5

Have you tried using pseudo-inverse numpy.linalg.pinv instead? It's supposed to deal with these situations.

B = np.linalg.pinv(a)

But I would suggest to check that you really calculated correctly your matrix and a singular matrix is supposed to appear.

Sign up to request clarification or add additional context in comments.

Comments

2

If you are sure the calculations you made are correct and it is what you wanted, then you can go for a psuedo inverse for the singular matrix that you are having.

It can be done in python like this..

mat = np.array([[ 9.14761019e-05,  9.41324993e-05, -8.79884397e-07],
 [ 9.41324993e-05,  9.84768945e-05, -9.04538042e-07],
 [-8.79884397e-07, -9.04538042e-07,  8.46387506e-09]])

p_inv = np.linalg.pinv(mat)

print(p_inv)

# output

array([[-1.00783988e+13,  5.50963966e+11, -9.88844703e+14],
       [ 5.50963966e+11, -3.01194390e+10,  5.40580308e+13],
       [-9.88844703e+14,  5.40580308e+13, -9.70207468e+16]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.