From f1f70ceb8487f2b1c32d1cdba5be7c824bda8dca Mon Sep 17 00:00:00 2001 From: Jitse Niesen Date: Wed, 18 Apr 2012 15:16:05 +0100 Subject: [PATCH] Fix infinite recursion in ProductBase::coeff() (bug #447) Triggered by product of dynamic-size 1 x n and n x 1 matrices. Also, add regression test. (transplanted from 77a5a2b28cb89bca74bdf5936dafb306af6be162) --- Eigen/src/Core/ProductBase.h | 6 ++++-- test/product_small.cpp | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Eigen/src/Core/ProductBase.h b/Eigen/src/Core/ProductBase.h index 233ed6467..91975880f 100644 --- a/Eigen/src/Core/ProductBase.h +++ b/Eigen/src/Core/ProductBase.h @@ -152,7 +152,8 @@ class ProductBase : public MatrixBase #else EIGEN_STATIC_ASSERT_SIZE_1x1(Derived) eigen_assert(this->rows() == 1 && this->cols() == 1); - return derived().coeff(row,col); + Matrix result = *this; + return result.coeff(row,col); #endif } @@ -160,7 +161,8 @@ class ProductBase : public MatrixBase { EIGEN_STATIC_ASSERT_SIZE_1x1(Derived) eigen_assert(this->rows() == 1 && this->cols() == 1); - return derived().coeff(i); + Matrix result = *this; + return result.coeff(i); } const Scalar& coeffRef(Index row, Index col) const diff --git a/test/product_small.cpp b/test/product_small.cpp index d7f1c09ff..cf430a2d3 100644 --- a/test/product_small.cpp +++ b/test/product_small.cpp @@ -25,6 +25,25 @@ #define EIGEN_NO_STATIC_ASSERT #include "product.h" +// regression test for bug 447 +void product1x1() +{ + Matrix matAstatic; + Matrix matBstatic; + matAstatic.setRandom(); + matBstatic.setRandom(); + VERIFY_IS_APPROX( (matAstatic * matBstatic).coeff(0,0), + matAstatic.cwiseProduct(matBstatic.transpose()).sum() ); + + MatrixXf matAdynamic(1,3); + MatrixXf matBdynamic(3,1); + matAdynamic.setRandom(); + matBdynamic.setRandom(); + VERIFY_IS_APPROX( (matAdynamic * matBdynamic).coeff(0,0), + matAdynamic.cwiseProduct(matBdynamic.transpose()).sum() ); +} + + void test_product_small() { for(int i = 0; i < g_repeat; i++) { @@ -33,6 +52,7 @@ void test_product_small() CALL_SUBTEST_3( product(Matrix3d()) ); CALL_SUBTEST_4( product(Matrix4d()) ); CALL_SUBTEST_5( product(Matrix4f()) ); + CALL_SUBTEST_6( product1x1() ); } #ifdef EIGEN_TEST_PART_6