1414import numpy as np
1515from scipy .linalg import qr
1616
17+
1718def 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
@@ -56,17 +57,12 @@ def qr_decomposition(A: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
5657 ValueError: row size should be greater than column size
5758 """
5859
59-
6060 rows , columns = np .shape (A )
6161 if rows < columns :
62- msg = (
63- "row size should be greater than column size"
64- )
62+ msg = "row size should be greater than column size"
6563 raise ValueError (msg )
6664 if rows < 2 or columns < 2 :
67- msg = (
68- "row size and column size should be greater than 2"
69- )
65+ msg = "row size and column size should be greater than 2"
7066 raise ValueError (msg )
7167 # Perform QR decomposition with pivoting
7268 # Q: Orthogonal matrix
@@ -78,14 +74,13 @@ def qr_decomposition(A: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
7874 # Note: The bottom row of R is all zeros because the matrix is rank-deficient.
7975 # Verification: A[:, P] should equal Q @ R
8076 AP = A [:, P ]
81- if ( np .allclose (AP , Q @ R ) ):
82- return np .round (Q ,2 ), np .round (R ,2 )
77+ if np .allclose (AP , Q @ R ):
78+ return np .round (Q , 2 ), np .round (R , 2 )
8379 else :
84- msg = (
85- "No matrix found which decompose given matrix"
86- )
80+ msg = "No matrix found which decompose given matrix"
8781 raise ValueError (msg )
8882
83+
8984if __name__ == "__main__" :
9085 import doctest
9186
0 commit comments