當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python SciPy linalg.qr用法及代碼示例

本文簡要介紹 python 語言中 scipy.linalg.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_a 布爾型,可選

a 中的數據是否被覆蓋(如果通過重用現有的輸入數據結構而不是創建新的輸入數據結構將 overwrite_a 設置為 True,則可能會提高性能。)

lwork 整數,可選

工作數組大小,lwork >= a.shape[1]。如果 None 或 -1,則計算最佳大小。

mode {‘full’, ‘r’, ‘economic’, ‘raw’},可選

確定要返回的信息:Q 和 R(‘full’,默認)、僅 R(‘r’)或 Q 和 R,但在 economy-size 中計算(‘economic’,請參閱注釋)。最後一個選項‘raw’(在SciPy 0.11 中添加)使函數以 LAPACK 使用的內部格式返回兩個矩陣(Q、TAU)。

pivoting 布爾型,可選

因式分解是否應包括rank-revealing qr 分解的旋轉。如果旋轉,計算分解 A P = Q R 如上所述,但選擇 P 使得 R 的對角線不增加。

check_finite 布爾型,可選

是否檢查輸入矩陣是否僅包含有限數。禁用可能會提高性能,但如果輸入確實包含無窮大或 NaN,則可能會導致問題(崩潰、非終止)。

返回

Q 浮點數或複數 ndarray

mode='economic' 的形狀為 (M, M) 或 (M, K)。如果 mode='r' 則不返回。如果 mode='raw' 則替換為元組 (Q, TAU)

R 浮點數或複數 ndarray

mode in ['economic', 'raw'] 的形狀為 (M, N) 或 (K, N)。 K = min(M, N)

P int 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
>>> 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,))

相關用法


注:本文由純淨天空篩選整理自scipy.org大神的英文原創作品 scipy.linalg.qr。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。