Row selection by function
Issue #310
new
Hi,
I’ve found the rows/column selection utility quite helpful, though I thought it would be easier if we could return rows satisfying a function rather than by indexes only. (This could make the selection method a little more ergonomic.)
I wrote the following as adapters which produce the desired results, but I thought I’d share the idea/an example implementation in case you wanted to incorporate them into a later blaze release.
template<typename F, typename IT=std::uint32_t, size_t N=16>
auto indices_if(const F &func, size_t n) {
blaze::SmallArray<IT, N> ret;
for(IT i = 0; i < n; ++i) if(func(i)) ret.pushBack(i);
return ret;
}
template<typename M, typename F, typename IT=std::uint32_t>
auto rows_if(const M &mat, const F &func) {
auto wrapfunc = [&](auto x) -> bool {return func(row(mat, x, blaze::unchecked));};
return rows(mat,
indices_if<decltype(wrapfunc),IT>(wrapfunc, // predicate
mat.rows())); // nrow
}
template<typename M, typename F, typename IT=std::uint32_t>
auto columns_if(const M &mat, const F &func) {
auto wrapfunc = [&](auto x) -> bool {return func(column(mat, x, blaze::unchecked));};
return columns(mat, // matrix
indices_if<decltype(wrapfunc), IT>(wrapfunc, // predicate
mat.columns())); // ncol
}
Thanks for your time!
Hi Daniel!
Thanks a lot for your proposal! I admit that I feel a little proud that Blaze already enables you to implement your idea of a row selection in just a few lines of code. I like the idea and will definitely consider this as a nice extension for one of the next releases of Blaze.
Have you considered that issue #285 might provide a different point of view on your problem? The given predicate is nothing but a reduction operation that reduces a row or column to a
bool
. The resulting vector ofbool
could be used to produce the necessary indices.Thanks again,
Best regards,
Klaus!