Boost logo

Boost Users :

Subject: Re: [Boost-users] multi_array: how can I efficiently have a columnselected?
From: petros mamales (pmamales_at_[hidden])
Date: 2010-01-29 17:35:53


Thank you very much for your quick response.
To be more concrete:

#include "boost/multi_array.hpp"
//stl
#include <algorithm>
#include <iostream>
using namespace std;

int main(int argc, char **argv){
 const int M = 3;
 const int N = 4;

 typedef boost::multi_array< int, 2> array_t;
 typedef array_t::index index;
 typedef array_t::index_range range_t;

 typedef array_t::array_view<1>::type array_1d_view_t;

 array_t A( boost::extents[M][N], boost::fortran_storage_order() );
//initialize:
 int num = 0;
 for ( index i = 0; i < M ; ++i ) {
      for ( index j = 0; j < N ; ++j ) {
           A[i][j] = ++num;
      }
 }
//show initialization
 cout << "A: 3x4 array" << endl;
 for ( index i = 0; i < M ; ++i ) {
      for ( index j = 0; j < N ; ++j ) {
           cout << A[i][j] << "\t";
      }
      cout << endl;
 }
 cout <<endl;

//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() );
//..and show it
 cout << endl << "It's third column is:" << endl;
 for ( boost::multi_array_ref< int, 1>::iterator rowIt = col.begin();
rowIt != col.end(); ++rowIt ) {
    cout << *(rowIt) << endl; }
//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() );
 cout << endl << "It's third row is:" << endl;
 for ( boost::multi_array_ref< int, 1>::iterator colIt = row.begin();
colIt != row.end(); ++colIt ) {
  cout << *(colIt) << "\t";
 }
 cout << endl;

     return 1;
}

Using Boost 1.40 on msvc 2010 win ista (32 bit)

Because I store things in fortran storage style, I derive the column quite
naturally.
However the row is a disaster:

A: 3x4 array
1 2 3 4
5 6 7 8
9 10 11 12

It's third column is:
3
7
11

It's third row is:
9 2 6 10
I understand this to be because

Two questions:
a) The only way I could think of to "promote" the view to a m_a_r
(multi_array_ref) was:
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() );
(o/wise there would be no way I could use the iterators)
Is this an overkill? Is there a simpler way?

b) about the real issue of the failure of the stepping through the row
elements: is there a workaround that would still allow me to use iterators?
there is no way for the lower-dimension-array to know/remember the stepping
sequence?

Thank you very much in advance,
Best Regards,
Petros

----- Original Message -----
From: "Rhys Ulerich" <rhys.ulerich_at_[hidden]>
To: <boost-users_at_[hidden]>
Sent: Friday, January 29, 2010 2:41 PM
Subject: Re: [Boost-users] multi_array: how can I efficiently have a
columnselected?

> I, say, have a 3d array (fortran storage), and want to create a view to it
> so that I select say:
> grid[ indices[
> [range(9,10)]
> [3],
> [range(9,10)] ];
>
> I am not clear on exactly what this creates: from the documentation, I see
> this creating a 3d view.

I think this would be a 2d slice since you provided the single value 3
in the second position. The underling type would be accessible through
either the templated array_view typedefs or else the array_view_gen
mechanism.

> Can I use stl-like iterators on this, to use stl type algorithms?

You can use stl-algorithms on the 2d view. Be aware that iterators
retrieved from the 2D view (via begin(), end()) walk over 1D views
in your example.

> Is there any other repository of examples that would use high-level
> constructs like this so that I could look into a few examples?

Other than the tutorial, the reference, and an article [1], I'm not
aware of any.

- Rhys

[1] http://www3.interscience.wiley.com/journal/109793810/abstract
_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users




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