Perform LU decomposition on a given matrix and raises an error if the matrix isn't square or if no such decomposition exists >>> matrix = np.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]]) >>> lower_mat, upper_mat = lower_upper_decomposition(matrix) >>> lower_mat array([[1. , 0.
(table: np.ndarray)
| 23 | |
| 24 | |
| 25 | def lower_upper_decomposition(table: np.ndarray) -> tuple[np.ndarray, np.ndarray]: |
| 26 | """ |
| 27 | Perform LU decomposition on a given matrix and raises an error if the matrix |
| 28 | isn't square or if no such decomposition exists |
| 29 | |
| 30 | >>> matrix = np.array([[2, -2, 1], [0, 1, 2], [5, 3, 1]]) |
| 31 | >>> lower_mat, upper_mat = lower_upper_decomposition(matrix) |
| 32 | >>> lower_mat |
| 33 | array([[1. , 0. , 0. ], |
| 34 | [0. , 1. , 0. ], |
| 35 | [2.5, 8. , 1. ]]) |
| 36 | >>> upper_mat |
| 37 | array([[ 2. , -2. , 1. ], |
| 38 | [ 0. , 1. , 2. ], |
| 39 | [ 0. , 0. , -17.5]]) |
| 40 | |
| 41 | >>> matrix = np.array([[4, 3], [6, 3]]) |
| 42 | >>> lower_mat, upper_mat = lower_upper_decomposition(matrix) |
| 43 | >>> lower_mat |
| 44 | array([[1. , 0. ], |
| 45 | [1.5, 1. ]]) |
| 46 | >>> upper_mat |
| 47 | array([[ 4. , 3. ], |
| 48 | [ 0. , -1.5]]) |
| 49 | |
| 50 | >>> # Matrix is not square |
| 51 | >>> matrix = np.array([[2, -2, 1], [0, 1, 2]]) |
| 52 | >>> lower_mat, upper_mat = lower_upper_decomposition(matrix) |
| 53 | Traceback (most recent call last): |
| 54 | ... |
| 55 | ValueError: 'table' has to be of square shaped array but got a 2x3 array: |
| 56 | [[ 2 -2 1] |
| 57 | [ 0 1 2]] |
| 58 | |
| 59 | >>> # Matrix is invertible, but its first leading principal minor is 0 |
| 60 | >>> matrix = np.array([[0, 1], [1, 0]]) |
| 61 | >>> lower_mat, upper_mat = lower_upper_decomposition(matrix) |
| 62 | Traceback (most recent call last): |
| 63 | ... |
| 64 | ArithmeticError: No LU decomposition exists |
| 65 | |
| 66 | >>> # Matrix is singular, but its first leading principal minor is 1 |
| 67 | >>> matrix = np.array([[1, 0], [1, 0]]) |
| 68 | >>> lower_mat, upper_mat = lower_upper_decomposition(matrix) |
| 69 | >>> lower_mat |
| 70 | array([[1., 0.], |
| 71 | [1., 1.]]) |
| 72 | >>> upper_mat |
| 73 | array([[1., 0.], |
| 74 | [0., 0.]]) |
| 75 | |
| 76 | >>> # Matrix is singular, but its first leading principal minor is 0 |
| 77 | >>> matrix = np.array([[0, 1], [0, 1]]) |
| 78 | >>> lower_mat, upper_mat = lower_upper_decomposition(matrix) |
| 79 | Traceback (most recent call last): |
| 80 | ... |
| 81 | ArithmeticError: No LU decomposition exists |
| 82 | """ |
nothing calls this directly
no outgoing calls
no test coverage detected