gpt4 book ai didi

python - sympy的QRsolve方法从不返回或抛出 "Could not normalize the vector"错误

转载 作者:行者123 更新时间:2023-11-28 17:28:14 26 4
gpt4 key购买 nike

我已经在下面显示的矩阵上尝试了 QRsolve 和 cholesky_solve(打印的 str repr)。我用这些从 numpy 得到结果。这些函数永远不会随 sympy 一起返回,我想这与 sympy 试图使用符号方法解决这些问题有关吗?我尝试使用 sympy 的原因是,随着我的系统变得越来越复杂,矩阵变得病态,我正在尝试使用 sympy 和 mpmath 来提高精度。

from sympy.matrices import Matrix as sy_matrix

A = sy_matrix([[-1.73598602689344 - 0.555723094599341j, -1.73598602689344 - 0.555723094599341j, 0.232989179693563 - 0.308565151130628j, 0.232989179693563 - 0.308565151130628j, 0.785911137306334 + 0.373372141423308j, 0.785911137306334 + 0.373372141423308j, 0.436377021604638 + 0.329496457808818j, 0.436377021604638 + 0.329496457808818j],
[-2.47182252542744 - 3.12228363243637j, -8.23364083219883 - 10.4003267796456j, 0.752244101320904 + 0.206999511678148j, 2.50572510149994 + 0.689515373399912j, 8.05887958417013 + 10.8152044077855j, 26.8441278948708 + 36.0254458823337j, -0.534283343532919 + 1.94160599872119j, -1.77969781730816 + 6.46748958174029j],
[-2.44359008697499 - 4.49072303037516j, -13.8356070724522 - 25.4264737979839j, 1.0065385313486 + 0.721365705527217j, 5.69902116449569 + 4.08437262469506j, 15.1118001221454 + 29.8836019724734j, 85.5630122915864 + 169.200954368143j, -2.42747866727978 + 3.38711806497384j, -13.744384214138 + 19.1778624838817j],
[-3.12331363856586 - 6.06170033660146j, -24.9646459130564 - 48.4511707904544j, 1.44382306453606 + 1.1598998787916j, 11.5404777548365 + 9.27107973118107j, 24.2361910493061 + 51.4282308184496j, 193.719875057099 + 411.065848931859j, -4.63756924615993 + 5.77276501482576j, -37.0680909845555 + 46.1417107635014j],
[0.232989179693563 - 0.308565151130628j, 0.232989179693563 - 0.308565151130628j, -1.73598602689344 - 0.555723094599341j, -1.73598602689344 - 0.555723094599341j, 0.436377021604638 + 0.329496457808818j, 0.436377021604638 + 0.329496457808818j, 0.785911137306335 + 0.373372141423309j, 0.785911137306335 + 0.373372141423309j],
[0.752244101320904 + 0.206999511678148j, 2.50572510149994 + 0.689515373399912j, -2.47182252542744 - 3.12228363243637j, -8.23364083219883 - 10.4003267796456j, -0.534283343532919 + 1.94160599872119j, -1.77969781730816 + 6.46748958174029j, 8.05887958417013 + 10.8152044077855j, 26.8441278948708 + 36.0254458823337j],
[1.0065385313486 + 0.721365705527217j, 5.69902116449569 + 4.08437262469506j, -2.44359008697499 - 4.49072303037516j, -13.8356070724522 - 25.4264737979839j, -2.42747866727978 + 3.38711806497384j, -13.744384214138 + 19.1778624838817j, 15.1118001221454 + 29.8836019724734j, 85.5630122915864 + 169.200954368143j],
[1.44382306453606 + 1.1598998787916j, 11.5404777548365 + 9.27107973118107j, -3.12331363856586 - 6.06170033660146j, -24.9646459130564 - 48.4511707904544j, -4.63756924615993 + 5.77276501482576j, -37.0680909845555 + 46.1417107635014j, 24.2361910493061 + 51.4282308184496j, 193.7198750571 + 411.065848931859j]])

b = sy_matrix([[1.73598602689344 + 0.555723094599341j], [0.742066203971011 + 0.937341228590923j], [0.431577196569235 + 0.793133703704558j], [0.39075611642261 + 0.758376121181231j], [-0.232989179693563 + 0.308565151130628j], [-0.225831312314891 - 0.06214335385114j], [-0.177770846229001 - 0.127404751947585j], [-0.180635939514086 - 0.145114460001454j]])

x = A.QRsolve(b)

我按照评论中的建议粘贴了代码,构建了一个简单的 repo 脚本。这里的区别是我在初始化时创建矩阵,而不是按值创建矩阵。现在我没有得到一个 block ,但出现以下错误:

追溯(最近的调用最后): 文件“sympyTest.py”,第 14 行,位于 x = A.QRsolve(b) 文件“C:\Python27\lib\site-packages\sympy\matrices\matrices.py”,第 1633 行,在 QRsolve 中 Q, R = self.as_mutable().QRdecomposition() 文件“C:\Python27\lib\site-packages\sympy\matrices\matrices.py”,第 1599 行,在 QRdecomposition 中 “无法标准化向量 %d。” %j)NotImplementedError:无法规范化向量 1。

最佳答案

作为QRsolve documentation says ,

This is mainly for educational purposes and symbolic matrices, for real (or complex) matrices use sympy.mpmath.qr_solve.

我建议遵循此建议,或者单独使用 mpmath。

这是问题 qith QRsolve 的一个更简单的重现:

A = sy_matrix([[2+3j, 1], [1, 1]])
b = sy_matrix([[1], [1]])
A.QRsolve(b)

这会抛出“NotImplementedError:无法规范化向量 0”。因为 QRsolve 所依赖的 QRdecomposition 方法甚至不尝试处理浮点错误。 This is what it does :

R[j, j] = tmp.norm()
Q[:, j] = tmp / R[j, j]
if Q[:, j].norm() != 1:
raise NotImplementedError("Could not normalize the vector %d." % j)

显然,通过将一列 float 除以其范数而获得的向量不需要恰好范数为 1。在我的示例中,

>>> (A[:,0]/A[:,0].norm()).norm() == 1
False

关于python - sympy的QRsolve方法从不返回或抛出 "Could not normalize the vector"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36815808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com