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

Elementwise predicate on NaN

Tags: None
(comma "," separated)
jbrandmeyer
Registered Member
Posts
1
Karma
0
OS

Elementwise predicate on NaN

Thu Aug 06, 2009 4:35 pm
I have implemented an algorithm using Eigen, and after a few thousand iterations, NaN's start to show up. Two part question:

- In the next version, can you add the predicates hasNaN() and hasInf() that test for the presence of an element with either condition?

- In the meantime, what is the best way to iterate across all of the elements of an Eigen matrix - just build my own nested loop over the row and column indexes? Is there a pre-formed iterator, or some other means of accessing all of the storage as a linear array? Ideally, this method would be efficient for both sparse and dense matrices and vectors.

Thanks,
-Jonathan
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Elementwise predicate on NaN

Thu Aug 06, 2009 4:56 pm
jbrandmeyer wrote:- In the next version, can you add the predicates hasNaN() and hasInf() that test for the presence of an element with either condition?


why not.

- In the meantime, what is the best way to iterate across all of the elements of an Eigen matrix - just build my own nested loop over the row and column indexes? Is there a pre-formed iterator, or some other means of accessing all of the storage as a linear array? Ideally, this method would be efficient for both sparse and dense matrices and vectors.


The are at least 3 possibilities:

1 - you can use two basic nested loops: (only for dense matrix)
Code: Select all
for(j=0;j<m.cols();++j)
for(i=0;i<m.rows();++i)


2 - you can use an "outer" loop + an iterator: (for dense and sparse)
Code: Select all
for(int j=0;j<m.outerSize();++j)
for(MatType::InnerIterator it(m,j);it;++it) {
 it.index(), it.value(), it.row(), it.col()
}


3 - you can write/build a functor to apply it:
- to generate a matrix (see MatrixBase::nullOp())
- coeff wise (see MatrixBase::unaryOp())
- coeff wise between two matrices
- to perform a reduction
- and we also have visitors

Our functor mechanism is compatible with the STL, so you can build them using std::bind, etc..

So basically, for your hasNan() predicate, you can simply write a trivial functor testing for a single scalar, and then do:
m.unaryOp(nan_predicate<ScalarType>()).any()


You can even extend MatrixBase to implement a m.hasNan() shorcut yourself without having to modify any Eigen's file. See http://eigen.tuxfamily.org/api/CustomizingEigen.html.


Bookmarks



Who is online

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