solve_circulant#
- scipy.linalg.solve_circulant(c, b, singular='raise', tol=None, caxis=-1, baxis=0, outaxis=0)[原始碼]#
求解 C x = b 中的 x,其中 C 是循環矩陣。
C 是與向量 c 相關聯的循環矩陣。
此系統通過在傅立葉空間中進行除法來求解。計算方式為
x = ifft(fft(b) / fft(c))
其中 fft 和 ifft 分別是快速傅立葉變換及其逆變換。對於大型向量 c,這比使用完整循環矩陣求解系統要快得多。
- 參數:
- carray_like
循環矩陣的係數。
- barray_like
在
a x = b
中的右手邊矩陣。- singularstr,可選
此引數控制近奇異循環矩陣的處理方式。如果 singular 為 “raise” 且循環矩陣接近奇異,則會引發
LinAlgError
。如果 singular 為 “lstsq”,則返回最小平方解。預設值為 “raise”。- tolfloat,可選
如果循環矩陣的任何特徵值的絕對值小於或等於 tol,則該矩陣被視為接近奇異。如果未給定,則 tol 設定為
tol = abs_eigs.max() * abs_eigs.size * np.finfo(np.float64).eps
其中 abs_eigs 是循環矩陣特徵值絕對值的陣列。
- caxisint
當 c 的維度大於 1 時,它被視為循環向量的集合。在這種情況下,caxis 是 c 的軸,用於保存循環係數的向量。
- baxisint
當 b 的維度大於 1 時,它被視為向量的集合。在這種情況下,baxis 是 b 的軸,用於保存右手邊向量。
- outaxisint
當 c 或 b 是多維時,
solve_circulant
返回的值是多維的。在這種情況下,outaxis 是結果的軸,用於保存解向量。
- 返回:
- xndarray
系統
C x = b
的解。
- 引發:
- LinAlgError
如果與 c 相關聯的循環矩陣接近奇異。
另請參閱
circulant
循環矩陣
註解
對於長度為 m 的 1 維向量 c,以及形狀為
(m, ...)
的陣列 b,solve_circulant(c, b)
返回與以下相同的結果
solve(circulant(c), b)
其中
solve
和circulant
來自scipy.linalg
。在版本 0.16.0 中新增。
範例
>>> import numpy as np >>> from scipy.linalg import solve_circulant, solve, circulant, lstsq
>>> c = np.array([2, 2, 4]) >>> b = np.array([1, 2, 3]) >>> solve_circulant(c, b) array([ 0.75, -0.25, 0.25])
將該結果與使用
scipy.linalg.solve
求解系統進行比較>>> solve(circulant(c), b) array([ 0.75, -0.25, 0.25])
奇異範例
>>> c = np.array([1, 1, 0, 0]) >>> b = np.array([1, 2, 3, 4])
呼叫
solve_circulant(c, b)
將會引發LinAlgError
。對於最小平方解,請使用選項singular='lstsq'
>>> solve_circulant(c, b, singular='lstsq') array([ 0.25, 1.25, 2.25, 1.25])
與
scipy.linalg.lstsq
比較>>> x, resid, rnk, s = lstsq(circulant(c), b) >>> x array([ 0.25, 1.25, 2.25, 1.25])
廣播範例
假設我們有兩個循環矩陣的向量儲存在形狀為 (2, 5) 的陣列中,以及三個 b 向量儲存在形狀為 (3, 5) 的陣列中。例如,
>>> c = np.array([[1.5, 2, 3, 0, 0], [1, 1, 4, 3, 2]]) >>> b = np.arange(15).reshape(-1, 5)
我們想要求解循環矩陣和 b 向量的所有組合,結果儲存在形狀為 (2, 3, 5) 的陣列中。當我們忽略 c 和 b 的軸(用於保存係數向量)時,集合的形狀分別為 (2,) 和 (3,),這與廣播不相容。為了獲得形狀為 (2, 3) 的廣播結果,我們向 c 添加一個微不足道的維度:
c[:, np.newaxis, :]
的形狀為 (2, 1, 5)。最後一個維度保存循環矩陣的係數,因此當我們呼叫solve_circulant
時,我們可以使用預設的caxis=-1
。b 向量的係數在陣列 b 的最後一個維度中,因此我們使用baxis=-1
。如果我們使用預設的 outaxis,則結果的形狀將為 (5, 2, 3),因此我們將使用outaxis=-1
將解向量放在最後一個維度中。>>> x = solve_circulant(c[:, np.newaxis, :], b, baxis=-1, outaxis=-1) >>> x.shape (2, 3, 5) >>> np.set_printoptions(precision=3) # For compact output of numbers. >>> x array([[[-0.118, 0.22 , 1.277, -0.142, 0.302], [ 0.651, 0.989, 2.046, 0.627, 1.072], [ 1.42 , 1.758, 2.816, 1.396, 1.841]], [[ 0.401, 0.304, 0.694, -0.867, 0.377], [ 0.856, 0.758, 1.149, -0.412, 0.831], [ 1.31 , 1.213, 1.603, 0.042, 1.286]]])
通過求解一對 c 和 b 向量來檢查(參見
x[1, 1, :]
)>>> solve_circulant(c[1], b[1, :]) array([ 0.856, 0.758, 1.149, -0.412, 0.831])