qr#
- scipy.linalg.qr(a, overwrite_a=False, lwork=None, mode='full', pivoting=False, check_finite=True)[原始碼]#
計算矩陣的 QR 分解。
計算分解式
A = Q R
,其中 Q 是么正/正交矩陣,而 R 是上三角矩陣。- 參數:
- a(M, N) 類陣列
要分解的矩陣
- overwrite_abool,可選
是否覆寫 a 中的資料(如果將 overwrite_a 設定為 True,透過重複使用現有的輸入資料結構而不是建立新的資料結構,可能會提高效能。)
- lworkint,可選
工作陣列大小,lwork >= a.shape[1]。如果為 None 或 -1,則會計算最佳大小。
- mode{‘full’,‘r’,‘economic’,‘raw’},可選
決定要傳回的資訊:Q 和 R 兩者(‘full’,預設),僅 R(‘r’),或 Q 和 R 兩者,但在經濟尺寸中計算(‘economic’,請參閱「註解」)。最後一個選項 ‘raw’(在 SciPy 0.11 中新增)使函式傳回 LAPACK 使用的內部格式的兩個矩陣(Q,TAU)。
- pivotingbool,可選
是否應包含用於顯示秩的 qr 分解的樞軸。如果進行樞軸,則計算分解式
A[:, P] = Q @ R
,如上所述,但其中選擇 P,使得 R 的對角線是非遞增的。或者,儘管效率較低,但可以透過置換單位矩陣的行或列(取決於要在其上使用的方程式的哪一側)來顯式形成顯式 P 矩陣。請參閱「範例」。- check_finitebool,可選
是否檢查輸入矩陣是否僅包含有限數字。停用可能會提高效能,但如果輸入確實包含無限或 NaN,則可能會導致問題(崩潰、未終止)。
- 返回:
- Qfloat 或 complex ndarray
形狀為 (M, M),或
mode='economic'
時為 (M, K)。如果mode='r'
,則不返回。如果mode='raw'
,則替換為元組(Q, TAU)
。- Rfloat 或 complex ndarray
形狀為 (M, N),或
mode in ['economic', 'raw']
時為 (K, N)。K = min(M, N)
。- Pint ndarray
當
pivoting=True
時,形狀為 (N,)。如果pivoting=False
,則不返回。
- 引發:
- LinAlgError
如果分解失敗則引發
註解
這是 LAPACK 常式 dgeqrf、zgeqrf、dorgqr、zungqr、dgeqp3 和 zgeqp3 的介面。
如果
mode=economic
,則 Q 和 R 的形狀為 (M, K) 和 (K, N) 而不是 (M,M) 和 (M,N),其中K=min(M,N)
。範例
>>> import numpy as np >>> from scipy import linalg >>> rng = np.random.default_rng() >>> a = rng.standard_normal((9, 6))
>>> q, r = linalg.qr(a) >>> np.allclose(a, np.dot(q, r)) True >>> q.shape, r.shape ((9, 9), (9, 6))
>>> r2 = linalg.qr(a, mode='r') >>> np.allclose(r, r2) True
>>> q3, r3 = linalg.qr(a, mode='economic') >>> q3.shape, r3.shape ((9, 6), (6, 6))
>>> q4, r4, p4 = linalg.qr(a, pivoting=True) >>> d = np.abs(np.diag(r4)) >>> np.all(d[1:] <= d[:-1]) True >>> np.allclose(a[:, p4], np.dot(q4, r4)) True >>> P = np.eye(p4.size)[p4] >>> np.allclose(a, np.dot(q4, r4) @ P) True >>> np.allclose(a @ P.T, np.dot(q4, r4)) True >>> q4.shape, r4.shape, p4.shape ((9, 9), (9, 6), (6,))
>>> q5, r5, p5 = linalg.qr(a, mode='economic', pivoting=True) >>> q5.shape, r5.shape, p5.shape ((9, 6), (6, 6), (6,)) >>> P = np.eye(6)[:, p5] >>> np.allclose(a @ P, np.dot(q5, r5)) True