调整大小 boost::multi_array 以匹配另一个

resizing boost::multi_array to match another

我需要将一个 multi_array 调整为另一个的大小。

在 Blitz++ 中我可以做到

arr1.resize(arr2.shape());

是否有类似长度的multi_array解决方案?因为

arr1.resize(boost::extents[arr2.shape()[0]][arr2.shape()[1]]);

好像有点长,有点费劲。

您可以使用 shape() 成员。遗憾的是,它不能直接用作 ExtentList(它没有模拟 Collection 概念),但很容易将其变成一个:

using MA = multi_array<double, 2>;
MA ma(extents[12][34]);
auto& ma_shape = reinterpret_cast<boost::array<size_t, MA::dimensionality> const&>(*ma.shape());

所以

// demo
std::cout << "[" << ma_shape[0] << "][" << ma_shape[1] << "]\n";

打印 [12][34].

现在,ma_shape可以直接用于reshape/resize另一个数组:

Live On Coliru

#include <boost/multi_array.hpp>
#include <iostream>

int main() {
    using namespace boost;
    using MA = multi_array<double, 2>;

    MA ma(extents[12][34]);
    auto& ma_shape = reinterpret_cast<boost::array<size_t, MA::dimensionality> const&>(*ma.shape());

    // demo
    std::cout << "[" << ma_shape[0] << "][" << ma_shape[1] << "]\n";

    // resize
    MA other;
    assert(!std::equal(ma_shape.begin(), ma_shape.end(), other.shape()));

    other.resize(ma_shape);
    assert(std::equal(ma_shape.begin(), ma_shape.end(), other.shape()));

    // reshape
    other.resize(extents[1][12*34]);
    assert(!std::equal(ma_shape.begin(), ma_shape.end(), other.shape()));

    other.reshape(ma_shape);
    assert(std::equal(ma_shape.begin(), ma_shape.end(), other.shape()));
}

我认为这是对 Boost.MultiArray 的又一次疏忽。我写了一堆 "utility" 函数,允许采用形状(维度大小)、基本索引(例如每个维度为 0 或 1)和扩展(每个维度中的基本索引和大小)。

namespace boost{
template<class MultiArray>
detail::multi_array::extent_gen<MultiArray::dimensionality>
extension(MultiArray const& ma){ //this function is adapted from 
    typedef detail::multi_array::extent_gen<MultiArray::dimensionality> gen_type;
    gen_type ret;
    typedef typename gen_type::range range_type;
    for(int i=0; i != MultiArray::dimensionality; ++i)
        ret.ranges_[i] = range_type(ma.index_bases()[i], ma.index_bases()[i]+ma.shape()[i]);
    return ret;
}
}

后来用作:

boost::multi::array<double, 3> m(boost::multi::extents[3][4][5]);
boost::multi::array<double, 3> n(extension(m)); // n takes the extension (and shape) of m

(如果基本索引不为零,它也适用。它也适用于视图和其他 multi_array-type 类。)