如何使用 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])
广告