Subject: [ublas] zero_matrix iteration
From: Marco Guazzone (marco.guazzone_at_[hidden])
Date: 2009-09-13 14:33:41
I've noticed that zero_matrix is "not iteratable".
That is if you iterate a zero_matrix Z either by-row or by-column the
condition "Z.begin1() != Z.end1()" or "Z.begin1() != Z.end2()",
respectively, is always FALSE (according to how zero_matrix iterators
However if you iterate by using plain row/column indices (instead of
iterators) you're successfully able to iterate through a zero_matrix.
Is this conceptually right?
I mean, in my opinion a zero_matrix(N,M) should be iteratable just
like any other ordinary matrix, both with iterators and with simple
By preventing iteration (with iterators) it would seem that the matrix
has size 0, while it has a precise dimension (NxM)
What do you think?
Just for curiosity, I've looked at MTL4.
It seems MTL4 does not have a zero-matrix type. Rather you assign a
scalar 0 to a matrix type or you call the set_to_zero function. The
resulting matrix seems to be iteratable.
The code below shows an example of what I said above:
typedef double value_type;
typedef boost::numeric::ublas::zero_matrix<value_type> matrix_type;
std::cout << "Iteration by ITERATORS" << std::endl;
for (matrix_type::const_iterator1 row_it = Z.begin1(); row_it !=
std::cout << "Iteration by ITERATORS: never entered" << std::endl;
for (matrix_type::const_iterator2 col_it = row_it.begin();
col_it != row_it.end(); ++col_it)
std::cout << "Z(" << row << "," << col << ") = " <<
*col_it << " ==> " << value_type(0) << std::endl;
std::cout << "Iteration by INDICES" << std::endl;
for (matrix_type::size_type row = 0; row < Z.size1(); ++row)
for (matrix_type::size_type col = 0; col < Z.size2(); ++col)
std::cout << "Z(" << row << "," << col << ") = " << Z(row,
col) << " ==> " << value_type(0) << std::endl;