Python 与 Matlab 的准确性以及奇怪的行为
我发现 Matlab 和 Python 之间有一个非常奇怪的行为。首先,我尝试解释一下:
我正在将 Matlab 代码翻译为 Python,它似乎工作得很好。我需要确定矩阵 A 的特征值进行计算,经过一些代数后我发现一个数量与 matlab 中的数量不同。因此,我将 4 位数字的矩阵 A 从 Python 复制到 Matlab,得到了与 Python 中相同的“错误”结果。所以我现在从 Python 复制相同的矩阵 A,但这次有 15 位数字,Matlab 中的结果是正确的。其次,我将 20 位数字的矩阵 A 从 Python 复制到 Matlab,结果是“错误”的。我不明白为什么 Matlab 只显示 15 位数字的正确结果。有趣的是,如果我取 4 位数字或 20 位数字,matlab 会得出相同的结果。 matlab只针对小数点分隔符后的15位数字计算出想要的结果。
有什么办法可以解决这个问题吗? Matlab 确定结果是正确的(因为我知道答案),但 Python 是错误的。
如果有人需要更多解释,我会尝试扩展我的问题,但整个计算非常复杂,所以我无法上传整个代码。
今天,我试图找到问题所在,结果发现,结果是eigh
。 我需要计算对称托普利茨矩阵 A 的特征值和特征向量,
w, v = np.linalg.eigh(A, UPLO='U')
我的特征值为零,并且该特征值的结果有所不同:7.13188e-14
与 3.33955e-14
。尽管矩阵 A 在两种情况下相同,但结果有所不同。怎么会这样呢?
I discover a very strange behaviour between Matlab and Python. First, I try to explain:
I am translating a Matlab Code to Python and it seems to work very well. I need to determine the eigenvalues of matrix A for a calculation and after some algebra I discover that a quantity is not the same as in matlab. So I copy the matrix A from Python to Matlab with 4 digits and I got the same "wrong" result as in Python. So I copy now the same Matrix A from Python but this time with 15 digits and the result in Matlab is right. Second, I copy the Matrix A from Python to Matlab with 20 digits and the result is the "wrong" one. I can't understand why Matlab shows the right result only for 15 digits. The funny thing is, that matlab lead to the same result if I take 4 digits or 20 digits. Only for 15 digits after the decimal seperator matlab calculate the desired result.
Is there any way to solve this problem? Matlab determines the result right (since I know the answer) but Python is wrong.
If someone need more explanation I try to extend my question, but the whole calculation is very complicated, so I cannot upload the whole code.
Today, I try to find the problem and I discover, that the result from eigh
.
I need to calculate the eigenvalues and eigenvectors of a symmetric Toeplitz matrix A with
w, v = np.linalg.eigh(A, UPLO='U')
I have an eigenvalue that is zero and the result differs for this eigenvalue: 7.13188e-14
vs.3.33955e-14
. Although the matrix A is in both cases the same, the result varies. How can that be the case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论