Skip to content

Commit 2d8f805

Browse files
committed
changes in description and variable
1 parent 58596f3 commit 2d8f805

1 file changed

Lines changed: 26 additions & 26 deletions

File tree

linear_algebra/qr_decomposition.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
"""
22
In linear algebra, a QR decomposition, also known as a QR factorization
33
or Q factorization,
4-
is a decomposition of a matrix a into a product a = QR
4+
is a decomposition of a matrix a into a product matrix_a = QR
55
of an orthonormal matrix Q and an upper triangular matrix R.
66
QR decomposition is often used to solve the linear least squares (LLS) problem
77
and is the basis for a particular eigenvalue algorithm, the QR algorithm.
@@ -15,50 +15,50 @@
1515
import numpy as np
1616
from scipy.linalg import qr
1717

18-
def qr_decomposition(a: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
18+
def qr_decomposition(matrix_a: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
1919
"""
2020
Perform QR decomposition on a given matrix and raises an error if in
2121
m×n matrix a if m is smaller than n or m,n is less than 2
2222
23-
>>> a = np.array([[1, 2, 3], [4, 5, 9], [7, 8, 15]])
24-
>>> q,r = qr_decomposition(a)
25-
>>> q
23+
>>> matrix_a = np.array([[1, 2, 3], [4, 5, 9], [7, 8, 15]])
24+
>>> (matrix_q,matrix_r) = qr_decomposition(matrix_a)
25+
>>> matrix_q
2626
array([[-0.17, 0.9 , 0.41],
2727
[-0.51, 0.28, -0.82],
2828
[-0.85, -0.35, 0.41]])
29-
>>> r
29+
>>> matrix_r
3030
array([[-17.75, -9.63, -8.11],
3131
[ 0. , 0.41, -0.41],
3232
[ 0. , 0. , 0. ]])
33-
>>> a = np.array([[1, 2], [4, 5], [7, 8]])
34-
>>> q,r = qr_decomposition(a)
35-
>>> q
33+
>>> matrix_a = np.array([[1, 2], [4, 5], [7, 8]])
34+
>>> (matrix_q,matrix_r) = qr_decomposition(matrix_a)
35+
>>> matrix_q
3636
array([[-0.21, 0.89, 0.41],
3737
[-0.52, 0.25, -0.82],
3838
[-0.83, -0.38, 0.41]])
39-
>>> r
39+
>>> matrix_r
4040
array([[-9.64, -8.09],
4141
[ 0. , -0.76],
4242
[ 0. , 0. ]])
43-
>>> a = np.array([[1, 2, 3], [4, 5, 6]])
44-
>>> q,r = qr_decomposition(a)
43+
>>> matrix_a = np.array([[1, 2, 3], [4, 5, 6]])
44+
>>> (matrix_q,matrix_r) = qr_decomposition(matrix_a)
4545
Traceback (most recent call last):
4646
...
4747
ValueError: row size should be greater than column size
48-
>>> a = np.array([[1], [4]])
49-
>>> q,r = qr_decomposition(a)
48+
>>> matrix_a = np.array([[1], [4]])
49+
>>> (matrix_q,matrix_r) = qr_decomposition(matrix_a)
5050
Traceback (most recent call last):
5151
...
5252
ValueError: row size and column size should be greater than 2
53-
>>> a = np.array([[1,4]])
54-
>>> q,r = qr_decomposition(a)
53+
>>> matrix_a = np.array([[1,4]])
54+
>>> (matrix_q,matrix_r) = qr_decomposition(matrix_a)
5555
Traceback (most recent call last):
5656
...
5757
ValueError: row size should be greater than column size
5858
"""
5959

6060

61-
rows, columns = np.shape(a)
61+
rows, columns = np.shape(matrix_a)
6262
if rows < columns:
6363
msg = (
6464
"row size should be greater than column size"
@@ -70,17 +70,17 @@ def qr_decomposition(a: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
7070
)
7171
raise ValueError(msg)
7272
# Perform QR decomposition with pivoting
73-
# Q: Orthogonal matrix
74-
# R: Upper triangular matrix
75-
# P: Pivot indices (permutation vector)
73+
# matrix_q: Orthogonal matrix
74+
# matrix_v: Upper triangular matrix
75+
# pivot: Pivot indices (permutation vector)
7676

77-
q, r, p = qr(a, pivoting=True)
77+
matrix_q, matrix_r, pivot = qr(matrix_a, pivoting=True)
7878

79-
# Note: The bottom row of R is all zeros because the matrix is rank-deficient.
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)
79+
# Note: The bottom row of matrix_r is all zeros because the matrix is rank-deficient.
80+
# Verification: matrix_a[:, pivot] should equal matrix_q @ matrix_r
81+
permute_matrix = matrix_a[:, pivot]
82+
if(np.allclose(permute_matrix, matrix_q @ matrix_r)):
83+
return np.round(matrix_q,2), np.round(matrix_r,2)
8484
else:
8585
msg = (
8686
"No matrix found which decompose given matrix"

0 commit comments

Comments
 (0)