2
2
Lower-upper (LU) decomposition factors a matrix as a product of a lower
3
3
triangular matrix and an upper triangular matrix. A square matrix has an LU
4
4
decomposition under the following conditions:
5
+
5
6
- 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).
9
10
- 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.
12
13
13
14
This algorithm will simply attempt to perform LU decomposition on any square
14
15
matrix 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
25
26
"""
26
27
Perform LU decomposition on a given matrix and raises an error if the matrix
27
28
isn't square or if no such decomposition exists
29
+
28
30
>>> matrix = np.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]])
29
31
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
30
32
>>> lower_mat
@@ -45,7 +47,7 @@ def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray
45
47
array([[ 4. , 3. ],
46
48
[ 0. , -1.5]])
47
49
48
- # Matrix is not square
50
+ >>> # Matrix is not square
49
51
>>> matrix = np.array([[2, -2, 1], [0, 1, 2]])
50
52
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
51
53
Traceback (most recent call last):
@@ -54,14 +56,14 @@ def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray
54
56
[[ 2 -2 1]
55
57
[ 0 1 2]]
56
58
57
- # Matrix is invertible, but its first leading principal minor is 0
59
+ >>> # Matrix is invertible, but its first leading principal minor is 0
58
60
>>> matrix = np.array([[0, 1], [1, 0]])
59
61
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
60
62
Traceback (most recent call last):
61
63
...
62
64
ArithmeticError: No LU decomposition exists
63
65
64
- # Matrix is singular, but its first leading principal minor is 1
66
+ >>> # Matrix is singular, but its first leading principal minor is 1
65
67
>>> matrix = np.array([[1, 0], [1, 0]])
66
68
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
67
69
>>> lower_mat
@@ -71,7 +73,7 @@ def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray
71
73
array([[1., 0.],
72
74
[0., 0.]])
73
75
74
- # Matrix is singular, but its first leading principal minor is 0
76
+ >>> # Matrix is singular, but its first leading principal minor is 0
75
77
>>> matrix = np.array([[0, 1], [0, 1]])
76
78
>>> lower_mat, upper_mat = lower_upper_decomposition(matrix)
77
79
Traceback (most recent call last):
0 commit comments