Python 对于大整数,numpy.dot给出了不正确的答案
我正在研究一些线性代数的东西,但我不明白为什么Python 对于大整数,numpy.dot给出了不正确的答案,python,arrays,numpy,dot-product,Python,Arrays,Numpy,Dot Product,我正在研究一些线性代数的东西,但我不明白为什么numpy会给出以下内容: 我从mathematica手上得到的结果是 编辑:如果需要矩阵: test = [[19722145, -21016468, 51417377], [-185674670, 298847128, -428429486], [289326728, -516012704, 691212936]] A = [[9, 4, 1], [2, 0, 8], [-8, 8, -8]] 正如@Pau
numpy
会给出以下内容:
我从mathematica手上得到的结果是
编辑:如果需要矩阵:
test = [[19722145, -21016468, 51417377],
[-185674670, 298847128, -428429486],
[289326728, -516012704, 691212936]]
A = [[9, 4, 1], [2, 0, 8], [-8, 8, -8]]
正如@PaulPanzer所指出的,您需要使用
np.int64
dtype数组。NumPy在平台/系统配置1上对输入数组使用np.int32
,不检查整数溢出
但是,矩阵乘法的结果包含的整数太大,无法存储在np.int32
中
由于NumPy不会自动将输入数组向上转换为np.int64
,因此在定义数组或通过向上转换时,需要明确指定np.int64
:
import numpy as np
test = np.array([[19722145, -21016468, 51417377],
[-185674670, 298847128, -428429486],
[289326728, -516012704, 691212936]],
dtype=np.int64)
A = np.array([[9, 4, 1],
[2, 0, 8],
[-8, 8, -8]],
dtype=np.int64)
res = np.dot(test, A)
print(res)
[[ -275872647 490227596 -559748615]
[ 2354058114 -4170134568 5632538242]
[-3957788344 6687010400 -9368478392]]
这是我的。还有一些问题。请将数组和代码作为文本而不是图像提问。我尝试复制您的数据,但遗憾的是我无法复制图像中的文本。请尝试
dtype
int64
非常感谢您,这样一个noob错误,哈哈哈,numpy是否使用int32
或int64
将取决于操作系统和计算机。在我看来,最近更新的Linux桌面使用int64
作为默认设置。