Skip to content

Commit

Permalink
added check for matrix positive definiteness
Browse files Browse the repository at this point in the history
  • Loading branch information
bobmyhill committed Mar 24, 2024
1 parent ddf58d6 commit d1c7081
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions burnman/utils/math.py
Original file line number Diff line number Diff line change
Expand Up @@ -553,3 +553,16 @@ def generate_complete_basis(incomplete_basis, array):
)

return complete_basis.round(decimals=12) + 0.0


def is_positive_definite(matrix):
"""
Checks if a matrix is positive definite
:param matrix: Input matrix
:type matrix: 2D numpy array
:return: Whether or not the matrix is positive definite
:rtype: bool
"""
return np.all(np.linalg.eigvals(matrix) > 0)
5 changes: 5 additions & 0 deletions tests/test_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from burnman.utils.math import smooth_array
from burnman.utils.math import interp_smoothed_array_and_derivatives
from burnman.utils.math import _pad_ndarray_inverse_mirror
from burnman.utils.math import is_positive_definite


class test_tools(BurnManTest):
Expand Down Expand Up @@ -200,6 +201,10 @@ def test_reactions_from_formulae(self):
)
self.assertTrue(R[0] == "2 fa = 2 fper + 1 fs")

def test_positive_definite(self):
arr = np.array([[2.0, 0.0], [0.0, 1.0]])
self.assertTrue(is_positive_definite(arr))


if __name__ == "__main__":
unittest.main()

0 comments on commit d1c7081

Please sign in to comment.