Re: [eigen] How to subtract a diagonal matrix from a matrix in one expression?
• To: eigen@xxxxxxxxxxxxxxxxxxx
• Subject: Re: [eigen] How to subtract a diagonal matrix from a matrix in one expression?
• Date: Thu, 12 Jan 2017 01:51:49 +0900
• Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=vQ+TPj18CWD+5BytSgVdYFPfwflj89WHrD6MyxQfZzc=; b=hw+TmIya7gxISe7kgFPFuFMcahnBrK4e7fXTn46rEBGGcf+JQngNhhp7Cm1EDSdTkB Qc0+1AsuAMk5T2dS2J0zYGq/enEfwvBFmdZ52Zv1xeGYP9ZH/VpaB7+L5veu8K8Z3Tyn 3ZZV+5+YYkeAzUSB4QUwED6cfOd2I3NiBx7ECf15hH8YM5KLbZIMqB2+71xYiLD4jD0S 7GmWdQyEjn7xLsuyILdY0wdvpuJhF05yakET/y79a41zUSdEwMqyuIY1w3KwJZo2fju9 0sSR9MgPlBFM1dBMwCoYbxBFqMVQvY5qIX7NxAITf1MtOEPjzvfJ6r3iCrejW5lymQNJ X6Sw==

Dear Alexander,

if you are using fixed size matrices, I don't see any problem with having temporary objects as they don't induce any memory allocation.
The following one-liner ther work:
const Matrix2d M2 = M - Matrix2d(v.asDiagonal());
and I guess the compiler can be quite smart to avoid any overhead.

A a side remarks, be very mindful of the auto keyword with Eigen expressions.
If you do
const auto M2 = M - Matrix2d(v.asDiagonal());
M2 will be a CwiseBinaryOp, not a Matrix2d, meaning that you subtraction will be recomputed each time you are using M2.

Best regards,

On Thu, Jan 12, 2017 at 12:44 AM, Alexander Voigt wrote:
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@xxxxxxxxxxx-aachen.de
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

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