我一直认为numpy uses是一种pairwise-summation,它也确保float32的高精度操作:
import numpy as np
N=17*10**6 # float32-precision no longer enough to hold the whole sum
print(np.ones((N,1),dtype=np.float32).sum(axis=0))
# [17000000.], kind of expected
但是,如果矩阵有多个列,则看起来好像使用了不同的算法:
print(np.ones((N,2),dtype=np.float32).sum(axis=0))
# [16777216. 16777216.] the error is just to big
print(np.ones((2*N,2),dtype=np.float32).sum(axis=0))
# [16777216. 16777216.] error is bigger
大概求和只是天真地将所有值求和.指示是16777216.f 1.0f = 16777216.f,例如:
one = np.array([1.], np.float32)
print(np.array([16777215.], np.float32)+one) # 16777216.
print(np.array([16777216.], np.float32)+one) # 16777216. as well
为什么numpy不对多列使用逐对求和,并且可以强制numpy对多列也使用成对求和?
我的numpy版本是1.14.2,如果有作用的话.