11"""
2- In linear algebra, a QR decomposition, also known as a QR factorization or QU factorization,
3- is a decomposition of a matrix A into a product A = QR
2+ In linear algebra, a QR decomposition, also known as a QR factorization
3+ or Q factorization,
4+ is a decomposition of a matrix a into a product a = QR
45of an orthonormal matrix Q and an upper triangular matrix R.
56QR decomposition is often used to solve the linear least squares (LLS) problem
67and is the basis for a particular eigenvalue algorithm, the QR algorithm.
1415import numpy as np
1516from scipy .linalg import qr
1617
17- def qr_decomposition (A : np .ndarray ) -> tuple [np .ndarray , np .ndarray ]:
18+ def qr_decomposition (a : np .ndarray ) -> tuple [np .ndarray , np .ndarray ]:
1819 """
1920 Perform QR decomposition on a given matrix and raises an error if in
20- m×n matrix A if m is smaller than n or m,n is less than 2
21+ m×n matrix a if m is smaller than n or m,n is less than 2
2122
22- >>> A = np.array([[1, 2, 3], [4, 5, 9], [7, 8, 15]])
23- >>> Q,R = qr_decomposition(A )
24- >>> Q
23+ >>> a = np.array([[1, 2, 3], [4, 5, 9], [7, 8, 15]])
24+ >>> q,r = qr_decomposition(a )
25+ >>> q
2526 array([[-0.17, 0.9 , 0.41],
2627 [-0.51, 0.28, -0.82],
2728 [-0.85, -0.35, 0.41]])
28- >>> R
29+ >>> r
2930 array([[-17.75, -9.63, -8.11],
3031 [ 0. , 0.41, -0.41],
3132 [ 0. , 0. , 0. ]])
32- >>> A = np.array([[1, 2], [4, 5], [7, 8]])
33- >>> Q,R = qr_decomposition(A )
34- >>> Q
33+ >>> a = np.array([[1, 2], [4, 5], [7, 8]])
34+ >>> q,r = qr_decomposition(a )
35+ >>> q
3536 array([[-0.21, 0.89, 0.41],
3637 [-0.52, 0.25, -0.82],
3738 [-0.83, -0.38, 0.41]])
38- >>> R
39+ >>> r
3940 array([[-9.64, -8.09],
4041 [ 0. , -0.76],
4142 [ 0. , 0. ]])
42- >>> A = np.array([[1, 2, 3], [4, 5, 6]])
43- >>> Q,R = qr_decomposition(A )
43+ >>> a = np.array([[1, 2, 3], [4, 5, 6]])
44+ >>> q,r = qr_decomposition(a )
4445 Traceback (most recent call last):
4546 ...
4647 ValueError: row size should be greater than column size
47- >>> A = np.array([[1], [4]])
48- >>> Q,R = qr_decomposition(A )
48+ >>> a = np.array([[1], [4]])
49+ >>> q,r = qr_decomposition(a )
4950 Traceback (most recent call last):
5051 ...
5152 ValueError: row size and column size should be greater than 2
52- >>> A = np.array([[1,4]])
53- >>> Q,R = qr_decomposition(A )
53+ >>> a = np.array([[1,4]])
54+ >>> q,r = qr_decomposition(a )
5455 Traceback (most recent call last):
5556 ...
5657 ValueError: row size should be greater than column size
5758 """
5859
5960
60- rows , columns = np .shape (A )
61+ rows , columns = np .shape (a )
6162 if rows < columns :
6263 msg = (
6364 "row size should be greater than column size"
@@ -73,13 +74,13 @@ def qr_decomposition(A: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
7374 # R: Upper triangular matrix
7475 # P: Pivot indices (permutation vector)
7576
76- Q , R , P = qr (A , pivoting = True )
77+ q , r , p = qr (a , pivoting = True )
7778
7879 # Note: The bottom row of R is all zeros because the matrix is rank-deficient.
79- # Verification: A [:, P] should equal Q @ R
80- AP = A [:, P ]
81- if (np .allclose (AP , Q @ R )):
82- return np .round (Q ,2 ), np .round (R ,2 )
80+ # Verification: a [:, P] should equal Q @ R
81+ ap = a [:, p ]
82+ if (np .allclose (ap , q @ r )):
83+ return np .round (q ,2 ), np .round (r ,2 )
8384 else :
8485 msg = (
8586 "No matrix found which decompose given matrix"
0 commit comments