From 8a06c699d0f8d47f4fc4f259a21574b4719792b2 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Tue, 29 Jan 2019 10:27:13 +0100 Subject: bug #1669: fix PartialPivLU/inverse with zero-sized matrices. --- Eigen/src/LU/PartialPivLU.h | 5 ++++- test/inverse.cpp | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/Eigen/src/LU/PartialPivLU.h b/Eigen/src/LU/PartialPivLU.h index 8726bf895..b414b5c46 100644 --- a/Eigen/src/LU/PartialPivLU.h +++ b/Eigen/src/LU/PartialPivLU.h @@ -511,7 +511,10 @@ void PartialPivLU::compute() // the row permutation is stored as int indices, so just to be sure: eigen_assert(m_lu.rows()::highest()); - m_l1_norm = m_lu.cwiseAbs().colwise().sum().maxCoeff(); + if(m_lu.cols()>0) + m_l1_norm = m_lu.cwiseAbs().colwise().sum().maxCoeff(); + else + m_l1_norm = RealScalar(0); eigen_assert(m_lu.rows() == m_lu.cols() && "PartialPivLU is only for square (and moreover invertible) matrices"); const Index size = m_lu.rows(); diff --git a/test/inverse.cpp b/test/inverse.cpp index 8754cb7e5..99f9e0c9b 100644 --- a/test/inverse.cpp +++ b/test/inverse.cpp @@ -105,6 +105,22 @@ template void inverse(const MatrixType& m) } } +template +void inverse_zerosized() +{ + Matrix A(0,0); + { + Matrix b, x; + x = A.inverse() * b; + } + { + Matrix b(0,1), x; + x = A.inverse() * b; + VERIFY_IS_EQUAL(x.rows(), 0); + VERIFY_IS_EQUAL(x.cols(), 1); + } +} + EIGEN_DECLARE_TEST(inverse) { int s = 0; @@ -118,6 +134,7 @@ EIGEN_DECLARE_TEST(inverse) s = internal::random(50,320); CALL_SUBTEST_5( inverse(MatrixXf(s,s)) ); TEST_SET_BUT_UNUSED_VARIABLE(s) + CALL_SUBTEST_5( inverse_zerosized() ); s = internal::random(25,100); CALL_SUBTEST_6( inverse(MatrixXcd(s,s)) ); -- cgit v1.2.3