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

Map 3 CRS vector into eigen sparse matrix

Tags: None
(comma "," separated)
eshani
Registered Member
Posts
3
Karma
0
Hi,

I have a simple sparse matrix class build out of 3 vectors
Val - non zero values.
Col - the column of each non zero value
R_ptr - row pointer that tells you when to go to the next row

All stored as std::vector

So basically standard CRS format to store sparse matrix
I can't figure out how to map in to eigen sparse matrix without having to loop through the values (which is very slow

Any help please?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
See: http://eigen.tuxfamily.org/dox-devel/gr ... tml#title3 (last line). In short:

Map<SparseMatrix<double,RowMajor> > mat(rows,cols,nnz,R_ptr,Col,Val);
eshani
Registered Member
Posts
3
Karma
0
Thank you ggael!

However, I am still struggling making a simple task work.
Let say I just want to enter the complex diagonal matrix (with real entries at the moment) that i have built separately in CRS format
A = [ 1 0 0 ]
[ 0 2 0 ]
[ 0 0 3 ]

The following code doesn't work for me :/
Code:

#include <vector>
#include <complex>
#include <Eigen/Sparse>

std::vector<std::complex<double> > values({ 1,2,3 });
std::vector<int> columns({ 0,1,2 });
std::vector<int> row_pointer({ 0,1,2,3 });
Eigen::Map<Eigen::SparseMatrix<std::complex<double>, Eigen::RowMajor> > mat(3, 3, 3, row_pointer, columns, values);

code end.

I have tried several formatting similar to that. can't make it to work.
Have any idea why ?

Thanks again!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
You need to pass pointers, so row_pointer .data(), columns.data(), etc.
eshani
Registered Member
Posts
3
Karma
0
Yea, I saw that in the Eigen manual. The problem that is still doesn't work.
Tried also to use .begin() of .front
with and with out & *
Nothing works

It gives me this error:

Error (active) no instance of constructor "Eigen::Map<PlainObjectType, MapOptions, StrideType>::Map [with PlainObjectType=Eigen::SparseMatrix<std::complex<double>, 1, int>, MapOptions=0, StrideType=Eigen::Stride<0, 0>]" matches the argument list


I can't find a way to resolve it.
Any clue what might cause it?
Maybe need to include more libraries?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
oh, you must use Eigen 3.3 for that feature. In 3.2, there is MappedSparseMatrix which is equivalent but deprecated in 3.3.


Bookmarks



Who is online

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