-
-
Notifications
You must be signed in to change notification settings - Fork 51k
Add cholesky_decomposition.py #11848
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8019213
Add cholesky_decomposition
99991 307ce1c
Simplify equations, rename variables
99991 907b783
Enforce symmetry on A
99991 818448b
Fix typo
99991 4522258
Rename variables
99991 7808f21
Rename variables
99991 21e1cd9
Merge branch 'master' into master
cclauss 6e88539
updating DIRECTORY.md
cclauss 666c446
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import numpy as np | ||
|
|
||
|
|
||
| def cholesky_decomposition(matrix: np.ndarray) -> np.ndarray: | ||
| """Return a Cholesky decomposition of the matrix A. | ||
|
|
||
| The Cholesky decomposition decomposes the square, positive definite matrix A | ||
| into a lower triangular matrix L such that A = L L^T. | ||
|
|
||
| https://en.wikipedia.org/wiki/Cholesky_decomposition | ||
|
|
||
| Arguments: | ||
| A -- a numpy.ndarray of shape (n, n) | ||
|
|
||
| >>> A = np.array([[4, 12, -16], [12, 37, -43], [-16, -43, 98]], dtype=float) | ||
| >>> L = cholesky_decomposition(A) | ||
| >>> np.allclose(L, np.array([[2, 0, 0], [6, 1, 0], [-8, 5, 3]])) | ||
| True | ||
|
|
||
| >>> # check that the decomposition is correct | ||
| >>> np.allclose(L @ L.T, A) | ||
| True | ||
|
|
||
| >>> # check that L is lower triangular | ||
| >>> np.allclose(np.tril(L), L) | ||
| True | ||
|
|
||
| The Cholesky decomposition can be used to solve the linear system A x = y. | ||
|
|
||
| >>> x_true = np.array([1, 2, 3], dtype=float) | ||
| >>> y = A @ x_true | ||
| >>> x = solve_cholesky(L, y) | ||
| >>> np.allclose(x, x_true) | ||
| True | ||
|
|
||
| It can also be used to solve multiple equations A X = Y simultaneously. | ||
|
|
||
| >>> X_true = np.random.rand(3, 3) | ||
| >>> Y = A @ X_true | ||
| >>> X = solve_cholesky(L, Y) | ||
| >>> np.allclose(X, X_true) | ||
| True | ||
| """ | ||
|
|
||
| assert matrix.shape[0] == matrix.shape[1], ( | ||
| f"Input matrix is not square, {matrix.shape=}" | ||
| ) | ||
| assert np.allclose(matrix, matrix.T), "Input matrix must be symmetric" | ||
|
|
||
| n = matrix.shape[0] | ||
| lower_triangle = np.tril(matrix) | ||
|
|
||
| for i in range(n): | ||
| for j in range(i + 1): | ||
| lower_triangle[i, j] -= np.sum( | ||
| lower_triangle[i, :j] * lower_triangle[j, :j] | ||
| ) | ||
|
|
||
| if i == j: | ||
| if lower_triangle[i, i] <= 0: | ||
| raise ValueError("Matrix A is not positive definite") | ||
|
|
||
| lower_triangle[i, i] = np.sqrt(lower_triangle[i, i]) | ||
| else: | ||
| lower_triangle[i, j] /= lower_triangle[j, j] | ||
|
|
||
| return lower_triangle | ||
|
|
||
|
|
||
| def solve_cholesky( | ||
| lower_triangle: np.ndarray, | ||
| right_hand_side: np.ndarray, | ||
| ) -> np.ndarray: | ||
| """Given a Cholesky decomposition L L^T = A of a matrix A, solve the | ||
| system of equations A X = Y where the right-hand side Y is either | ||
| a matrix or a vector. | ||
|
|
||
| >>> L = np.array([[2, 0], [3, 4]], dtype=float) | ||
| >>> Y = np.array([[22, 54], [81, 193]], dtype=float) | ||
| >>> X = solve_cholesky(L, Y) | ||
| >>> np.allclose(X, np.array([[1, 3], [3, 7]], dtype=float)) | ||
| True | ||
| """ | ||
|
|
||
| assert lower_triangle.shape[0] == lower_triangle.shape[1], ( | ||
| f"Matrix L is not square, {lower_triangle.shape=}" | ||
| ) | ||
| assert np.allclose(np.tril(lower_triangle), lower_triangle), ( | ||
| "Matrix L is not lower triangular" | ||
| ) | ||
|
|
||
| # Handle vector case by reshaping to matrix and then flattening again | ||
| if len(right_hand_side.shape) == 1: | ||
| return solve_cholesky(lower_triangle, right_hand_side.reshape(-1, 1)).ravel() | ||
|
|
||
| n = right_hand_side.shape[0] | ||
|
|
||
| # Solve L W = Y for W | ||
| w = right_hand_side.copy() | ||
| for i in range(n): | ||
| for j in range(i): | ||
| w[i] -= lower_triangle[i, j] * w[j] | ||
|
|
||
| w[i] /= lower_triangle[i, i] | ||
|
|
||
| # Solve L^T X = W for X | ||
| x = w | ||
| for i in reversed(range(n)): | ||
| for j in range(i + 1, n): | ||
| x[i] -= lower_triangle[j, i] * x[j] | ||
|
|
||
| x[i] /= lower_triangle[i, i] | ||
|
|
||
| return x | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
|
|
||
| doctest.testmod() | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.