mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-04-22 05:37:36 +00:00
Enforce symmetry on A
This commit is contained in:
parent
307ce1cd7f
commit
907b783668
@ -43,7 +43,8 @@ def cholesky_decomposition(A: np.ndarray) -> np.ndarray:
|
|||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
|
|
||||||
assert A.shape[0] == A.shape[1], f"A is not square, {A.shape=}"
|
assert A.shape[0] == A.shape[1], f"Matrix A is not square, {A.shape=}"
|
||||||
|
assert np.allclose(A, A.T), "Matrix A must be symmetric"
|
||||||
|
|
||||||
n = A.shape[0]
|
n = A.shape[0]
|
||||||
L = np.tril(A)
|
L = np.tril(A)
|
||||||
@ -74,8 +75,8 @@ def solve_cholesky(L: np.ndarray, Y: np.ndarray) -> np.ndarray:
|
|||||||
True
|
True
|
||||||
"""
|
"""
|
||||||
|
|
||||||
assert L.shape[0] == L.shape[1], f"L is not square, {L.shape=}"
|
assert L.shape[0] == L.shape[1], f"Matrix L is not square, {L.shape=}"
|
||||||
assert np.allclose(np.tril(L), L), "L is not lower triangular"
|
assert np.allclose(np.tril(L), L), "Matrix L is not lower triangular"
|
||||||
|
|
||||||
# Handle vector case by reshaping to matrix and then flattening again
|
# Handle vector case by reshaping to matrix and then flattening again
|
||||||
if len(Y.shape) == 1:
|
if len(Y.shape) == 1:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user