Re: Boost FOREACH matrix iterator examples?

"oswin krause" <[email protected]>
Newsgroups gmane.comp.lib.boost.ublas
Message-ID <[email protected]>
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
> [email protected]
> http://lists.boost.org/mailman/listinfo.cgi/ublas
> Sent to: [email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.