Manipulating boost::multi_array

Right now, I'm using boost::multi_array for implementing a dynamic-sized matrix for an algorithm in which the number of rows and columns therein may wildly fluctuate (is boost::multi_array even the right class for this?). What's frustrating me is how I can do any and all of the following: 1. Remove a single row or column from a matrix 2. Insert a single row or column to a matrix 3. Augment two matrices together 4. Swap two rows or columns Is there any way to do the first three without calling resize() and moving the elements around manually? For the last, does something like std::swap(A[row1], A[row2]) work?

I too would like answers to these questions.
I tried:
template <typename T>
void display_array_3d( T& array_3d )
{
int z_dim = array_3d.shape()[0];
int y_dim = array_3d.shape()[1];
int x_dim = array_3d.shape()[2];
if( x_dim > 0 && y_dim > 0 ){
for( int z = 0; z < z_dim; z++ ){
for( int y = 0; y < y_dim; y++ ){
for( int x = 0; x < x_dim; x++ ){
printf( "\t%f", array_3d[z][y][x] );
}
printf( "\n" );
}
printf( "\n" );
}
return;
}
if( y_dim > 0 ){
for( int z = 0; z < z_dim; z++ ){
for( int y = 0; y < y_dim; y++ ){
printf( "\t%f", array_3d[z][y] );
}
printf( "\n" );
}
return;
}
if( z_dim > 0 ){
for( int z = 0; z < z_dim; z++ ){
printf( "\t%f", array_3d[z] );
}
return;
}
}
void test_3d_to_2d_matrix( void )
{
int z_dim = 2;
int y_dim = 3;
int x_dim = 4;
boost::shared_ptr<float> array_3d_data = boost::shared_ptr<float>( new
float[x_dim * y_dim * z_dim] );
for( int i = 0; i< x_dim * y_dim * z_dim; i++){
(array_3d_data.get())[i] = i;
}
typedef boost::multi_array_ref

Something else I noticed: array_type::array_view<3>:: type row_2 = a_3d_array[ boost::indices[range(0, z_dim)][range(1,1)][range(0,0)] ]; was perfectly acceptable from a boost::multi_array ... why? dimensions of zero thickness should not be allowed. That is the ranges range(1,1) and range(0,0) should not be allowed imho. Of course if I can enter the zeroth dimenson of infintesmal thickness then all would be good.

On 11/29/11 19:46, Brian Davis wrote:
Something else I noticed:
array_type::array_view<3>:: type row_2 = a_3d_array[ boost::indices[range(0, z_dim)][range(1,1)][range(0,0)] ];
was perfectly acceptable from a boost::multi_array ... why? dimensions of zero thickness should not be allowed. That is the ranges range(1,1) and range(0,0) should not be allowed imho. Of course if I can enter the zeroth dimenson of infintesmal thickness then all would be good.
If a multi_array with 0 elements is not allowed, then a std::list<T>
with no elements should not be allowed. To be more concrete, imagine:
multi_array
participants (3)
-
Brian Davis
-
kelvSYC
-
Larry Evans