22Lower-upper (LU) decomposition factors a matrix as a product of a lower
33triangular matrix and an upper triangular matrix. A square matrix has an LU
44decomposition under the following conditions:
5+
56 - If the matrix is invertible, then it has an LU decomposition if and only
6- if all of its leading principal minors are non-zero (see
7- https://en.wikipedia.org/wiki/Minor_(linear_algebra) for an explanation of
8- leading principal minors of a matrix).
7+ if all of its leading principal minors are non-zero (see
8+ https://en.wikipedia.org/wiki/Minor_(linear_algebra) for an explanation of
9+ leading principal minors of a matrix).
910 - If the matrix is singular (i.e., not invertible) and it has a rank of k
10- (i.e., it has k linearly independent columns), then it has an LU
11- decomposition if its first k leading principal minors are non-zero.
11+ (i.e., it has k linearly independent columns), then it has an LU
12+ decomposition if its first k leading principal minors are non-zero.
1213
1314This algorithm will simply attempt to perform LU decomposition on any square
1415matrix and raise an error if no such decomposition exists.
@@ -25,6 +26,7 @@ def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray
2526 """
2627 Perform LU decomposition on a given matrix and raises an error if the matrix
2728 isn't square or if no such decomposition exists
29+
2830 >>> matrix = np.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]])
2931 >>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
3032 >>> lower_mat
@@ -45,7 +47,7 @@ def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray
4547 array([[ 4. , 3. ],
4648 [ 0. , -1.5]])
4749
48- # Matrix is not square
50+ >>> # Matrix is not square
4951 >>> matrix = np.array([[2, -2, 1], [0, 1, 2]])
5052 >>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
5153 Traceback (most recent call last):
@@ -54,14 +56,14 @@ def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray
5456 [[ 2 -2 1]
5557 [ 0 1 2]]
5658
57- # Matrix is invertible, but its first leading principal minor is 0
59+ >>> # Matrix is invertible, but its first leading principal minor is 0
5860 >>> matrix = np.array([[0, 1], [1, 0]])
5961 >>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
6062 Traceback (most recent call last):
6163 ...
6264 ArithmeticError: No LU decomposition exists
6365
64- # Matrix is singular, but its first leading principal minor is 1
66+ >>> # Matrix is singular, but its first leading principal minor is 1
6567 >>> matrix = np.array([[1, 0], [1, 0]])
6668 >>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
6769 >>> lower_mat
@@ -71,7 +73,7 @@ def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray
7173 array([[1., 0.],
7274 [0., 0.]])
7375
74- # Matrix is singular, but its first leading principal minor is 0
76+ >>> # Matrix is singular, but its first leading principal minor is 0
7577 >>> matrix = np.array([[0, 1], [0, 1]])
7678 >>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
7779 Traceback (most recent call last):
0 commit comments