Hi I'm having a problem with the boost/multi_array and I'm hoping someone here can help, or at least explain why the following doesn't work. The problem: I have a multi_array of ints that I want to be able to view as being either row major or column major to simplify a number algorithms. I don't want to change the internal representation of the array (the default c++ row major storage layout is fine), I just want to be able to interchange the indexing order from array[row][col] to array[col][row] for the same multi_array. To do so, I attempted to define 2 array_views of the same multi_array with different stride values, like this: #include <iostream> using namespace std; #include <boost/multi_array.hpp> using namespace boost; int main(void) { typedef multi_array<int, 2> intarray2; typedef intarray2::array_view<2>::type intarray2view2; typedef intarray2::array_view<1>::type intarray2view1; typedef intarray2::index_range intarray2range; intarray2 arr(extents[3][3]); // test data int a = 0; for (int r = 0; r < 3; r++) { for (int c = 0; c < 3; c++) { arr[r][c] = a++; } } intarray2view2 rows = arr[indices[intarray2range().stride(3)][intarray2range().stride(1)]]; intarray2view2 cols = arr[indices[intarray2range().stride(1)][intarray2range().stride(3)]]; /* 0 1 2 3 4 5 6 7 8 */ cout << rows[2][1]; // i expect this to print 7 cout << cols[2][1]; // i expect this to print 5 } Unfortunately, this doesn't work at all (an assert fails when indexing the views). I debugged (using Visual C 2005), and found that the 'rows' array_view has a stride collection of [9, 1], while the 'cols' array_view stride collection is [3, 3]. Both are different than what I specified during construction. It appears that they're being multiplied by the extents of the first dimension of the multi_view array (in this example: 3). I can produce a column major view of a single column of the array using a 1 dimensional view constructed using a degenerate range, like this: intarray2view1 col= arr[indices[1][intarray2range()]]; // column 1 in arr but that's not what I'm hoping to achieve. Can someone explain the behavior above and/or suggest a way of creating a 2d column major array_view of a 2d multi_array? Thanks - Chris Dion _________________________________________________________________ Show Your Messenger Buddies How You Really Feel http://www.freemessengeremoticons.ca/?icid=EMENCA122
On 8/22/07, chris dion <evolocity@hotmail.com> wrote:
Hi
I'm having a problem with the boost/multi_array and I'm hoping someone here can help, or at least explain why the following doesn't work.
What you are trying is not possible with the views (from your code it seems you misunderstood the way they work - take a second look at the docs here<http://www.boost.org/libs/multi_array/doc/user.html#sec_views>). You can solve your problem with a multi_array_ref and a different storage ordering. Here is an example: using namespace boost; typedef multi_array<int,2> arr2; typedef multi_array_ref<int,2> rarr2; typedef array<arr2::index, 2> shape2; // // create and fill array // shape2 sh = {3,4}; arr2 foo(sh); for (int i=0; i!=sh[0]; ++i) for (int j=0; j!=sh[1]; ++j) foo[i][j] = i + j*10; // // create a reference to transposed shape // shape2 shT = {4,3}; rarr2 fooT(foo.data(), shT, fortran_storage_order()); for (int i=0; i!=sh[0]; ++i) for (int j=0; j!=sh[1]; ++j) assert( foo[i][j] == fooT[j][i] ); hth Levent -- Server Levent Yilmaz Mechanical Engineering University of Pittsburgh
participants (2)
-
chris dion
-
Server Levent Yilmaz