获取给定 NumPy 数组的 QR 分解
在本文中,我们将讨论矩阵的QR 分解或QR 因式分解。矩阵的 QR 因式分解是将矩阵“A”分解为“A=QR”,其中 Q 是正交矩阵,R 是上三角矩阵。我们使用numpy.linalg.qr()函数分解矩阵。
Syntax : numpy.linalg.qr(a, mode=’reduced’)
Parameters :
- a : matrix(M,N) which needs to be factored.
- mode : it is optional. It can be :
以下是如何使用上述函数的一些示例:
示例 1: 2X2 矩阵的 QR 分解
Python3
# Import numpy package
import numpy as np
# Create a numpy array
arr = np.array([[10,22],[13,6]])
# Find the QR factor of array
q, r = np.linalg.qr(arr)
# Print the result
print("Decomposition of matrix:")
print( "q=\n", q, "\nr=\n", r)
Python3
# Import numpy package
import numpy as np
# Create a numpy array
arr = np.array([[0, 1], [1, 0], [1, 1], [2, 2]])
# Find the QR factor of array
q, r = np.linalg.qr(arr)
# Print the result
print("Decomposition of matrix:")
print( "q=\n", q, "\nr=\n", r)
Python3
# Import numpy package
import numpy as np
# Create a numpy array
arr = np.array([[5, 11, -15], [12, 34, -51],
[-24, -43, 92]], dtype=np.int32)
# Find the QR factor of array
q, r = np.linalg.qr(arr)
# Print the result
print("Decomposition of matrix:")
print( "q=\n", q, "\nr=\n", r)
输出 :
示例 2: 2X4 矩阵的 QR 分解
Python3
# Import numpy package
import numpy as np
# Create a numpy array
arr = np.array([[0, 1], [1, 0], [1, 1], [2, 2]])
# Find the QR factor of array
q, r = np.linalg.qr(arr)
# Print the result
print("Decomposition of matrix:")
print( "q=\n", q, "\nr=\n", r)
输出 :
示例 3: 3X3 矩阵的 QR 分解
Python3
# Import numpy package
import numpy as np
# Create a numpy array
arr = np.array([[5, 11, -15], [12, 34, -51],
[-24, -43, 92]], dtype=np.int32)
# Find the QR factor of array
q, r = np.linalg.qr(arr)
# Print the result
print("Decomposition of matrix:")
print( "q=\n", q, "\nr=\n", r)
输出 :