From d1c708150837c734ca22e527c24953be85e2ed74 Mon Sep 17 00:00:00 2001 From: Bob Myhill Date: Sun, 24 Mar 2024 20:17:20 +0000 Subject: [PATCH] added check for matrix positive definiteness --- burnman/utils/math.py | 13 +++++++++++++ tests/test_tools.py | 5 +++++ 2 files changed, 18 insertions(+) diff --git a/burnman/utils/math.py b/burnman/utils/math.py index 1538869e2..3a9a258f7 100644 --- a/burnman/utils/math.py +++ b/burnman/utils/math.py @@ -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) diff --git a/tests/test_tools.py b/tests/test_tools.py index 61b2c4d4f..6b4bab86d 100644 --- a/tests/test_tools.py +++ b/tests/test_tools.py @@ -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): @@ -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()