This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Failure to cast to scalar

Tags: None
(comma "," separated)
User avatar
alecjacobson
Registered Member
Posts
26
Karma
0

Failure to cast to scalar

Tue Mar 17, 2015 3:13 pm
Not sure if this is a bug, but it's at least surprising that it doesn't compile (with gcc or clang):

Code: Select all
Eigen::Vector3d X;           
Eigen::Matrix3d Y;           
1+(Y.row(0)*X)/(Y.row(0)*X);


This does compile:
Code: Select all
Eigen::Vector3d X;           
Eigen::Matrix3d Y;           
1+(Y.row(0)*X);
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Failure to cast to scalar  Topic is solved

Wed Mar 18, 2015 8:40 am
Indeed, what happens here is that Y.row(0)*X returns a 1x1 matrix expression having a conversion operator to a double. This is why "1+(Y.row(0)*X)" does compile. The following expression does compile too:
Code: Select all
(Y.row(0)*X)/(Y.row(0)*X);

by casting the right hand side to a double. The type of this sub-expression is something like "division_by_scalar<inner_product<row<matrix>,vector> >".

However, this conversion operator is only available for inner-products. Therefore the complete sub-expression cannot be casted to a double to add "1".

I agree this is rather weird, but there is not much we can do on our side because allowing implicit conversion to a scalar for any 1x1 expression yields much weirder side effects (we already tried). Making inner-products returning a scalar instead of 1x1 expression is also worse.

Workarounds are numerous, like using Y.row(0).dot(X), or (Y.row(0)*X).value(), or (Y.row(0)*X)(0)...


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]