我可以删除boost :: multi_array的单个维度而不复制其内容吗?

时间:2013-07-16 09:57:12

标签: c++ boost multidimensional-array

以下是我从形状[3 4 2]重塑boost :: multi_array到[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;                                                                                                                                                                                                                                                                                                                
}   

2 个答案:

答案 0 :(得分:1)

原来可以通过查看(c.f。Creating Views)来实现这一点。

在我的例子中:

... reshape ...

typedef boost::multi_array_types::index_range range;                                                                       
array_type::index_gen indices;  
array_type::array_view<2>::type myview =       
    A[ indices[range(0,12)][range(0,2)][0] ];                                   
for(index i = 0; i != 12; ++i)      
  for(index j = 0; j != 2; ++j){  
    std::cout << "myview[" <<i <<"]["<<j<<"] = " << myview[i][j] << std::endl;  

答案 1 :(得分:1)

我不认为你可以在一般情况下这样做。您可以做的是生成具有较低维度的子视图。我读了你想要让你的2D数组元素与原始3D数组一样多的代码。

您可以做的是使用const_multi_array_ref重复使用3D数组的现有数据:

boost::multi_array< int, 3 > a( boost::extents[ 2 ][ 3 ][ 4 ] );
boost::const_multi_array_ref< int, 2 > b( a.data(), boost::extents[ 2 ][ 12 ] );

根据存储顺序,这可能会使您尝试获取。

我建议在multi_array周围编写一个小包装器,使用所需的索引计算访问multi_array