通过矢量乘法的矩阵运算过程中的小数点问题

问题描述 投票:0回答:1

编辑:好,所以,我确实很愚蠢。我建议关闭此问题,因为它不会给您带来任何好处。基本上,我在凌晨2点吸吮基本思维和数学...

这让我发疯。我正在尝试在python中获得带有向量的矩阵的乘积。

我有一个9x9矩阵a和一个1x9向量b。

a = [[-0.03619046050233981, 0.01694804504223569, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.01694804504223569, -0.03619046050233981, 0.01694804504223569, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.01694804504223569, -0.03619046050233981, 0.01694804504223569, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.01694804504223569, -0.03619046050233981, 0.01694804504223569, 0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.01694804504223569, -0.03619046050233981, 0.01694804504223569, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.01694804504223569, -0.03619046050233981, 0.01694804504223569, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.01694804504223569, -0.03619046050233981, 0.01694804504223569, 0.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.01694804504223569, -0.03619046050233981, 0.01694804504223569], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.04756713402738598, -0.06941534088216819]]

a = np.array(a)

b = [2.774218316317263e-09, 1.9240011547847137e-09, 1.3342489213593189e-09, 9.251229045630879e-10, 6.412369741504171e-10, 4.441595895415701e-10, 3.07211064663576e-10, 2.1185176007909776e-10, -4.172326584582343e-11]

b = np.array(b)

print(np.dot(a,b))
>>> array([-6.77921802e-11,  0.00000000e+00,  6.46234854e-27,  0.00000000e+00,
    6.46234854e-27,  3.23117427e-27, -4.84676140e-27, -3.16751358e-12,
    1.29734158e-11])

我期望的是:

...
2nd value: 0.01694805 * 2.77421832e-09 -0.03619046 * 1.92400115e-09 + 0.01694805 * 1.33424892e-09
>>> 2.15478530e-17
...

array([-6.77921695e-11,  2.15478530e-17,  1.47710395e-17,  1.02173204e-17,
    7.13131671e-18,  4.91186255e-18,  3.40092760e-18,  2.36168005e-18,
   -7.44835521e-19])

这里是踢脚:

c = array([[-0.03619046,  0.01694805,  0.        ,  0.        ,  0.        ,
     0.        ,  0.        ,  0.        ,  0.        ],
   [ 0.01694805, -0.03619046,  0.01694805,  0.        ,  0.        ,
     0.        ,  0.        ,  0.        ,  0.        ],
   [ 0.        ,  0.01694805, -0.03619046,  0.01694805,  0.        ,
     0.        ,  0.        ,  0.        ,  0.        ],
   [ 0.        ,  0.        ,  0.01694805, -0.03619046,  0.01694805,
     0.        ,  0.        ,  0.        ,  0.        ],
   [ 0.        ,  0.        ,  0.        ,  0.01694805, -0.03619046,
     0.01694805,  0.        ,  0.        ,  0.        ],
   [ 0.        ,  0.        ,  0.        ,  0.        ,  0.01694805,
    -0.03619046,  0.01694805,  0.        ,  0.        ],
   [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
     0.01694805, -0.03619046,  0.01694805,  0.        ],
   [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
     0.        ,  0.01694805, -0.03619046,  0.01694805],
   [ 0.        ,  0.        ,  0.        ,  0.        ,  0.        ,
     0.        ,  0.        ,  0.04756713, -0.06941534]])

np.dot(c,b)
>>> array([-6.77921692e-11,  2.13353146e-17,  1.47955319e-17,  1.02587195e-17,
    7.11069875e-18,  4.92530088e-18,  3.40667400e-18, -3.16751216e-12,
    1.29734149e-11])

如您所见,ca的近似值,其有效位数较小。使用它给了我预期的效果。我在打数字假象吗?还是我犯了一个巨大而愚蠢的错误?

请注意,a @ b和np.matmul(a,b)遇到相同的问题。

我正在使用Python 3.7.0和Numpy 1.15.0

numpy matrix python-3.7
1个回答
1
投票

似乎您将数字复制粘贴错了。

print(np.dot(a, b))
print(np.dot(a[1], b))
print(
    0.01694804504223569 * 2.774218316317263e-09
    + -0.03619046050233981 * 1.9240011547847137e-09
    + 0.01694804504223569 * 1.3342489213593189e-09
)
[-6.77921802e-11  0.00000000e+00  6.46234854e-27  0.00000000e+00
  6.46234854e-27  3.23117427e-27 -4.84676140e-27 -3.16751358e-12
  1.29734158e-11]
-6.462348535570529e-27
-6.462348535570529e-27

np.dot(a, b)[1]不完全等于np.dot(a[1], b)有点奇怪,但是在这样的大小下,这并不奇怪。

[您可以使用accupykdot / fdot(属于我的项目),看看这些四舍五入的失败实施对您是否有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.