Reputation: 341
I have a matrix, key_class
of the value:
[[17, 17, 5],
[21, 18, 21],
[2, 2, 19]]
I'm trying to calculate its inverse with the following code:
key_class = [[17, 17, 5],
[21, 18, 21],
[2, 2, 19]]
import sys
key_class = np.array(key_class)
print ("Class key:")
print (key_class)
# Check if matrix is singular
if np.linalg.cond(key_class) < 1/sys.float_info.epsilon:
print ("Inverse exists")
key_class_inverse = np.linalg.inv(key_class)
else:
print ("Inverse does not exist")
key_class_mul = np.floor(np.matmul(key_class, key_class_inverse))
print ("Class inverse:")
print (key_class_inverse)
print ("Multiplicative Inverse:")
print (key_class_mul)
Here's the output:
Class key:
[[17 17 5]
[21 18 21]
[ 2 2 19]]
Inverse exists
Class inverse:
[[-3.19488818e-01 3.33333333e-01 -2.84345048e-01]
[ 3.80191693e-01 -3.33333333e-01 2.68370607e-01]
[-6.38977636e-03 2.11344372e-18 5.43130990e-02]]
Multiplicative Inverse:
[[ 1. -1. 0.]
[ 0. 1. 0.]
[-1. 0. 0.]]
The output should have been an identity matrix which isn't the case. What's going wrong?
Upvotes: 1
Views: 261
Reputation: 2726
>>> np.round(key_class_inverse@key_class,1) # works
array([[ 1., 0., 0.],
[ 0., 1., -0.],
[ 0., 0., 1.]])
>>> np.floor(key_class_inverse@key_class) # does not work
array([[ 1., 0., 0.],
[ 0., 1., -1.],
[ 0., 0., 0.]])
>>> np.floor(key_class@key_class_inverse) # also does not work
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[-1., 0., 0.]])
You just misunderstand np.floor
. Everything else is good.
Upvotes: 1