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

nonZeros() documentation

Tags: None
(comma "," separated)
jbauer
Registered Member
Posts
25
Karma
0

nonZeros() documentation

Sat Dec 20, 2014 11:14 pm
The documentation for the nonZeros() method on a dense matrix states:
the number of nonzero coefficients which is in practice the number of stored coefficients.

The following code outputs 10, however:
Code: Select all
std::cout << VectorXs::Zero(10).nonZeros() << std::endl;

I take it for a dense matrix nonZeros() always gives the size of the matrix, despite the documentation stating that it is the "the number of nonzero coefficients"? Not a big deal, I can do something like this instead, but it seems like it could be inefficient:
Code: Select all
( matrix.array() == 0 ).cast<int>().sum()


Thanks for the clarification!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: nonZeros() documentation

Mon Dec 29, 2014 7:31 am
Right, the documentation is not very clear as this method is useful for sparse matrices only, and available on dense matrices for API compatibility. Here "non-zeros" really means explicitly stored coefficients. You can use the count() method to simplify the expression:
Code: Select all
(matrix.array() == 0).count()


Bookmarks



Who is online

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