This bugzilla service is closed. All entries have been migrated to https://gitlab.com/libeigen/eigen

Bug 1610

Summary: Can't convert product of 3 1x1 matrices to scalar
Product: Eigen Reporter: Ruslan <b7.10110111>
Component: Core - matrix productsAssignee: Nobody <eigen.nobody>
Status: DECISIONNEEDED ---    
Severity: API Change CC: chtz, gael.guennebaud, jacob.benoit.1
Priority: Normal    
Version: 3.3 (current stable)   
Hardware: All   
OS: All   
Whiteboard:
Bug Depends on:    
Bug Blocks: 1608    

Description Ruslan 2018-10-11 10:40:16 UTC
The following test compiles successfully with Eigen 3.2.92, but fails in 3.3.4 and 3.3.5:

#include <Eigen/Dense>
int main()
{
    const Eigen::Matrix<double,1,1> a(1), b(2), c(3);
    const double prod1 = a*b;
    const double prod2 = a*b*c;
}

The error given by g++ 7.3.0-27ubuntu1~18.04 is as follows:

test.cpp:6:30: error: cannot convert ‘Eigen::internal::enable_if<true, const Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<double, double>, const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, const Eigen::Matrix<double, 1, 1, 0, 1, 1> >, const Eigen::Matrix<double, 1, 1, 0, 1, 1> > >::type {aka const Eigen::CwiseBinaryOp<Eigen::internal::scalar_product_op<double, double>, const Eigen::CwiseNullaryOp<Eigen::internal::scalar_constant_op<double>, const Eigen::Matrix<double, 1, 1, 0, 1, 1> >, const Eigen::Matrix<double, 1, 1, 0, 1, 1> >}’ to ‘const double’ in initialization
     const double prod2 = a*b*c;

Notice that the prod1 line compiles successfully, unlike the prod2 one.
The workaround is to explicitly take (0,0) element instead of relying on automatic conversion.
Comment 1 Christoph Hertzberg 2018-10-12 15:55:31 UTC
I'm not sure, if we actually want to fix this -- we also don't allow assigning "products with one factor" (i.e., 1x1 matrices) to a scalar.

Assigning products 1xN * Nx1 directly to a scalar already is a hack, which usually is better expressed by using a `.dot()` product, IMO, i.e.:

   a.adjoint().dot(b*c);

This of course also works if the inner dimensions of the products are not 1.

I would actually prefer to deprecate the auto-conversion (and remove it at some point).
Comment 2 Gael Guennebaud 2018-10-13 18:59:15 UTC
Allowing any 1x1 expression to be convertible to a Scalar is not possible. I've tried that a while back and this yield to countless ambiguous calls. This is why we decided to enable it for inner products only. This is especially useful when dealing for complexes as it makes which operands gets conjugated explicit.

Regarding this precise issue, that is:

 some_inner_prod * 1x1_mat

the problem is that the compiler prefers to convert the lhs to a scalar instead of calling the matrix-product operator*. I tried to add more specialized overload of operator* to either Product<> or MatrixBase<>, but at bet I get an ambiguous call error. 

One possibility would be to have a single generic operator* for MatrixBase that would return a scalar multiple or matrix product depending on the argument. But that sounds overkill.
Comment 3 Nobody 2019-12-04 18:01:13 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to gitlab.com's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: https://gitlab.com/libeigen/eigen/issues/1610.