Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #199 from jhlegarreta/AddPositiveDefiniteCheckUtil
Browse files Browse the repository at this point in the history
ENH: Add positive definite matrix check util method
  • Loading branch information
jhlegarreta authored May 30, 2024
2 parents 356431a + be638e5 commit cc681d5
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/developers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Information on specific functions, classes, and methods.
api/eddymotion.data
api/eddymotion.data.dmri
api/eddymotion.estimator
api/eddymotion.math
api/eddymotion.model
api/eddymotion.utils
api/eddymotion.viz
Empty file added src/eddymotion/math/__init__.py
Empty file.
Empty file.
33 changes: 33 additions & 0 deletions src/eddymotion/math/tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
#
# Copyright 2024 The NiPreps Developers <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# We support and encourage derived works from this project, please read
# about our expectations at
#
# https://www.nipreps.org/community/licensing/
#
import numpy as np

from eddymotion.math.utils import is_positive_definite


def test_is_positive_definite():
matrix = np.array([[4, 1, 2], [1, 3, 1], [2, 1, 5]])
assert is_positive_definite(matrix)

matrix = np.array([[4, 1, 2], [1, -3, 1], [2, 1, 5]])
assert not is_positive_definite(matrix)
45 changes: 45 additions & 0 deletions src/eddymotion/math/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
#
# Copyright 2024 The NiPreps Developers <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY kIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# We support and encourage derived works from this project, please read
# about our expectations at
#
# https://www.nipreps.org/community/licensing/
#
import numpy as np


def is_positive_definite(matrix):
"""Check whether the given matrix is positive definite. Any positive
definite matrix can be decomposed as the product of a lower triangular
matrix and its conjugate transpose by performing the Cholesky decomposition.
Parameters
----------
matrix : np.ndarray
The matrix to check.
Returns
-------
True is the matrix is positive definite; False otherwise
"""

try:
# Attempt Cholesky decomposition
np.linalg.cholesky(matrix)
return True
except np.linalg.LinAlgError:
# Matrix is not positive definite
return False

0 comments on commit cc681d5

Please sign in to comment.