boost::numeric::ublas matrix iterators

Hi, I read through the Ublas documentation but did not see any examples with the iterator1 and iterator2 being used with a matrix. I am having trouble working with them. Does anyone know how to recreate this example using the iterators? http://www.boost.org/libs/numeric/ublas/doc/matrix.htm int main () { using namespace boost::numeric::ublas; matrix<double> m (3, 3); for (unsigned i = 0; i < m.size1 (); ++ i) for (unsigned j = 0; j < m.size2 (); ++ j) m (i, j) = 3 * i + j; std::cout << m << std::endl; } Many thanks, Ed Johnson

Ed Johnson
Does anyone know how to recreate this example using the iterators?
try: using namespace boost::numeric::ublas; typedef matrix<double> MatType; MatType m(3,3); for(MatType::iterator1 i1 = m.begin1(); i1!=m.end1(); ++i1) for(MatType::iterator2 i2 = i1.begin2(); i2!=i1.end2(); ++i2) *i2= ... // obviously you cannot assign the iterators here untested - but the main idea is, that iterator1 accesses each row and iterator2 loops over the columns of the rows - this should work for all models of Matrix (including sparse matrices) Good luck, Alexander.

Alexander Bell wrote:
Ed Johnson
writes: Does anyone know how to recreate this example using the iterators?
try:
using namespace boost::numeric::ublas; typedef matrix<double> MatType; MatType m(3,3);
for(MatType::iterator1 i1 = m.begin1(); i1!=m.end1(); ++i1) for(MatType::iterator2 i2 = i1.begin2(); i2!=i1.end2(); ++i2) *i2= ... // obviously you cannot assign the iterators here
untested - but the main idea is, that iterator1 accesses each row and iterator2 loops over the columns of the rows - this should work for all models of Matrix (including sparse matrices)
Good luck,
Alexander.
Thanks, Alexander. I tried the code and got these compilation errors. c:\workspace\hivm\trunk\src\test\PreProcessorTest.cpp(315): error C2039: 'begin2' : is not a member of 'boost::numeric::ublas::matrix<T>::iterator1' with [ T=double ] c:\workspace\hivm\trunk\src\test\PreProcessorTest.cpp(315): error C2039: 'end2' : is not a member of 'boost::numeric::ublas::matrix<T>::iterator1' with [ T=double ] I tried the following, but it doesn't work either. The goal of the code is to load the entire matrix with 2's. code: using namespace boost::numeric::ublas; typedef matrix<double> MatType; MatType m(3,3); for(MatType::iterator1 i1 = m.begin1(); i1!=m.end1(); ++i1) for(MatType::iterator2 i2 = m.begin2(); i2!=m.end2(); ++i2) *i2 = 2; std::cout << m << std::endl; output: ((2,2,2),(-6.27744e+066,-6.27744e+066,-6.27744e+066),(-6.27744e+066,-6.27744e+066,-6.27744e+066)) Also, pointing an iterator2 to an iterator1 results in a conversion error. c:\workspace\hivm\trunk\src\test\PreProcessorTest.cpp(315): error C2440: 'initializing' : cannot convert from 'boost::numeric::ublas::matrix<T>::iterator1' to 'boost::numeric::ublas::matrix<T>::iterator2' with [ T=double ] and [ T=double ] Any ideas? Thanks, Ed

Ed Johnson
I tried the code and got these compilation errors.
sorry... iterator2 has to be initialised from "standard" begin() and end() of iterator1: typedef matrix<double> MatType; MatType m(3,3); for(MatType::iterator1 i1 = m.begin1(); i1!=m.end1(); ++i1) for(MatType::iterator2 i2 = i1.begin(); i2!=i1.end(); ++i2) *i2 = 2; this time I tested the code (g++ v. 4.02, boost 1.33.1). Hope that helps, Alexander.

Alexander Bell wrote:
Ed Johnson
writes: I tried the code and got these compilation errors.
sorry... iterator2 has to be initialised from "standard" begin() and end() of iterator1:
typedef matrix<double> MatType; MatType m(3,3);
for(MatType::iterator1 i1 = m.begin1(); i1!=m.end1(); ++i1) for(MatType::iterator2 i2 = i1.begin(); i2!=i1.end(); ++i2) *i2 = 2;
this time I tested the code (g++ v. 4.02, boost 1.33.1).
Hope that helps,
Alexander.
Thanks. Unfortunately, it doesn't compile for me on Visual Studio. (VS 7.1, boost 1.33.1) c:\workspace\hivm\trunk\src\test\PreProcessorTest.cpp(516): error C2039: 'begin' : is not a member of 'boost::numeric::ublas::matrix<T>::iterator1' with [ T=double ] c:\workspace\hivm\trunk\src\test\PreProcessorTest.cpp(516): error C2039: 'end' : is not a member of 'boost::numeric::ublas::matrix<T>::iterator1' with [ T=double ]
participants (2)
-
Alexander Bell
-
Ed Johnson