From b2814d53a707f699e5c56f565847e7020654efc2 Mon Sep 17 00:00:00 2001 From: Antonio Sanchez Date: Tue, 16 Jan 2024 08:50:26 -0800 Subject: [PATCH] Fix stableNorm when input is zero-sized. --- Eigen/src/Core/StableNorm.h | 4 ++++ test/stable_norm.cpp | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/Eigen/src/Core/StableNorm.h b/Eigen/src/Core/StableNorm.h index 1ec71403c..be80b7fac 100644 --- a/Eigen/src/Core/StableNorm.h +++ b/Eigen/src/Core/StableNorm.h @@ -55,6 +55,10 @@ typename MatrixType::RealScalar stable_norm_impl(const MatrixType& mat) { RealScalar invScale(1); RealScalar ssq(0); // sum of squares + if (mat.size() == 0) { + return RealScalar(0); + } + stable_norm_kernel(mat, ssq, scale, invScale); return scale * sqrt(ssq); } diff --git a/test/stable_norm.cpp b/test/stable_norm.cpp index c16e44d8d..e4c13f761 100644 --- a/test/stable_norm.cpp +++ b/test/stable_norm.cpp @@ -209,6 +209,11 @@ void stable_norm(const MatrixType& m) { } } +void test_empty() { + Eigen::VectorXf empty(0); + VERIFY_IS_EQUAL(empty.stableNorm(), 0.0f); +} + template void test_hypot() { typedef typename NumTraits::Real RealScalar; @@ -235,6 +240,8 @@ void test_hypot() { } EIGEN_DECLARE_TEST(stable_norm) { + CALL_SUBTEST_1(test_empty()); + for (int i = 0; i < g_repeat; i++) { CALL_SUBTEST_3(test_hypot()); CALL_SUBTEST_4(test_hypot());