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

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

*To*: eigen@xxxxxxxxxxxxxxxxxxx*Subject*: Re: [eigen] How to subtract a diagonal matrix from a matrix in one expression?*From*: Christoph Hertzberg <chtz@xxxxxxxxxxxxxxxxxxxxxxxx>*Date*: Thu, 12 Jan 2017 16:50:42 +0100

On 2017-01-11 17:51, Adrien Escande wrote:

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.

void foo(Matrix2d& M2, const Matrix2d& M, const Vector2d& v) { M2 = M - Matrix2d(v.asDiagonal()); } I'm getting something like this when compiling it with -O2: pxor %xmm0, %xmm0 movaps %xmm0, (%rsp) movaps %xmm0, 16(%rsp) movsd (%rdx), %xmm0 movsd %xmm0, (%rsp) movsd 8(%rdx), %xmm0 movsd %xmm0, 24(%rsp) movapd (%rsi), %xmm0 subpd (%rsp), %xmm0 movaps %xmm0, (%rdi) movapd 16(%rsi), %xmm0 subpd 16(%rsp), %xmm0 movaps %xmm0, 16(%rdi) That means there is an actual temporary allocated (on the stack).

movsd (%rsi), %xmm0 movsd 8(%rdx), %xmm1 subsd (%rdx), %xmm0 movsd %xmm0, (%rdi) movsd 8(%rsi), %xmm0 movsd %xmm0, 8(%rdi) movsd 16(%rsi), %xmm0 movsd %xmm0, 16(%rdi) movsd 24(%rsi), %xmm0 subsd %xmm1, %xmm0 movsd %xmm0, 24(%rdi)

If one changes the expression to: M2 = M; M2.diagonal()-=v; I'm getting this: movapd (%rsi), %xmm0 movaps %xmm0, (%rdi) movapd 16(%rsi), %xmm0 movaps %xmm0, 16(%rdi) movsd (%rdi), %xmm0 subsd (%rdx), %xmm0 movsd %xmm0, (%rdi) movsd 24(%rdi), %xmm0 subsd 8(%rdx), %xmm0 movsd %xmm0, 24(%rdi)

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.

Actually, it is much worse in this case:

Cheers, Christoph

Best regards, Adrien On Thu, Jan 12, 2017 at 12:44 AM, Alexander Voigt < alexander.voigt@xxxxxxxxxxxxxxxxxxxxx> 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@xxxxxxxxxxxxxxxxxxxxx ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-- Dipl. Inf., Dipl. Math. Christoph Hertzberg Universität Bremen FB 3 - Mathematik und Informatik AG Robotik Robert-Hooke-Straße 1 28359 Bremen, Germany Zentrale: +49 421 178 45-6611 Besuchsadresse der Nebengeschäftsstelle: Robert-Hooke-Straße 5 28359 Bremen, Germany Tel.: +49 421 178 45-4021 Empfang: +49 421 178 45-6600 Fax: +49 421 178 45-4150 E-Mail: chtz@xxxxxxxxxxxxxxxxxxxxxxxx Weitere Informationen: http://www.informatik.uni-bremen.de/robotik

**Follow-Ups**:**Re: [eigen] How to subtract a diagonal matrix from a matrix in one expression?***From:*Gael Guennebaud

**Re: [eigen] How to subtract a diagonal matrix from a matrix in one expression?***From:*Alexander Voigt

**References**:**[eigen] How to subtract a diagonal matrix from a matrix in one expression?***From:*Alexander Voigt

**Re: [eigen] How to subtract a diagonal matrix from a matrix in one expression?***From:*Adrien Escande

**Messages sorted by:**[ date | thread ]- Prev by Date:
**[eigen] Let's get reshape in shape for 3.4** - Next by Date:
**Re: [eigen] How to subtract a diagonal matrix from a matrix in one expression?** - Previous by thread:
**Re: [eigen] How to subtract a diagonal matrix from a matrix in one expression?** - Next by thread:
**Re: [eigen] How to subtract a diagonal matrix from a matrix in one expression?**

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