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

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


Dear Adrien,

> 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.

many thanks for the hint!  I'll go with this solution then.

Best regards,
Alex

On 01/11/2017 05:51 PM, 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.
> 
> 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,
> Adrien
> 
> On Thu, Jan 12, 2017 at 12:44 AM, Alexander Voigt
> <alexander.voigt@xxxxxxxxxxxxxxxxxxxxx
> <mailto: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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~



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