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

Sparse Matrix Incompitable to Diagonal and Matrix ?

Tags: None
(comma "," separated)
shouldntcare
Registered Member
Posts
2
Karma
0
Hi,

so I basicly want to create a Sparse Matrix, which Diagonal is saved in a VectorXd.
This matrix is supposed to get added to another sparse matrix. In Matlab this would be M=spdiags(v);
Being confused by the way the Diagonal, normal and Sparse Matrix interact in Eigen, I found this thread:
https://forum.kde.org/viewtopic.php?f=74&t=128552#p342800
DiagonalMatrix<double,Dynamic> diag1, diag2;
SparseMatrix<double> sparse_mat1;
sparse_mat1 -= diag2;


But this minimal example already gives me a compiler error:
Code: Select all
 no match for 'operator-=' (operand types are 'Eigen::SparseMatrix<double>' and 'Eigen::DiagonalMatrix<double, -1>')
       sparse_mat1 -= diag2;
                   ^

These are my includes:
Code: Select all
#include <Eigen/SparseCore>
#include <Eigen/Eigen>
#include <Eigen/Sparse>
#include <iostream>
#include <QVector>
#include <Eigen/src/SparseCore/SparseMatrix.h>
#include <Eigen/src/SparseCore/SparseUtil.h>
#include<Eigen/SparseLU>
#include<vector>
#include<Eigen/Core>


Thanks for your help!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
You need the 3.3 branch.
shouldntcare
Registered Member
Posts
2
Karma
0
Thanks for your reply, I will try and report back.
Edit:
I tried it, and it compiled with the devel version, but as soon as I start putting numbers in things start to fall apart.
Code: Select all
// create Matrix and Vector
    Eigen::Vector4d C(1,1,1,1);
    SparseMatrix<double> sparse_mat1(4,4);
    DiagonalMatrix<double,Dynamic> diag_C;
    Matrix<double,Dynamic,Dynamic>E;
    //this works
    diag_C=C.asDiagonal();
    E=diag_C;
    std::cout<<E<<std::endl;
    //this crashes
    sparse_mat1 -= diag_C;
    std::cout<<"Not running to this point"<<std::endl;

This crashes with an assertion error
"You are using a non finalized sparse matrix or written coefficient does not exist.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Indeed -= or += operators assumes that the diagonal elements exists. I admit that the error message is not very clear... On the other hand, doing:

sparse_mat1 = diag_C;

or

sparse_mat1 = -diag_C;

will work as expected.

Once the diagonal coefficients exists, you can modify them:

sparse_mat1 += VectorXd::Ones(n).asDiagonal();

or:

sparse_mat1.diagonal().array() += 1.0;

etc.


Bookmarks



Who is online

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