user3834119
user3834119

Reputation: 421

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

Upvotes: 1

Views: 4104

Answers (2)

Ganesh_
Ganesh_

Reputation: 589

Here is another method:

I works only on matrix

you can use np.linalg.inv(x) for inverse

In [11]: import numpy as np

In [12]: A = np.array([[4, 3], [3, 2]])

In [13]: B = np.linalg.inv(A)

In [14]: A.dot(B)
Out[14]: 
array([[ 1.,  0.],
       [ 0.,  1.]])

Upvotes: 1

Colonel Beauvel
Colonel Beauvel

Reputation: 31181

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]])

Upvotes: 4

Related Questions