Skip to content

Commit

Permalink
feat: safe inverse for dynamic matrices (acts-project#3945)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->

## Summary by CodeRabbit

- **New Features**
	- Enhanced the `safeInverse` function to support dynamic matrices in addition to fixed-size matrices.

- **Tests**
	- Added a new test case for the `safeInverse` function to validate its behavior with dynamically sized identity matrices.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
AJPfleger authored Dec 5, 2024
1 parent b3933b0 commit 3390f50
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
5 changes: 2 additions & 3 deletions Core/include/Acts/Utilities/AlgebraHelpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ inline ActsMatrix<A::RowsAtCompileTime, B::ColsAtCompileTime> blockedMult(
/// Calculate the inverse of an Eigen matrix after checking if it can be
/// numerically inverted. This allows to catch potential FPEs before they occur.
/// For matrices up to 4x4, the inverse is computed directly. For larger
/// matrices, the FullPivLU is used.
/// matrices, and dynamic matrices the FullPivLU is used.
///
/// @tparam Derived Eigen derived concrete type
/// @tparam Result Eigen result type defaulted to input type
Expand All @@ -192,12 +192,11 @@ std::optional<ResultType> safeInverse(const MatrixType& m) noexcept {
constexpr int cols = MatrixType::ColsAtCompileTime;

static_assert(rows == cols);
static_assert(rows != -1);

ResultType result;
bool invertible = false;

if constexpr (rows > 4) {
if constexpr (rows > 4 || rows == -1) {
Eigen::FullPivLU<MatrixType> mFullPivLU(m);
if (mFullPivLU.isInvertible()) {
invertible = true;
Expand Down
16 changes: 9 additions & 7 deletions Tests/UnitTests/Core/Utilities/AlgebraHelpersTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ BOOST_AUTO_TEST_CASE(safeInverseLargeMatrix) {
BOOST_CHECK_EQUAL(*identityInv, identity);
}

BOOST_AUTO_TEST_CASE(safeInverseDynamicMatrix) {
Eigen::MatrixXd identity{Eigen::MatrixXd::Identity(2, 2)};

auto identityInv = Acts::safeInverse(identity);

BOOST_CHECK(identityInv);
BOOST_CHECK_EQUAL(*identityInv, identity);
}

BOOST_AUTO_TEST_CASE(SafeInverseBadSmallMatrix) {
Eigen::Matrix<double, 2, 2> m;
m << 1, 1, 2, 2;
Expand Down Expand Up @@ -111,13 +120,6 @@ BOOST_AUTO_TEST_CASE(SafeInverseFPELargeMatrix) {
// auto mInv = Acts::safeInverse(m);
// }

/// This test should not compile
// BOOST_AUTO_TEST_CASE(SafeInverseDynamicMatrix) {
// Eigen::MatrixXd m{Eigen::MatrixXd::Identity(2, 2)};
//
// auto mInv = Acts::safeInverse(m);
// }

BOOST_AUTO_TEST_SUITE_END()

BOOST_AUTO_TEST_SUITE(SafeExp)
Expand Down

0 comments on commit 3390f50

Please sign in to comment.