1610
2018-10-11 10:40:16 +0000
Can't convert product of 3 1x1 matrices to scalar
2019-12-04 18:01:13 +0000
1
1
1
Unclassified
Eigen
Core - matrix products
3.3 (current stable)
All
All
DECISIONNEEDED
Normal
API Change
---
1608
1
b7.10110111
eigen.nobody
chtz
gael.guennebaud
jacob.benoit.1
oldest_to_newest
7571
0
b7.10110111
2018-10-11 10:40:16 +0000
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.
7576
1
chtz
2018-10-12 15:55:31 +0000
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).
7578
2
gael.guennebaud
2018-10-13 18:59:15 +0000
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.
10054
3
eigen.nobody
2019-12-04 18:01:13 +0000
-- 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.