简体   繁体   中英

Python numpy - Giving identity matrix with non-diognal elements non-zero

I'm using python numpy for matrix operations. Calculation of identity matrix is giving unexpected results - Not getting the standard identity matrix.

R0 = matrix([
    [0.02187598,  0.98329681, -0.18068986],
    [0.99856708, -0.01266115,  0.05199501],
    [0.04883878, -0.18156839, -0.9821648]
]);

print R0.dot(R0.I) 

# prints [[  1.00000000e+00   0.00000000e+00  5.55111512e-17]
#         [  0.00000000e+00   1.00000000e+00   0.00000000e+00]
#         [ -5.55111512e-17   0.00000000e+00   1.00000000e+00]]

The problem is that even though mathematically the result of dot(R, RI) is equal to I, due to numerical errors in the floating point numbers, numpy returns something very close to I, but not exactly equal to it.

The values with e-17 are very close approximations of 0.

If you want to generate the exact identity matrix, just use numpy.identity:

numpy.identity(3)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM