multi_array: how can I efficiently have a column selected?
Hi, I am a real newbie here, so please excuse the naivity of the question. 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. Can I use stl-like iterators on this, to use stl type algorithms? Is there any other repository of examples that would use high-level constructs like this so that I could look into a few examples? Again, I apologize for the bredth of the question. Best Regards, Petros
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
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 <
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@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
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#mul...
Thank you very much.
----- Original Message -----
From: "Rhys Ulerich"
//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#mul... _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
petros mamales
-
Rhys Ulerich