Fix computeInverseAndDetWithCheck for dynamic result matrices

libeigen/eigen!2312

Closes #2917
This commit is contained in:
Pavel Guzenfeld
2026-03-21 15:38:27 +00:00
committed by Rasmus Munk Larsen
parent cc8c7cf0e6
commit 1d21d62fbc
2 changed files with 28 additions and 3 deletions

View File

@@ -308,8 +308,9 @@ inline void MatrixBase<Derived>::computeInverseAndDetWithCheck(ResultType& inver
typename ResultType::Scalar& determinant,
bool& invertible,
const RealScalar& absDeterminantThreshold) const {
// i'd love to put some static assertions there, but SFINAE means that they have no effect...
EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(Derived, ResultType)
eigen_assert(rows() == cols());
inverse.resize(rows(), cols());
// for 2x2, it's worth giving a chance to avoid evaluating.
// for larger sizes, evaluating has negligible cost and limits code size.
typedef std::conditional_t<RowsAtCompileTime == 2,
@@ -343,8 +344,6 @@ template <typename ResultType>
inline void MatrixBase<Derived>::computeInverseWithCheck(ResultType& inverse, bool& invertible,
const RealScalar& absDeterminantThreshold) const {
Scalar determinant;
// i'd love to put some static assertions there, but SFINAE means that they have no effect...
eigen_assert(rows() == cols());
computeInverseAndDetWithCheck(inverse, determinant, invertible, absDeterminantThreshold);
}

View File

@@ -58,6 +58,32 @@ void inverse_for_fixed_size(const MatrixType& m1, std::enable_if_t<MatrixType::S
VERIFY_IS_APPROX((m5.template topLeftCorner<MatrixType::RowsAtCompileTime, MatrixType::ColsAtCompileTime>()),
m2.inverse());
}
// check that computeInverseAndDetWithCheck resizes dynamic result matrix (issue #2917)
{
typedef Matrix<Scalar, Dynamic, Dynamic> DynamicMatrix;
DynamicMatrix m_dyn_inv;
Scalar det_dyn;
bool inv_dyn;
m1.computeInverseAndDetWithCheck(m_dyn_inv, det_dyn, inv_dyn);
VERIFY(inv_dyn);
VERIFY_IS_EQUAL(m_dyn_inv.rows(), m1.rows());
VERIFY_IS_EQUAL(m_dyn_inv.cols(), m1.cols());
VERIFY_IS_APPROX(identity, m1 * m_dyn_inv);
VERIFY_IS_APPROX(det_dyn, m1.determinant());
}
// check that computeInverseWithCheck resizes dynamic result matrix
{
typedef Matrix<Scalar, Dynamic, Dynamic> DynamicMatrix;
DynamicMatrix m_dyn_inv;
bool inv_dyn;
m1.computeInverseWithCheck(m_dyn_inv, inv_dyn);
VERIFY(inv_dyn);
VERIFY_IS_EQUAL(m_dyn_inv.rows(), m1.rows());
VERIFY_IS_EQUAL(m_dyn_inv.cols(), m1.cols());
VERIFY_IS_APPROX(identity, m1 * m_dyn_inv);
}
}
template <typename MatrixType>