Boost logo

Boost Users :

Subject: Re: [Boost-users] multi_array: how can I efficiently have a columnselected?
From: Rhys Ulerich (rhys.ulerich_at_[hidden])
Date: 2010-02-03 18:46:09


Re: Petros' question about getting row and column views of a 2D,
Fortran order multi_array...

> //choose the third column:
> array_1d_view_t col_view = A[ boost::indices[ range_t(0,M) ][2] ];
> boost::multi_array_ref< int, 1> col( &(col_view[0]),   boost::extents[M],
> boost::fortran_storage_order() );

multi_array_ref wraps a contiguous block of storage [1]. Since matrix
A has Fortran ordering, the column is contiguous and your code works.

> // and the same for row
> array_1d_view_t row_view = A[ boost::indices[2][range_t(0,N)] ];
> boost::multi_array_ref< int, 1> row( &(row_view[0]), boost::extents[N],
> boost::fortran_storage_order() );

Rows in Fortran ordering are not contiguous, row_view is not
contiguous, and "wrapping" row_view with multi_array_ref isn't what
you want.

Had you worked with C ordering in your sample code, the columns would
fail but the rows would work. You should be using the views directly.
 Each view models the MultiArray concept and has (nearly) all the
iterator goodness that multi_array and multi_array_ref do:

//choose the third column:
array_1d_view_t col_view = A[ boost::indices[ range_t(0,M) ][2] ];
//..and show it
cout << endl << "It's third column is:" << endl;
for ( array_1d_view_t::iterator it = col_view.begin(); it !=
col_view.end(); ++it ) {
  cout << *(it) << endl;
}

//and the same for row:
array_1d_view_t row_view = A[ boost::indices[2][range_t(0,N)] ];
cout << endl << "It's third row is:" << endl;
for ( array_1d_view_t::iterator it = row_view.begin(); it !=
row_view.end(); ++it ) {
  cout << *(it) << endl;
}

- Rhys

[1] http://www.boost.org/doc/libs/1_42_0/libs/multi_array/doc/reference.html#multi_array_ref


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net