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

edit: SparseMatrix: reading single elements

Tags: None
(comma "," separated)
Zortex
Registered Member
Posts
5
Karma
0
OS
Hi, this ist my first post here ;)
I started using eigen last week.
In my project, I use Matrices as a storage structure. Many coefficients in the Matrices are zero, so SparseMatrix saves a lot of Memory :)
Thanks for that great Project, which is simple to use.

As i wrote, i'm using SpareMatrix as a storage structure. For filling the SpareMatrix, i iterate over all rows and columns, calculate the value an set them. (m.fill(index1, index2) = calcvalue;)

But the problem is: I need to read single coefficients later from that matrix. Is there a way like matrix.read(row_index,col_index) ??
Is there an easier (better) way to read out the single coefficient?

In some parts of the code, i can iterate over the nonzero coefficients described here,
but sometimes i cannot do that.

Edit:
I've found a typo in that part. there ist a slash that shouldn't be there

Code: Select all
SparseMatrixType mat(rows,cols);
for (int k=0; k\<m1.outerSize(); ++k)  //here ist the typo
  for (SparseMatrixType::InnerIterator it(mat,k); it; ++it)
  {
    it.value();
    it.row();   // row index
    it.col();   // col index (here it is equal to k)
    it.index(); // inner index, here it is equal to it.row()
  }

%%and now the corrected line:
for (int k=0; k<m1.outerSize(); ++k)  //correct line


IDE ist MS Visual Studio 2008 pro, with the MS compiler building a MFC Application.

Hope someone can understand my problem and give me some help.
As you can read, i'm not a native englich writer :)

Thanks for creating eigen, especially SparseMatrix.

Zortex
Zortex
Registered Member
Posts
5
Karma
0
OS
I've found a way with mat.CoeffRef(rowind, colind)

Bur before i can do that, i have to check if that Coefficient on that row an column exist (is nonzero).
Otherweise, there is an runtime error if that value does not exist.
How can i check that?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
indeed, this not possible yet. I'll add a isNonZero(i,j) function doing so...

EDIT: oops, actually the function coeff(i,j) returns zero if the element does not exist.
Zortex
Registered Member
Posts
5
Karma
0
OS
hm, what is wrong with that code??
Example:
Code: Select all
Eigen::SparseMatrix<double,Eigen::RowMajor> sp_TEST(200,100);
1   sp_TEST.startFill();
2   sp_TEST.fill(30,40) = 5;
3   sp_TEST.fill(35,46) = 19;
4   sp_TEST.endFill();
5   double ref_val = sp_TEST.coeffRef(30,40);
6   double ref_val2 = sp_TEST.coeffRef(1,3);


Code runs fine without Line 6. If Line 6 is not commented out, there is an error while running that code:
Assertion failed!
File:<PATH TO EIGEN>..\SparseMatrix.h
Line: 111

Expression: end>start &&"coeffRef cannot be called on a zero coefficient"

For information on how you can cause an assertionn failure...


Running on Win7 (x64), building an 64bit-MFC-App with MS VS 2008 pro (Debug Mode) Eigen is version 2.0.15.


Looks like coeffRef is returning an error on zero elements in my implementation.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
coeffRef(i,j) returns a writable reference to the element i,j, so this element has to exist. Use coeff(i,j) to get the readonly value of the element i,j.
Zortex
Registered Member
Posts
5
Karma
0
OS
Great.
I've done a (very) short test, that seem's to work 8)

I'll check that tomorrow. That could increase the performace of code definetly :)
Zortex
Registered Member
Posts
5
Karma
0
OS
Grat.
I've checked that with mat.coeff(i,j)

It is running well now :)

Thanks to ggael :)


Bookmarks



Who is online

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