2016-04-23 30 views
3

我試過QRsolve和cholesky_solve在下面顯示的矩陣(打印str repr)。我從這些中獲得了numpy的結果。函數永遠不會與sympy一起返回,我猜這與sympy試圖用符號方法解決這些問題有關?我嘗試sympy的原因是,隨着我的系統變得越來越複雜,矩陣變成了病態,我正在嘗試使用sympy和mpmath來提高精度。sympy的QRsolve方法永遠不會返回或拋出「無法正常化矢量」錯誤

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腳本。這裏的區別在於我在初始化時創建了矩陣,而不是按值創建矩陣。現在,我沒有得到一個塊,但以下錯誤:

回溯(最近通話最後一個): 文件 「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 \ packages \ sympy \ matrices \ matrices.py「,第1599行,QRdecomposition 」無法標準化矢量%d「。 %j)的 NotImplementedError:無法正常化矢量1

+0

概率得到答案可能是如果您提供我們可以複製和測試的代碼,則會更高。 – tfv

回答

2

作爲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依賴的QR分解方法甚至不嘗試處理浮點錯誤。 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) 

顯然,通過將浮點數的列其規範不一定有模究竟1.在我的例子中獲得的載體,

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

非常感謝,我明白了。我曾嘗試sympy.mpmath.qr_solve,並最終不得不問以下問題:http://stackoverflow.com/questions/36810785/cannot-create-mpf-when-calling-qr-solve –

相關問題