一些数值调试使我得到了下面的最小示例。我观察到单个元素的相对误差为 0.75。
有没有办法在不诉诸更高精度算术的情况下估计/限制这个错误?
import numpy as np
x = np.array([[11041, 13359, 15023, 18177], [13359, 16165, 18177, 21995], [15023, 18177, 20453, 24747], [18177, 21995, 24747, 29945]])
y = np.array([[29945, -24747, -21995, 18177], [-24747, 20453, 18177, -15023], [-21995, 18177, 16165, -13359], [18177, -15023, -13359, 11041]])
print(x@y) # high precision
#[[16 0 0 0]
# [ 0 16 0 0]
# [ 0 0 16 0]
# [ 0 0 0 16]]
x0 = x.astype(np.float32)
y0 = y.astype(np.float32)
print(x0@y0) # low precision
#[[28. 0. -4. 0.]
# [ 0. 28. 0. -4.]
# [20. 0. 20. 0.]
# [ 0. 20. 0. 20.]]
这个例子来自使用精确算术计算,并检查在 float32 中
和