This forum has been archived. All content is frozen. Please use KDE Discuss instead.

M.diagonal( ).asDiagonal( )

Tags: None
(comma "," separated)
sylvainrousseau
Registered Member
Posts
7
Karma
0

M.diagonal( ).asDiagonal( )

Fri Nov 28, 2014 10:30 am
Hi !

Working with SparseMatrix<double>.
Trying to compute A - B where B=M - diag(M)

I'am not even able to compile the very first step of this computation :
Code: Select all
SparseMatrix<double> diagM = M.diagonal( ).asDiagonal( ) ;


Receiving the compiler error :
/home/rousseau/MPI/proj/src/elemFinSeq.cpp|27|erreur: conversion from ‘const Eigen::DiagonalWrapper<const Eigen::Diagonal<const Eigen::SparseMatrix<double>, 0> >’ to non-scalar type ‘Eigen::SparseMatrix<double>’ requested|

May I ask you some hints please ?

All the best
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: M.diagonal( ).asDiagonal( )

Fri Nov 28, 2014 1:35 pm
To make it efficient one option would be:
Code: Select all
C = A - M;
C.diagonal() += M.diagonal();

In case you need B=M-diag(M) multiple times:
Code: Select all
B=M;
B.prune([] (int i, int j, double&) { return i!=j; });
C = A-B;
sylvainrousseau
Registered Member
Posts
7
Karma
0

Re: M.diagonal( ).asDiagonal( )

Fri Nov 28, 2014 2:22 pm
Many thanks Gaël for your precious support.

May I ask you what's wrong with my naive way to extract the sparse matrix diagonal in my snippet , I don't actually understand the meaning of the compiler complaint ?

Tried to use your hint to fetch a sparse matrix without its own diagonal :
Code: Select all
  SparseMatrix<double> M(3,3) ,C(3,3);
    C = M;
   C.diagonal() -= M.diagonal() ;


Does not compile neither.

All the best

Sylvain
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: M.diagonal( ).asDiagonal( )

Mon Dec 01, 2014 1:54 pm
Your version does not work because there is no conversion from a diagonal matrix to a sparse one. It should not be difficult to add, but this approach will still be slower than the ones I suggested because computing M-diagM is a O(nnz) operation with a somewhat complicated logic and dynamic memory management (as A-B). On the other hand, C.diagonal() -= M.diagonal() exploits the fact that the coefficients are sorted to quickly find the entries which as to be modified.

Regarding, "C.diagonal() -= M.diagonal()", indeed, I've forgotten that SparseMatrix::diagonal() was read-only. In the devel branch it is now read-write with the restriction that the diagonal entries must exist if the user writes to the diagonal.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]