Error in code
Title
Question
To practice i had created another matrix find its inverse and check whether its multiplication with it's inverse is giving an identity matrix or not. as you had said that multiplication with it's inverse gives an identity matrix so the result of allclose function has to be True but I am getting False. I am sharing the code please tell me where the mistake is?
#calculating determinant of a matrix
from numpy.linalg import det
m=asmatrix(arange(1,10).reshape(3,3))
print(m)
print(det(m))
[[1 2 3] [4 5 6] [7 8 9]] 6.66133814775094e-16
from numpy.linalg import inv print(m) im=inv(m) print(im)
[[1 2 3] [4 5 6] [7 8 9]] [[-4.50359963e+15 9.00719925e+15 -4.50359963e+15] [ 9.00719925e+15 -1.80143985e+16 9.00719925e+15] [-4.50359963e+15 9.00719925e+15 -4.50359963e+15]]
#to check whether multiplication of a matrix with it's inverse matrix gives identity matrix or not from numpy import eye,allclose print(asmatrix(eye(3))) rs=im*m rs.shape print(rs) allclose(rs,asmatrix(eye(3)))
[[1. 0. 0.] [0. 1. 0.] [0. 0. 1.]] [[ 12. 8. 8.] [-16. -8. 0.] [ 4. 0. 0.]]
Out[118]:
False
Python-3.4.3 Advanced-Matrix-Operations 06-07 min 50-60 sec
Answers:
m4 = asmatrix([[2,-3,1], [2,0,-1], [1,4,5]]) #Normal 3x3 matrix im4 = inv(m4) #Inverse of the matrix m4 print(allclose(im4*m4, asmatrix(eye(3)))) #eye(3) is a 3x3 identity matrix and allclose checks whether both are same or not.
Login to add comment