[multi-array] Extracting dimension information from a multi-array
From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Ryan McConnehey Sent: 12 September 2010 07:37 To: Boost-users Subject: [Boost-users] [multi-array] Extracting dimension information from a multi-array I'm trying to extract dimension information from a boost multi-array. The documentation gives the following short example. typedef boost::multi_array<double, 3> array_type; typedef array_type::index index; array_type A(boost::extents[3][4][2]); // Assign values to the elements int values = 0; for(index i = 0; i != 3; ++i) for(index j = 0; j != 4; ++j) for(index k = 0; k != 2; ++k) A[i][j][k] = values++; This is okay if you know the dimension information. Most of the time, though, you don't want to hard code the dimensions into your loop. The multi_array::dimensionality specifies the dimensions of the array. How do I find the max elements of each dimension? Ryan _____ I believe the shape() member function is what you're looking for,
Ryan McConnehey <mccorywork <at> gmail.com> writes:
Do you have an example of how shape() works? The shape method isn't used in any of the multi-array tutorials. Also, the reference material says the shape method returns a list of "NumDims" but the return signature specifies a "const size_type *" is returned. How is "const size_type *" a list of dimension?
Since it is not a container, you will need to use the ::dimensionality to see how far to iterate this bare pointer. e.g. to put it into a bounded ublas vector vector: const static std::size_t rank = ublas::multi_array<double, 3>::dimensionality; //Or just 3 in this case... Showing you this could work in generic code ublas::bounded_vector<std::size_t, rank> dims; std::copy(var.shape(), var.shape() + rank, dims.begin());
Ryan McConnehey <mccorywork <at> gmail.com> writes:
Since the above code works we can access a specific dimension like so. var.shape()[1] //as long as the operator values exists within the dimension
Yes.Since the type returned from var.shape() is a raw pointer, you should be able to access it under all circumstances like this.
I just viewed this while trying to figure out how to access the dimensionality of MultiArray Since the above code works we can access a specific dimension like so.
var.shape()[1] //as long as the operator values exists within the dimension
Yes.Since the type returned from var.shape() is a raw pointer, you should be able to access it under all circumstances like this.
Is raw pointers seriously how we should be accessing Boost.MultiArray in the age of std::vector? I was expecting something like (which is what I use in my code that does not use MultiArray which I have here adapted to use MultiArray) typedef std::vector<size_t> dimensions_t; typedef boost::multi_array<double, 2> TForm_t; dimensions_t dimensions( TForm_t t ) { dimensions_t dims; TForm_t::size_type dim; for( int i = 0; i < t.num_dimensions(); i++ ) dims.push_back(t.shape()[i]); return dims; } dimensions_t dims = dimensions(t); At least with above I get a std::vector access exception when accessing dims[2] or greater. Using raw pointers I just get random memory values... awesome. I'll add these to my pool of lottery numbers. It seems as though Boost.MultiArray is designed to hide pointers and bookeeping only to leave the programmer "hanging" when trying to get something simple such as get the dimensions. Clearly this is was well thought out. Am I missing something here?
participants (4)
-
Brian Davis
-
Hicham Mouline
-
Jesse Perla
-
Ryan McConnehey