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

Using colwise() in range based for loop: Recommended?

Tags: colwise, iterators colwise, iterators colwise, iterators
(comma "," separated)
lens
Registered Member
Posts
4
Karma
0
I have a function taking an eigen matrix. Inside the function I want to iterate over the matrix' columns. So far I've only seen range-based iteration for flattened matrices, such as in the tutorial on STL iterators.
However, to my slight surprise, it also works when iterating over the VectorwiseOp returned by colwise():
Code: Select all
using Eigen::Ref;
using Matrix2Xd = Eigen::Matrix<double, 2, Eigen::Dynamic>;

void myFunc( const Ref<const Matrix2Xd>& m ){
    for ( const Ref<const Matrix2Xd>::ConstColXpr col : m.colwise() ){
        std::cout << col.transpose() << "\n";
    }
}
My question is, whether this is recommended usage, as I've not seen it before and don't know whether there are caveats that I'm not aware of. The documentation on VectorwiseOp and colwise implies that they're only intended for reductions.

The resulting code is, however, rather expressive.

EDIT: I've been using Eigen for a while now and I just want to say how amazing it is. Thanks to ggael and everyone who is involved!
lens
Registered Member
Posts
4
Karma
0
To answer my own question: At least for the developer branch, it's even described in one of the tutorials. See here at the very bottom.


Bookmarks



Who is online

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