This bugzilla service is closed. All entries have been migrated to
Bug 610 - Unsupported operations like "Matrix3d::Identity() op vector3d.asDiagonal()"
Summary: Unsupported operations like "Matrix3d::Identity() op vector3d.asDiagonal()"
Alias: None
Product: Eigen
Classification: Unclassified
Component: Core - general (show other bugs)
Version: 3.4 (development)
Hardware: All All
: Normal Feature Request
Assignee: Nobody
Depends on: 112 520 ExprEval
Blocks: 3.4
  Show dependency treegraph
Reported: 2013-06-06 16:19 UTC by Rhys Ulerich
Modified: 2019-12-04 12:22 UTC (History)
4 users (show)


Description Rhys Ulerich 2013-06-06 16:19:14 UTC
As discussed in
the following does not compile but should...

#include <Eigen/Core>

int main()
    using namespace Eigen;

    // Works as expected
    Matrix3d A1;
    Matrix3d B1 = Matrix3d::Identity() * A1;
    Matrix3d C1 = Matrix3d::Identity() + A1;
    Matrix3d D1 = Matrix3d::Identity() - A1;

    // Does not work but I expect that it should
    Vector3d A2;
    Matrix3d B2 = Matrix3d::Identity() * A2.asDiagonal();
    Matrix3d C2 = Matrix3d::Identity() + A2.asDiagonal();
    Matrix3d D2 = Matrix3d::Identity() - A2.asDiagonal();

    return 0;
Comment 1 Christoph Hertzberg 2014-03-04 16:06:48 UTC
Apparently, the multiplication works meanwhile.
I think this is critical enough to block 3.3 and should be fixable using ExprEval (bug 99).
Comment 2 Sergiu Deitsch 2017-04-22 18:30:17 UTC
Any progress on this? Addition/subtraction is still broken in 3.3.
Comment 3 Gael Guennebaud 2018-10-10 21:23:34 UTC
Regarding this issue, do we want to support "diagmat +/- diagmat" only or more general "diagmat +/- dense" operations?

The former is easy and can be specialized for Dense::Identity() since it is also a diagonal matrix.

Adding/subtracting dense and diagonal matrices would be very inefficient except for some simple assignments like:

(1)  res = diagmat + dense;
(2)  res -= dense - diagmat;

for which the rewriting rules I've written for mixing sparse and dense would equally apply here, see: 

However, it would be too cumbersome to allow for "res = diagmat + dense" but not for "diagmat + dense" within more general expression.

If we want to support "diagmat + dense" within general expressions, the best approach I can think of would be to evaluate it into a temporary with the same strategy as for expression (1) above:
  tmp = dense;
Comment 4 Christoph Hertzberg 2019-03-15 16:06:12 UTC
It would of course be awesome, if expressions like:

   w = (A+s*Matrix::Identity())*v;

would evaluate to something like
   w = s*v; w += A*v;

But rewriting expressions like:

   w = (s0*A + s1*diag1 + s2*diag2) * v;

to the following can get arbitrarily complicated:

   w = (s1*diag1 + s2*diag2) * v; // element-wise, can happen on-the-fly
   w += s0*A*v;                   // GEMV-product

So I guess, for now it should be sufficient to make them compile (and get correct results), if necessary with temporaries.
Comment 5 Nobody 2019-12-04 12:22:16 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to'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:

Note You need to log in before you can comment on or make changes to this bug.