0

I have a 2 dimensional data(row x col), which was reading in a boost::multi_array container. I also need to know if I can read this data into ublas::vector, e.g., data has three rows and read them into three vectors v1, v2, v3: I am not very familiar with the interface of ublas::vector. Data is stored in a .h5 file and in order to read I'm using this library. Can anyone show me how to replace boost::multi_array with the ublas::vector? Suggestion with some other example is also appreciated. Thanks!

#include <boost/multi_array.hpp>
#include <h5xx/h5xx.hpp>
#include <iostream>
#include <boost/numeric/ublas/vector.hpp>

using array_2d_t = boost::multi_array<float, 2>;

template <typename T>
void print_array(T const& array)
{
    for (auto const& row : array)
        {   for (auto v : row)
            printf("%10f ", v);
        printf("\n");
    }
    std::cout << "\n End of file " << std::endl;
}

array_2d_t read_frame(std::string const& filename) {
    
    h5xx::file xaa(filename, h5xx::file::mode::in);

    h5xx::group   g(xaa, "particles/lipids/box/edges");
    h5xx::dataset ds(g, "box_size");

    auto ds_shape = h5xx::dataspace(ds).extents<2>();
    array_2d_t arr(boost::extents[ds_shape[0]][ds_shape[1]]);

    h5xx::read_dataset(ds, arr);
    return arr;
}

int main(int argc, char const* argv[]) {
    if ( argc < 2) {
        std::cout << "Usage: " << argv[0] << " input.h5 " << std::endl;
        return -1;
    }

    std::string filename(argv[1]);

    auto count = read_frame(filename);
    std::cout << "Frames in file: " << count[1][1] << "\n";
    print_array(count);
    return 0;
}
Mahesh
  • 556
  • 1
  • 3
  • 16
  • Just asking, how come you don't just use `std::vector>` and then `using array_2d = std::vector>`? It will allow you to avoid the need for a third-party library. Is there a reason you are interested in `ublast:vector`? Again just curious – Lars Nielsen Jan 24 '22 at 10:31
  • 1
    @LarsNielsen Yes indeed there is reason to use `ublas::vector`. I will be using these vectors for operations like `/,+,-``etc. with variables of same type from some library. So it is better to change the type now, rather than struggle with them later. – Mahesh Jan 24 '22 at 10:37
  • Ublas vectors are dynamically allocated. This isn't going to scale to your data set. – sehe Jan 24 '22 at 11:37

0 Answers0