Boost logo

Ublas :

Subject: [ublas] Bugs about reverse iterator of matrix_binary
From: Shangtong Zhang (zhangshangtong.cpp_at_[hidden])
Date: 2016-04-02 09:56:38


Hi,

Recently I’m working on toeplitz matrix. However I find some confusing problems with matrix_binary.
See the following code:

> banded_matrix<int> bm(3, 4, 2, 2);
> bm(0, 0) = 7;
> std::cout << bm(0, 0);
> bm(0, 2) = 9;
> std::cout << bm(0, 2);
> auto row = (bm + bm).begin1();
> std::cout << '\n';
> for (auto it = row.begin(); it < row.end(); ++it) {
> std::cout << *it << ' ';
> }

It works well and outputs ’14 0 18 0’ as expected.
When I change it to reverse iterator,

> for (auto it = row.rbegin(); it < row.rend(); ++it) {
> std::cout << *it << ' ';
> }

It fails with output ‘0 0 0 0’.
If I use normal matrix rather than banded matrix, both iterator and reverse iterator work well.

See https://github.com/uBLAS/ublas/blob/master/include/boost/numeric/ublas/matrix_expression.hpp <https://github.com/uBLAS/ublas/blob/master/include/boost/numeric/ublas/matrix_expression.hpp>
There are many condition checks which only make sense for normal iterators, such as Line 2516, Line 2548.

> BOOST_UBLAS_INLINE
> void decrement (packed_random_access_iterator_tag) {
> if (it1_ != it1_end_)
> if (j_ <= it1_.index2 ())
> -- it1_;
> if (it2_ != it2_end_)
> if (j_ <= it2_.index2 ())
> -- it2_;
> -- j_;
> }

However 'dense access specialisation’(Line 2482) doesn’t have these checks, so it works well for reverse iterators.
I’m not sure how to fix this issue.

Shangtong Zhang,
Senior Student,
School of Computer Science,
Fudan University, PRC.