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

SparseMatrix: Setting an element to zero

Tags: None
(comma "," separated)
pamparana
Registered Member
Posts
18
Karma
0
Hello,

I am new to Eigen and trying to use the SparseMatrix class. If I set the value of a SparseMatrix entry in Eigen as follows:

Code: Select all
sparse_matrix->coeffref(10, 10) = 0;


Would this actually shrink the storage required by the matrix or would it try and store a 0 and use up 4 bytes there (assuming integer type)?
If the answer is the latter, how can I set entries to 0, so that it does not use my extra space?

Also, what about something like this:

Code: Select all

typedef Eigen::Triplet<double> TripletType;
std::vector<TripletType> t;
for (int i = 0; i < count; ++i)
{
    for (int j = 0; j < dim; ++count)
    {
        t.push_back(TripletType(i, j, 0));
    }
}

sparse_matrix->setFromTriplets(t.begin(), t.end())       


Would explicit zeros be stored in this case? If I want to avoid that, what should I do.

Thanks a lot,

Luca
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Doing: t.push_back(TripletType(i, j, 0)) will insert an explicit zero, so just do not insert it.
pamparana
Registered Member
Posts
18
Karma
0
Thank your for the answer. However, if I have many entries to update, it seems that the Triplet way is the best way to do it in an efficient manner at least from the Eigen documentation that I have seen so far. In that case, can you suggest how to update the entries (in a batch) but not setting an explicit zero. So, the entries could or could not have an explicit value but I would like to reclaim it.

Do you mean, have something like:

Code: Select all
t.push_back(TripletType(i, j))


instead of:
Code: Select all
t.push_back(TripletType(i, j, 0))


Also, any idea what can be done for individual updates like:

Code: Select all
sparse_matrix->coeff(j,j) = 0;


Thanks again,
Luca
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
Your question is not clear, so let me try to reformulate. Are you saying that you already have a sparse matrix A that you already used for some purpose like solving Ax=b, and now you would like to change a *few* entries in A, and in particular to remove some? If that's so, then set them to 0 using A.coeffRef(i,j) = 0, and once you're done, you can remove them with A.prune().
pamparana
Registered Member
Posts
18
Karma
0
Thanks for the answer. This is what I meant. So I guess the solution is to call prune regardless of whether I use coeffRef or use the Triplets to set the entries to zero.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
The triplet list cannot be used to partially modify a matrix. It can be used to set all coefficients at once. Implicitly, setFromTriplets does a global setZero().


Bookmarks



Who is online

Registered users: Baidu [Spider], Bing [Bot], Google [Bot]