Skip to content

Commit 2d392ae

Browse files
authored
Fix formatting and improve docstring clarity
1 parent 23a01a6 commit 2d392ae

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

‎linear_algebra/qr_decomposition.py‎

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
1-
"""In linear algebra, a QR decomposition, also known as a QR factorization
2-
or Q factorization,
3-
is a decomposition of a matrix a into a product matrix_a = QR
4-
of an orthonormal matrix Q and an upper triangular matrix R.
5-
QR decomposition is often used to solve the linear least squares (LLS) problem
6-
and is the basis for a particular eigenvalue algorithm, the QR algorithm.
1+
"""
2+
In linear algebra, a QR decomposition, also known as a QR factorization or
3+
Q factorization, is a decomposition of a matrix A into a product matrix_a = QR of an
4+
orthonormal matrix Q and an upper triangular matrix R.
5+
6+
QR decomposition is often used to solve the linear least squares (LLS) problem and is
7+
the basis for a particular eigenvalue algorithm, the QR algorithm.
8+
79
This algorithm will simply attempt to perform QR decomposition on any square matrix.
8-
Reference: https://en.wikipedia.org/wiki/QR_decomposition"""
10+
https://en.wikipedia.org/wiki/QR_decomposition
11+
"""
912

1013
import numpy as np
1114
from scipy.linalg import qr
1215

1316

1417
def qr_decomposition(matrix_a: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
1518
"""
16-
Perform QR decomposition on a given matrix and raises an error if in
19+
Perform QR decomposition on a given matrix and raise an error if in
1720
rowXcolumn matrix a if row is smaller than column or row,column is less than 2
1821
1922
>>> matrix_a = np.array([[1, 2, 3], [4, 5, 9], [7, 8, 15]])
20-
>>> (matrix_q,matrix_r) = qr_decomposition(matrix_a)
23+
>>> (matrix_q, matrix_r) = qr_decomposition(matrix_a)
2124
>>> matrix_q
2225
array([[-0.17, 0.9 , 0.41],
2326
[-0.51, 0.28, -0.82],
@@ -27,7 +30,7 @@ def qr_decomposition(matrix_a: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
2730
[ 0. , 0.41, -0.41],
2831
[ 0. , 0. , 0. ]])
2932
>>> matrix_a = np.array([[1, 2], [4, 5], [7, 8]])
30-
>>> (matrix_q,matrix_r) = qr_decomposition(matrix_a)
33+
>>> (matrix_q, matrix_r) = qr_decomposition(matrix_a)
3134
>>> matrix_q
3235
array([[-0.21, 0.89, 0.41],
3336
[-0.52, 0.25, -0.82],
@@ -37,17 +40,17 @@ def qr_decomposition(matrix_a: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
3740
[ 0. , -0.76],
3841
[ 0. , 0. ]])
3942
>>> matrix_a = np.array([[1, 2, 3], [4, 5, 6]])
40-
>>> (matrix_q,matrix_r) = qr_decomposition(matrix_a)
43+
>>> (matrix_q, matrix_r) = qr_decomposition(matrix_a)
4144
Traceback (most recent call last):
4245
...
4346
ValueError: row size should be greater than column size
4447
>>> matrix_a = np.array([[1], [4]])
45-
>>> (matrix_q,matrix_r) = qr_decomposition(matrix_a)
48+
>>> (matrix_q, matrix_r) = qr_decomposition(matrix_a)
4649
Traceback (most recent call last):
4750
...
4851
ValueError: row size and column size should be greater than 2
4952
>>> matrix_a = np.array([[1,4]])
50-
>>> (matrix_q,matrix_r) = qr_decomposition(matrix_a)
53+
>>> (matrix_q, matrix_r) = qr_decomposition(matrix_a)
5154
Traceback (most recent call last):
5255
...
5356
ValueError: row size should be greater than column size

0 commit comments

Comments
 (0)