0

I want to add two boost multi-arrays in C++.

  1. The first challenge is the alignment of the two arrays. One of these arrays is vertically aligned and the other is horizontally aligned as shown in the figure below. They shall be added, resulting in a 3-dimensional array (see also below). The easiest/laziest way would be to duplicate the vertically aligned array along its y-axis until it has the same width as the horizontally aligned array. Additionally duplicating the horizontally aligned array along its x-axis until it has the same height as the vertically aligned array. Then I would need a possibility to add two 3-dimensionally boost multi-arrays.
  2. The second challenge is, that adding boost multi-arrays is not natively supported by boost, or I am wrong on this point? I want to avoid for-loops and take advantage of the straight allocated memory which makes e.g. copy operations less time intensive.

Does anybody have a good recommendation to handle this quest?

vertically aligned and horizontally aligned arrays resulting cube

UweJ
  • 447
  • 3
  • 10

2 Answers2

0

After a lot of thoughts about it, I think the best solution will be to extend one of these 2d arrays to an 3d array. Then I can sum the 2d matrix slide-wise to the 3d matrix.

UweJ
  • 447
  • 3
  • 10
  • That would be very inefficient as you will need to copy the slices several times, if I understand the problem correctly. – alfC Feb 27 '23 at 07:14
0
  1. A naive but correct way to do this is:
    boost::multi_array<int, 2> A(boost::extents[5][5]);
    boost::multi_array<int, 2> B(boost::extents[5][5]);

    boost::multi_array<int, 3> C(boost::extents[5][5][5]);

    for(auto i = 0; i != C.shape()[0]; ++i) {
        for(auto j = 0; j != C.shape()[1]; ++j) {
            for(auto k = 0; k != C.shape()[2]; ++k) {
                C[i][j][k] = A[i][j] + B[j][k];
            }
        }
    }

    assert( C[1][2][3] == A[1][2] + B[2][3] ); 

https://godbolt.org/z/4jP1PbjYo

  1. You are correct, Boost.MA doesn't have arithmetic operations for arrays.
alfC
  • 14,261
  • 4
  • 67
  • 118