|Re: [eigen] How to subtract a diagonal matrix from a matrix in one expression?|
[ Thread 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: Adrien Escande <adrien.escande@xxxxxxxxx>
- 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==
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.