 # Ublas :

Subject: [ublas] zero_matrix iteration
From: Marco Guazzone (marco.guazzone_at_[hidden])
Date: 2009-09-13 14:33:41

Hello dears,

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
are defined).
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
indices.
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:

---[code]---
#include <boost/numeric/ublas/matrix.hpp>
#include <iostream>

int main()
{
typedef double value_type;
typedef boost::numeric::ublas::zero_matrix<value_type> matrix_type;

matrix_type Z(5,4);

std::cout << "Iteration by ITERATORS" << std::endl;

for (matrix_type::const_iterator1 row_it = Z.begin1(); row_it !=
Z.end1(); ++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)
{
matrix_type::size_type row(col_it.index1());
matrix_type::size_type col(col_it.index2());

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;
}
}
}
---[/code]---

Cheers!!

-- Marco