diff --git a/Eigen/src/Core/Product.h b/Eigen/src/Core/Product.h index de356c6db..e370cd4fb 100644 --- a/Eigen/src/Core/Product.h +++ b/Eigen/src/Core/Product.h @@ -194,7 +194,9 @@ template class Product inline Product(const Lhs& lhs, const Rhs& rhs) : m_lhs(lhs), m_rhs(rhs) { - ei_assert(lhs.cols() == rhs.rows()); + ei_assert(lhs.cols() == rhs.rows() + && "invalid matrix product" + && "if you wanted a coeff-wise or a dot product use the respective explicit functions"); } /** \internal @@ -265,6 +267,21 @@ template inline const typename ProductReturnType::Type MatrixBase::operator*(const MatrixBase &other) const { + enum { + ProductIsValid = Derived::ColsAtCompileTime==Dynamic + || OtherDerived::RowsAtCompileTime==Dynamic + || int(Derived::ColsAtCompileTime)==int(OtherDerived::RowsAtCompileTime), + AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime, + SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived,OtherDerived) + }; + // note to the lost user: + // * for a dot product use: v1.dot(v2) + // * for a coeff-wise product use: v1.cwise()*v2 + EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes), + invalid_vector_vector_product__if_you_wanted_a_dot_or_coeff_wise_product_you_must_use_the_explicit_functions); + EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors), + invalid_matrix_product__if_you_wanted_a_coeff_wise_product_you_must_use_the_explicit_function); + EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, invalid_matrix_product); return typename ProductReturnType::Type(derived(), other.derived()); } diff --git a/Eigen/src/Core/util/StaticAssert.h b/Eigen/src/Core/util/StaticAssert.h index ad46bde46..fbaf676de 100644 --- a/Eigen/src/Core/util/StaticAssert.h +++ b/Eigen/src/Core/util/StaticAssert.h @@ -66,7 +66,10 @@ scalar_type_must_be_floating_point, default_writting_to_selfadjoint_not_supported, writting_to_triangular_part_with_unit_diag_is_not_supported, - this_method_is_only_for_fixed_size + this_method_is_only_for_fixed_size, + invalid_matrix_product, + invalid_vector_vector_product__if_you_wanted_a_dot_or_coeff_wise_product_you_must_use_the_explicit_functions, + invalid_matrix_product__if_you_wanted_a_coeff_wise_product_you_must_use_the_explicit_function }; }; @@ -110,15 +113,18 @@ || int(TYPE0::SizeAtCompileTime)==int(TYPE1::SizeAtCompileTime)),\ you_mixed_vectors_of_different_sizes) -// static assertion failing if the two matrix expression types are not compatible (same fixed-size or dynamic size) -#define EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(TYPE0,TYPE1) \ - EIGEN_STATIC_ASSERT( \ - ((int(TYPE0::RowsAtCompileTime)==Eigen::Dynamic \ +#define EIGEN_PREDICATE_SAME_MATRIX_SIZE(TYPE0,TYPE1) \ + ((int(TYPE0::RowsAtCompileTime)==Eigen::Dynamic \ || int(TYPE1::RowsAtCompileTime)==Eigen::Dynamic \ || int(TYPE0::RowsAtCompileTime)==int(TYPE1::RowsAtCompileTime)) \ && (int(TYPE0::ColsAtCompileTime)==Eigen::Dynamic \ || int(TYPE1::ColsAtCompileTime)==Eigen::Dynamic \ - || int(TYPE0::ColsAtCompileTime)==int(TYPE1::ColsAtCompileTime))),\ + || int(TYPE0::ColsAtCompileTime)==int(TYPE1::ColsAtCompileTime))) + +// static assertion failing if the two matrix expression types are not compatible (same fixed-size or dynamic size) +#define EIGEN_STATIC_ASSERT_SAME_MATRIX_SIZE(TYPE0,TYPE1) \ + EIGEN_STATIC_ASSERT( \ + EIGEN_PREDICATE_SAME_MATRIX_SIZE(TYPE0,TYPE1),\ you_mixed_matrices_of_different_sizes) #endif // EIGEN_STATIC_ASSERT_H diff --git a/test/product.h b/test/product.h index 5ca56d995..62cbdee92 100644 --- a/test/product.h +++ b/test/product.h @@ -101,7 +101,7 @@ template void product(const MatrixType& m) VERIFY_IS_APPROX(MatrixType::Identity(rows, cols)(r,c), static_cast(r==c)); if (rows!=cols) - VERIFY_RAISES_ASSERT(m3 = m1*m1); + VERIFY_RAISES_ASSERT(m3 = m1*m1); // test the previous tests were not screwed up because operator* returns 0 // (we use the more accurate default epsilon) diff --git a/test/product_small.cpp b/test/product_small.cpp index a1ff642e5..bc1a774b3 100644 --- a/test/product_small.cpp +++ b/test/product_small.cpp @@ -22,6 +22,7 @@ // License and a copy of the GNU General Public License along with // Eigen. If not, see . +#define EIGEN_NO_STATIC_ASSERT #include "product.h" void test_product_small()