0

I am trying to extract a sub-array from a multi_array. For this demo, let's assume that there are no collapsed dimensions (i.e. the dimensionality of the sub-array is the same as the original array). I think I am constructing a view with the requested extents correctly (although awkwardly...), but now how do I copy the data from the requested indices (aka all indices of the view) into the sub-array? Here is an outline:

#include <boost/multi_array.hpp>

const unsigned int Dimension = 3;
using ArrayType = boost::multi_array<double, Dimension>;
using IndexType = boost::array<ArrayType::index, Dimension>;

ArrayType ExtractSubGrid(const ArrayType& array, const typename boost::detail::multi_array::index_gen<Dimension, Dimension>& indices)
{
    typename ArrayType::template const_array_view<Dimension>::type view = array[indices];

    IndexType subArraySize;
    for(size_t dim = 0 ; dim < Dimension; ++dim) {
        subArraySize[dim] = indices.ranges_[dim].finish_ - indices.ranges_[dim].start_;
    }

    ArrayType subArray;
    subArray.resize(subArraySize);

    // How to do this?
    //subArray.data() = view.data();

    return subArray;
}

int main()
{
    ArrayType myArray(IndexType({{3,4,2}}));

    boost::detail::multi_array::index_gen<3,3> indices = boost::indices[ArrayType::index_range(0,2)][ArrayType::index_range(1,3)][ArrayType::index_range(0,4)];

    ArrayType subGrid = ExtractSubGrid(myArray, indices);

    return 0;
}
sehe
  • 374,641
  • 47
  • 450
  • 633
David Doria
  • 9,873
  • 17
  • 85
  • 147
  • I'd expect `subArray = view;` to be more logically close to intended interface. No time to test things now though – sehe Feb 29 '16 at 15:05
  • @sehe Wow, that compiles, and is exactly what I wanted. Now just need to test that the correct data comes along for the ride. – David Doria Feb 29 '16 at 15:07

1 Answers1

0

Would something like this help:

std::copy(view.begin(), view.end(), subArray.begin());
Samer Tufail
  • 1,835
  • 15
  • 25
  • That would be nice, as long as we're sure that is copying the indices in the correct order? I'm trying to test by filling some data, but gettings lots of errors with something (seemingly) simple like this: ` unsigned int i = 0; for(auto itr = myArray.begin(); itr != myArray.end(); ++itr) { *itr = data[i]; i++; }` ? It doesn't seem to dereference `itr` to a mutable double? – David Doria Feb 29 '16 at 15:02
  • Would it be because of the `const` ness? – Samer Tufail Feb 29 '16 at 15:06
  • I tried `ArrayType::iterator` instead of `auto` in the loop to make sure it wasn't getting a const iterator and it still has the same compiler errors. They are deep inside subarray.hpp (`request for member num_dimensions in 'other', which is of non-class type 'const double', etc.). – David Doria Feb 29 '16 at 15:09