|
Ublas : |
Subject: Re: [ublas] Boost FOREACH matrix iterator examples?
From: oswin krause (oswin.krause_at_[hidden])
Date: 2013-10-07 02:57:50
Hi.
To answer your question. Both of your ways are wrong, but the second is
not beyond hope :). You can't implement this with BOOST_FOREACH because
a matrix is not a container. The solution from Joaquim will of course
also work and perform ssentially the same operations, however i am not
so sure about the costs :).
boost::numeric::ublas::compressed_matrix<u32>::iterator1 i=M.begin1();
//important: store end iterator here, this is super expensive to construct
boost::numeric::ublas::compressed_matrix<u32>::iterator1 iEnd = M.end1();
//iterate over the rows
for (; i != iEnd; ++i)
{
//use dual iterator to iterate over the columns of the current row.
again: this is expensive to construct.
boost::numeric::ublas::compressed_matrix<u32>::iterator2 j = i.begin();
boost::numeric::ublas::compressed_matrix<u32>::iterator2 jend = i.end();
for (; j != jend; ++j)
{
// use j.index1(), j.index2(), *j to access each non zero element
of M
*j = v(j.index1()); // do something with each non zero element of
both matrix and vector...
} // end for
} // end for
depending on what you exactly want to do there might be a lot faster
ways (factor 3+). This is the way that works with every matrix/matrix
expression. if you are only interested in the column index and column
value of the compressed_matrix you can have direct access to the
internal data structures.
On 04.10.2013 23:19, Reich, Darrell wrote:
>
> The two pieces of code below attempt to iterate over the non zero
> elements of a matrix, both crash. I suspect from looking over the
> various discussions online that the correct placement of & is
> critical? Assuming it is possible to create two working examples of
> BOOST_FOREACH matrix and matrix iterators, which method is preferred /
> "better" / more efficient? Thanks!
>
> #include <boost/foreach.hpp>
>
> #include <boost/numeric/ublas/matrix.hpp>
>
> #include <boost/numeric/ublas/matrix_sparse.hpp>
>
> #include <boost/numeric/ublas/matrix_expression.hpp>
>
> #include <boost/numeric/ublas/matrix_proxy.hpp>
>
> #include <boost/numeric/ublas/vector.hpp>
>
> #include <boost/numeric/ublas/vector_sparse.hpp>
>
> #include <boost/numeric/ublas/vector_expression.hpp>
>
> #include <boost/numeric/ublas/vector_proxy.hpp>
>
> boost::numeric::ublas::compressed_matrix<float> M(n,n);
>
> boost::numeric::ublas::compressed_vector<float> v(n);
>
> // Example 1: BOOST for each...
>
> // TODO: 2D nested matrix example?
>
> BOOST_FOREACH(float, v)
>
> {
>
> v += 1; // do something with each non zero element of v...
>
> } // end for each
>
> // Example 2: iterators
>
> boost::numeric::ublas::compressed_matrix<u32>::iterator1 i;
>
> boost::numeric::ublas::compressed_matrix<u32>::iterator2 j;
>
> for (i = M.begin1(); i != M.end1(); ++i)
>
> {
>
> for (j = M.begin2(); j != M.end2(); ++j)
>
> {
>
> // use j.index1(), j.index2(), *j to access each non zero
> element of M
>
> *j = v(j.index1()); // do something with each non zero element
> of both matrix and vector...
>
> } // end for
>
> } // end for
>
> Extra credit: I suspect I should protect the code with some kind of
> assert to ensure the size of vector is the same as the size of matrix.
>
>
>
> _______________________________________________
> ublas mailing list
> ublas_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> Sent to: Oswin.Krause_at_[hidden]