1

I'm using the inverse matrix function inv() in Python. Im calculating the inverse of a 3x3 matrix, but when I multiply the result with the original matrix, I don't get the unity matrix, why?

Example:

AA = [[1,6,5],[2,3,1],[1,1,7]]

>>> inv(AA)
array([[-0.31746032,  0.58730159,  0.14285714],
[ 0.20634921, -0.03174603, -0.14285714],
[ 0.01587302, -0.07936508,  0.14285714]])

>>> inv(AA) * AA
array([[-0.31746032,  3.52380952,  0.71428571],
[ 0.41269841, -0.0952381 , -0.14285714],
[ 0.01587302, -0.07936508,  1.        ]])

>>> inv(AA) * AA
array([[-0.31746032,  3.52380952,  0.71428571],
[ 0.41269841, -0.0952381 , -0.14285714],
[ 0.01587302, -0.07936508,  1.        ]])

...which is not the unity matrix I. What am I missing?

1
  • print(np.dot(inv(AA),AA)) gives you the unity matrix. This is not Matlab, the * operator is element by element multiplication. Commented Nov 19, 2018 at 9:14

2 Answers 2

3

You're doing element-wise multiplication, not matrix multiplication. Change your code to np.matmul(inv(AA), AA) or np.dot(inv(AA), AA) and you'll get the correct result

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

Comments

1

Python 3.5 introduced a new operator for matrix multiplication, so this could be:

from numpy.linalg import inv

AA = [[1,6,5],[2,3,1],[1,1,7]]

inv(AA) @ AA

which gives me:

array([[ 1.00000000e+00,  2.77555756e-17, -1.11022302e-16],
       [ 0.00000000e+00,  1.00000000e+00,  1.11022302e-16],
       [ 0.00000000e+00,  0.00000000e+00,  1.00000000e+00]])

which is about as close to unity as can be expected.

maybe relevant, see differences between @ and dot and other questions/answers linked there.

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.