-
-
Notifications
You must be signed in to change notification settings - Fork 46.8k
added Schur complement to linear algebra #4793
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
l3str4nge
merged 5 commits into
TheAlgorithms:master
from
iradonov:feature/schur_complement
Oct 18, 2021
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import unittest | ||
|
||
import numpy as np | ||
|
||
|
||
def schur_complement( | ||
mat_a: np.ndarray, | ||
mat_b: np.ndarray, | ||
mat_c: np.ndarray, | ||
pseudo_inv: np.ndarray = None, | ||
) -> np.ndarray: | ||
""" | ||
Schur complement of a symmetric matrix X given as a 2x2 block matrix | ||
consisting of matrices A, B and C. | ||
Matrix A must be quadratic and non-singular. | ||
In case A is singular, a pseudo-inverse may be provided using | ||
the pseudo_inv argument. | ||
|
||
Link to Wiki: https://en.wikipedia.org/wiki/Schur_complement | ||
See also Convex Optimization – Boyd and Vandenberghe, A.5.5 | ||
>>> import numpy as np | ||
>>> a = np.array([[1, 2], [2, 1]]) | ||
>>> b = np.array([[0, 3], [3, 0]]) | ||
>>> c = np.array([[2, 1], [6, 3]]) | ||
>>> schur_complement(a, b, c) | ||
array([[ 5., -5.], | ||
[ 0., 6.]]) | ||
""" | ||
shape_a = np.shape(mat_a) | ||
shape_b = np.shape(mat_b) | ||
shape_c = np.shape(mat_c) | ||
|
||
if shape_a[0] != shape_b[0]: | ||
raise ValueError( | ||
f"Expected the same number of rows for A and B. \ | ||
Instead found A of size {shape_a} and B of size {shape_b}" | ||
) | ||
|
||
if shape_b[1] != shape_c[1]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No tests for this condition. |
||
raise ValueError( | ||
f"Expected the same number of columns for B and C. \ | ||
Instead found B of size {shape_b} and C of size {shape_c}" | ||
) | ||
|
||
a_inv = pseudo_inv | ||
if a_inv is None: | ||
try: | ||
a_inv = np.linalg.inv(mat_a) | ||
except np.linalg.LinAlgError: | ||
raise ValueError( | ||
"Input matrix A is not invertible. Cannot compute Schur complement." | ||
) | ||
|
||
return mat_c - mat_b.T @ a_inv @ mat_b | ||
|
||
|
||
class TestSchurComplement(unittest.TestCase): | ||
def test_schur_complement(self) -> None: | ||
a = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]) | ||
b = np.array([[0, 3], [3, 0], [2, 3]]) | ||
c = np.array([[2, 1], [6, 3]]) | ||
|
||
s = schur_complement(a, b, c) | ||
|
||
input_matrix = np.block([[a, b], [b.T, c]]) | ||
|
||
det_x = np.linalg.det(input_matrix) | ||
det_a = np.linalg.det(a) | ||
det_s = np.linalg.det(s) | ||
|
||
self.assertAlmostEqual(det_x, det_a * det_s) | ||
|
||
def test_improper_a_b_dimensions(self) -> None: | ||
a = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]) | ||
b = np.array([[0, 3], [3, 0], [2, 3]]) | ||
c = np.array([[2, 1], [6, 3]]) | ||
|
||
with self.assertRaises(ValueError): | ||
schur_complement(a, b, c) | ||
|
||
def test_improper_b_c_dimensions(self) -> None: | ||
a = np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]) | ||
b = np.array([[0, 3], [3, 0], [2, 3]]) | ||
c = np.array([[2, 1, 3], [6, 3, 5]]) | ||
|
||
with self.assertRaises(ValueError): | ||
schur_complement(a, b, c) | ||
|
||
|
||
if __name__ == "__main__": | ||
import doctest | ||
|
||
doctest.testmod() | ||
unittest.main() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No tests for this condition.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@l3str4nge added tests.