如何使用 Python SciPy 求解迴圈矩陣方程?


scipy.linalg.solveh_banded 是用於解決帶狀矩陣方程的線性函式。在以下給出的示例中,我們將求解迴圈系統 Cx = b −

示例

from scipy.linalg import solve_circulant, solve, circulant, lstsq
import numpy as np
c = np.array([2, 2, 4])
b = np.array([1, 2, 3])
solve_circulant(c, b)

輸出

array([ 0.75, -0.25, 0.25])

示例

我們來看一個奇異的例子,它會引發 LinAlgError −

from scipy.linalg import solve_circulant, solve, circulant, lstsq
import numpy as np
c = np.array([1, 1, 0, 0])
b = np.array([1, 2, 3, 4])
solve_circulant(c, b)

輸出

--------------------------------------------------------------------------

LinAlgError Traceback (most recent call last)
<ipython-input-6-978604ed0a97> in <module>
----> 1 solve_circulant(c, b)

~\AppData\Roaming\Python\Python37\site-packages\scipy\linalg\basic.py in
solve_circulant(c, b, singular, tol, caxis, baxis, outaxis)
865    if is_near_singular:
866       if singular == 'raise':
--> 867     raise LinAlgError("near singular circulant matrix.")
868    else:
869    # Replace the small values with 1 to avoid errors in the
LinAlgError: near singular circulant matrix.

現在,為了消除此錯誤,我們需要使用選項 singular = ‘lstsq’,如下所示 −

from scipy.linalg import solve_circulant, solve, circulant, lstsq
import numpy as np
c = np.array([1, 1, 0, 0])
b = np.array([1, 2, 3, 4])
solve_circulant(c, b, singular='lstsq')

輸出

array([0.25, 1.25, 2.25, 1.25])

更新日期:2021 年 11 月 24 日

227 次瀏覽

開啟您的職業生涯

完成課程即可獲得認證

開始學習
廣告
© . All rights reserved.