下面是我将 boost::multi_array 从 shape 重塑[3 4 2]
为[12 2]
. 由于维度在 boost::multi_array 中是固定的,所以我首先将三维数组重塑为形状[12 2 1]
,然后将前两个维度复制到一个新数组中。
是否有更简单的方法不进行任何复制(我更喜欢本着numpy.reshape的精神查看原始数据)。
运行g++ -g test.cc && ./a.out
#include "boost/multi_array.hpp"
#include <cassert>
#include <iostream>
int
main () {
// Create a 3D array that is 3 x 4 x 2
typedef boost::multi_array<double, 3> array_type;
typedef array_type::index index;
array_type A(boost::extents[3][4][2]);
// indexer can be const a boost::array
boost::array<array_type::index,3> idx = {{0,0,0}};
// 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++;
std::cout << "array elements: " << A.num_elements() << std::endl;
std::cout << "array ndim: " << A.num_dimensions() << std::endl;
std::cout << "array size: " << A.size() << std::endl; // equivalent to a.shape()[0];
std::cout << "array shape: " << A.shape()[0] <<" " << A.shape()[1] <<" " << A.shape()[2] << std::endl;
int verify = 0;
for(index i = 0; i != 3; ++i)
for(index j = 0; j != 4; ++j)
for(index k = 0; k != 2; ++k) {
std::cout << "A[" <<i <<"]["<<j<<"]["<<k<<"] = " << A[i][j][k] << std::endl;
assert(A[i][j][k] == verify++);
}
boost::array<array_type::index,3> dims2 = {{12, 2, 1}};
A.reshape(dims2);
std::cout << "array shape: " << A.shape()[0] <<" " << A.shape()[1] <<" " << A.shape()[2] << std::endl;
for(index i = 0; i != 12; ++i)
for(index j = 0; j != 2; ++j)
for(index k = 0; k != 1; ++k) {
std::cout << "A[" <<i <<"]["<<j<<"]["<<k<<"] = " << A[i][j][k] << std::endl;
}
typedef boost::multi_array<double, 2> Array2d;
Array2d B(boost::extents[12][2]);
for(index i = 0; i != 12; ++i)
for(index j = 0; j != 2; ++j){
B[i][j] = A[i][j][0];
std::cout << "B[" <<i <<"]["<<j<<"] = " << B[i][j] << std::endl;
}
return 0;
}