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;
}
2
  • I'd expect subArray = view; to be more logically close to intended interface. No time to test things now though Commented Feb 29, 2016 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. Commented Feb 29, 2016 at 15:07

1 Answer 1

0

Would something like this help:

std::copy(view.begin(), view.end(), subArray.begin());
Sign up to request clarification or add additional context in comments.

3 Comments

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?
Would it be because of the const ness?
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.).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.