user3834119 user3834119 - 4 months ago 26
Python Question

Python numpy: Matrix multiplication giving wrong result

I'm using matrices in numpy python. I have a matrix A and I then I calculate its inverse. Now I multiply A with its inverse, and I'm not getting the identity matrix. Can anyone point out what's wrong here?

A = matrix([
[4, 3],
[3, 2]
]);

print (A.I) # prints [[-2 3], [ 3 -4]] - correct
print A.dot(A.T) # prints [[25 18], [18 13]] - Incorrect
print A*(A.T) # prints [[25 18], [18 13]] - Incorrect

Answer

You are using dot on the matrix and the transposed matrix (not the inverse) ...

In [16]: np.dot(A.I, A)
Out[16]:
matrix([[ 1.,  0.],
        [ 0.,  1.]])

With the transposed you have the result you showed:

In [17]: np.dot(A.T, A)
Out[17]:
matrix([[25, 18],
        [18, 13]])