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
Fix 19547 is positive semidefinite incorrect #19573
Changes from 6 commits
bf4fcef
1bf89e8
74e6807
50fbb84
b07e43f
9e80ddf
1682ac3
7612cb8
7bb906e
b15fcf0
f2537e6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,11 @@ | |
from sympy.matrices import eye, Matrix | ||
from sympy.core.singleton import S | ||
from sympy.testing.pytest import raises, XFAIL | ||
from sympy.matrices.eigen import ( | ||
_is_positive_semidefinite_by_eigenvalues, | ||
_is_positive_semidefinite_by_cholesky_factorization_with_pivots, | ||
_is_positive_semidefinite_by_minors | ||
) | ||
from sympy.matrices.matrices import NonSquareMatrixError, MatrixError | ||
from sympy.simplify.simplify import simplify | ||
from sympy.matrices.immutable import ImmutableMatrix | ||
|
@@ -493,13 +498,19 @@ def test_definite(): | |
m = Matrix([[2, -1, 0], [-1, 2, -1], [0, -1, 2]]) | ||
assert m.is_positive_definite == True | ||
assert m.is_positive_semidefinite == True | ||
assert _is_positive_semidefinite_by_cholesky_factorization_with_pivots(m) | ||
assert _is_positive_semidefinite_by_eigenvalues(m) | ||
assert _is_positive_semidefinite_by_minors(m) | ||
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. There is a trailing whitespace at the end of line 503 which is causing quality tests to fail, please fix that. 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. My bad there. I removed whitespace and updated the flake8 part of setup.cfg to catch it earlier next time. 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. I guess adding whitespace to setup.cfg was a bad idea, since it caused a whole bunch of linting errors in the pipeline. Removed in latest commit. |
||
assert m.is_negative_definite == False | ||
assert m.is_negative_semidefinite == False | ||
assert m.is_indefinite == False | ||
|
||
m = Matrix([[5, 4], [4, 5]]) | ||
assert m.is_positive_definite == True | ||
assert m.is_positive_semidefinite == True | ||
assert _is_positive_semidefinite_by_cholesky_factorization_with_pivots(m) | ||
assert _is_positive_semidefinite_by_eigenvalues(m) | ||
assert _is_positive_semidefinite_by_minors(m) | ||
assert m.is_negative_definite == False | ||
assert m.is_negative_semidefinite == False | ||
assert m.is_indefinite == False | ||
|
@@ -508,13 +519,19 @@ def test_definite(): | |
m = Matrix([[2, -1, -1], [-1, 2, -1], [-1, -1, 2]]) | ||
assert m.is_positive_definite == False | ||
assert m.is_positive_semidefinite == True | ||
assert _is_positive_semidefinite_by_cholesky_factorization_with_pivots(m) | ||
assert _is_positive_semidefinite_by_eigenvalues(m) | ||
assert _is_positive_semidefinite_by_minors(m) | ||
assert m.is_negative_definite == False | ||
assert m.is_negative_semidefinite == False | ||
assert m.is_indefinite == False | ||
|
||
m = Matrix([[1, 2], [2, 4]]) | ||
assert m.is_positive_definite == False | ||
assert m.is_positive_semidefinite == True | ||
assert _is_positive_semidefinite_by_cholesky_factorization_with_pivots(m.T + m) | ||
assert _is_positive_semidefinite_by_eigenvalues(m.T + m) | ||
assert _is_positive_semidefinite_by_minors(m.T + m) | ||
assert m.is_negative_definite == False | ||
assert m.is_negative_semidefinite == False | ||
assert m.is_indefinite == False | ||
|
@@ -524,13 +541,20 @@ def test_definite(): | |
m = Matrix([[2, 3], [4, 8]]) | ||
assert m.is_positive_definite == True | ||
assert m.is_positive_semidefinite == True | ||
assert _is_positive_semidefinite_by_cholesky_factorization_with_pivots(m.T + m) | ||
assert _is_positive_semidefinite_by_eigenvalues(m.T + m) | ||
assert _is_positive_semidefinite_by_minors(m.T + m) | ||
assert m.is_negative_definite == False | ||
assert m.is_negative_semidefinite == False | ||
assert m.is_indefinite == False | ||
|
||
# Hermetian matrices | ||
m = Matrix([[1, 2*I], [-I, 4]]) | ||
assert m.is_positive_definite == True | ||
assert m.is_positive_semidefinite == True | ||
assert _is_positive_semidefinite_by_cholesky_factorization_with_pivots(m.H + m) | ||
assert _is_positive_semidefinite_by_eigenvalues(m.H + m) | ||
assert _is_positive_semidefinite_by_minors(m.H + m) | ||
assert m.is_negative_definite == False | ||
assert m.is_negative_semidefinite == False | ||
assert m.is_indefinite == False | ||
|
@@ -541,20 +565,29 @@ def test_definite(): | |
m = Matrix([[a, 0, 0], [0, a, 0], [0, 0, a]]) | ||
assert m.is_positive_definite == True | ||
assert m.is_positive_semidefinite == True | ||
assert _is_positive_semidefinite_by_cholesky_factorization_with_pivots(m) | ||
assert _is_positive_semidefinite_by_eigenvalues(m) | ||
assert _is_positive_semidefinite_by_minors(m) | ||
assert m.is_negative_definite == False | ||
assert m.is_negative_semidefinite == False | ||
assert m.is_indefinite == False | ||
|
||
m = Matrix([[b, 0, 0], [0, b, 0], [0, 0, b]]) | ||
assert m.is_positive_definite == False | ||
assert m.is_positive_semidefinite == False | ||
assert not _is_positive_semidefinite_by_cholesky_factorization_with_pivots(m) | ||
assert not _is_positive_semidefinite_by_eigenvalues(m) | ||
assert not _is_positive_semidefinite_by_minors(m) | ||
assert m.is_negative_definite == True | ||
assert m.is_negative_semidefinite == True | ||
assert m.is_indefinite == False | ||
|
||
m = Matrix([[a, 0], [0, b]]) | ||
assert m.is_positive_definite == False | ||
assert m.is_positive_semidefinite == False | ||
assert not _is_positive_semidefinite_by_cholesky_factorization_with_pivots(m) | ||
assert not _is_positive_semidefinite_by_eigenvalues(m) | ||
assert not _is_positive_semidefinite_by_minors(m) | ||
assert m.is_negative_definite == False | ||
assert m.is_negative_semidefinite == False | ||
assert m.is_indefinite == True | ||
|
@@ -571,6 +604,9 @@ def test_definite(): | |
]) | ||
assert m.is_positive_definite == True | ||
assert m.is_positive_semidefinite == True | ||
assert _is_positive_semidefinite_by_cholesky_factorization_with_pivots(m.T + m) | ||
assert _is_positive_semidefinite_by_eigenvalues(m.T + m) | ||
assert _is_positive_semidefinite_by_minors(m.T + m) | ||
assert m.is_indefinite == False | ||
|
||
# test for issue 19547: https://github.com/sympy/sympy/issues/19547 | ||
|
@@ -581,3 +617,7 @@ def test_definite(): | |
]) | ||
assert not m.is_positive_definite | ||
assert not m.is_positive_semidefinite | ||
assert not _is_positive_semidefinite_by_cholesky_factorization_with_pivots(m) | ||
assert not _is_positive_semidefinite_by_eigenvalues(m) | ||
assert not _is_positive_semidefinite_by_minors(m) | ||
|
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.
These are too long names that can easily exceed 80 characters.
What about making them shorter like
_is_positive_semidefinite_cholesky
.I don't think that it should need much mathematical meaning, especially if these functions are only used internaly.
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.
I agree they are a bit verbose. I tend to err on the side of more specificity in my variable names and comments. I shortened them.