[eigen] How to subtract a diagonal matrix from a matrix in one expression?

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Dear Eigen developers,

is there a way to subtract a diagonal matrix (which has been constructed
from a vector v using v.asDiagonal()) from a square matrix in one
expression?  (I'm asking for one expression, because I'd like to make
the result const and avoid a temporary.)

Example:

=====================================================
include <Eigen/Core>
using namespace Eigen;

int main() {
   Matrix<double,2,2> M;
   Matrix<double,2,1> v;

   const auto M2 = M - v.asDiagonal(); // does not compile
}
=====================================================

The marked line yields a compiler error with g++ 4.9.2.
I know that it is possible to write instead:

=====================================================
   Matrix<double,2,2> M2(M);
   M2 -= v.asDiagonal();     // works
=====================================================

However, this latter approach has the disadvantage that M2 cannot be
made const, which I'd like to achieve.  An approach that works with one
expression would be

=====================================================
   const Matrix<double,2,2> M2 = [&M,&v]{
      Matrix<double,2,2> tmp(M);
      tmp -= v.asDiagonal();
      return tmp;
   }();
=====================================================

but this has the disadvantage that it is more complicated and needs a
temporary object.

Many thanks and best regards,
Alex

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Dr. Alexander Voigt
Institute for Theoretical Particle Physics and Cosmology
RWTH Aachen
Sommerfeldstr. 14
52074 Aachen
Room: 28A408
Phone: +49 (0)241 80 27049
Fax  : +49 (0)241 80 22187
E-Mail: alexander.voigt@xxxxxxxxxxxxxxxxxxxxx
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/