Akavall
Akavall

Reputation: 86178

Indexing Matrix in Boost

I am a python programmer, and I am trying to understand boost.

Here is what I got:

>>> import numpy as np
>>> a
array([[ 0,  1,  2],
       [ 3,  4,  5],
       [ 6,  7,  8],
       [ 9, 10, 11]])
>>> b
array([[20, 21, 22],
       [23, 24, 25]])
>>> a[[0,2]] = b
>>> a
array([[20, 21, 22], # first row of b
       [ 3,  4,  5],
       [23, 24, 25],  # second row of b
       [ 9, 10, 11]])

I can do this using boost:

#include <iostream>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>

using namespace std;

int main()
{

  using namespace boost::numeric::ublas;
  matrix<double> a (4, 3);
  for (unsigned i = 0; i < a.size1 (); ++ i)
      for (unsigned j = 0; j < a.size2 (); ++ j)
        a (i, j) = a.size2() * i + j;


  matrix<double> b (2,3);
  for (unsigned i = 0; i < b.size1 (); ++ i)
      for (unsigned j = 0; j < b.size2 (); ++ j)
        b (i, j) = b.size2() * i + j + 20;


  cout << "matrix a : " << a << endl;
  cout << "matrix b : " << b << endl;

  // replace row 0 in a with row 0 in b
  // replace row 2 in a with row 1 in b

  unsigned rows[] = {0,2};
  int length = sizeof(rows) / sizeof(rows[0]);

  for(int i = 0; i < length; i++)
    for(int j = 0; j < a.size2(); j++)
      a(rows[i], j) = b(i, j);

  cout << "matrix a : " <<  a << endl;

  return 0;
}

However, I am not sure whether this is the best way to do it. Looking at the documentation I did not see a built in method for indexing, so it looks like looping is the only option, am I missing anything?

However, looping in C++ might not be that bad. Looping in python in python is slow, and my above example looping takes place at the C level. But looping is not slow in C++, and hence even if we have to manually loop, the code is still efficient is that correct?

Please let me know if my understanding is correct and/or I am missing a better way to implement the above.

Upvotes: 3

Views: 708

Answers (1)

Praetorian
Praetorian

Reputation: 109119

Boost.MultiArray is better suited for this sort of indexing. Your Python example can be reproduced as follows:

// helper type for a 2d matrix of ints
using array_type = boost::multi_array<int, 2>;

array_type a(boost::extents[4][3]);   // 4x3 matrix
array_type b(boost::extents[2][3]);   // 2x3 matrix

Now fill these matrices

std::iota(a.data(), a.data() + a.num_elements(), 0);
std::iota(b.data(), b.data() + b.num_elements(), 20);

Then define a lambda function that'll print a 2d matrix, so we can see the contents

auto array_printer = [](array_type const& arr) {
  for(auto const &row : arr) {
    for(auto const& elem : row) {
      std::cout << std::setw(2) << elem << ' ';
    }
    std::cout << '\n';
  }
  std::cout << '\n';
};

Let's print what we have so far

std::cout << "Before:\na =\n";
array_printer(a);
std::cout << "b =\n";
array_printer(b);

Output:

Before:
a =
 0  1  2 
 3  4  5 
 6  7  8 
 9 10 11 

b =
20 21 22 
23 24 25 

Time to create a 2d view of the array

using range_type = boost::multi_array_types::index_range;
using view_type = array_type::array_view<2>::type;

// Create a 2d view of a 
//  - the rows of the view consist of rows [0, 3) with a stride of 2
//  - the columns of the view consist of all columns of a
view_type a_view = a[boost::indices[range_type(0,3,2)][range_type()]];

Now assign b to the view we created, and print the result

a_view = b;
std::cout << "After:\na =\n";
array_printer(a);

Output:

After:
a =
20 21 22 
 3  4  5 
23 24 25 
 9 10 11 

Live demo

Upvotes: 4

Related Questions