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

Sparse Matrix: Setting nonzero elements of row to be 0

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

I'm a bit new to Eigen as well as C++. I'm trying to implement a FEM code using Sparse matrices where I need to apply Dirichlet Boundary Conditions to some certain elements/nodes.

So here is what I want to do:
After forming my sparse matrix, I'd like to remove some rows or change the entries of those rows with 0 whose index meet some certain conditions.

What I've tried:
1- didn't work:
m.rows(i)=0; // ok with .rows() I don't have write permit on sparse matrices

2- didn't work: prune method (found here: stackoverflow.com/questions/20986305/set-row-column-block-to-0-in-eigen-sparse-matrix)
However I could not pass row index 'i' into prune function. It simply doesn't take that many inputs.

3- worked but really very costly for a big matrix:
using m.coeffRef assign 0 to all row. For a 500000x500000 matrix, it's definitely not the way to do, especially when there is more than one row I'd like to apply this.

What I think I could try but don't know how to:
Somehow obtain the column values of non-zero entries on the row 'i', then replace only those by 0. The rest is already 0. There shouldn't be too many of such nonzero elements initially.

Another note:
It says on the documentation
(sm1.middleRows(start, numRows); // RO because sm1 is column-major
Well, I don't think it works even though it is row-major matrix. I tried writing 0 using: m.middleRows(i,1)=0;


Well I'm pretty sure I've been doing somethings wrong and at least one of the methods should work. If you could make suggestions/comments, I'd be glad.

Thanks
arifg
Registered Member
Posts
2
Karma
0
I believe the following line is also acceptably quick and it does sort of what I asked:
Code: Select all
for (Eigen::SparseMatrix<double, Eigen::RowMajor>::InnerIterator it(mat, i); it; ++it)   
    {it.valueRef() = 0;}

But I'm still open to suggestions if there is any quicker way of assigning zeros to a row.


Bookmarks



Who is online

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