Search code examples
c++vectorgsl

Is it possible to combine GSL vectors into a single vector?


I am using the Gnu Scientific Library (GSL), where I have initialized different vectors.

Now I want to combine these vectors into a single vector in order to iterate over the full vector. Does anyone know of a method, where it is possible to do this?

This question discusses the same problem in a more general fashion, but I wanted to know if anyone knew a way to do this directly using GSL (I am to use the sort function implemented in GSL afterwards).

Thank you, Rasmus


Solution

  • If by " initialized different vectors " you meant initialized different std::vectors, then the answer of your question is here use std::assign.

    EDIT 1: In this case, std::assign is the best answer (and not std::copy as many places would suggest) because std::copy will insert the new elements one by one (instead of inserting the whole array at once) and that can cause multiple reallocation (meaning: when you try to insert a new element to a vector where its current size (given by std::vector::size) is equal to its current capacity (given by std::vector::capacity), a reallocation is made that doubles vector capacity. Depending of the size of your vector, this can happen multiple times and it is a very (very!) expensive operation. With std::assign this will only happen once.

    If not (meaning you have a collection of gsl_vectors), then in principle it is possible to use STL algorithms with C arrays see here (gsl_vectors holds a C array called data). However it is quite dangerous because the way the memory is aligned inside gsl_vectors is tricky. In this case, you need to convert them manually to std::vector or merge then manually to a bigger gsl_vector)

    But, unless you need to implement matrices or need very fast BLAS operations with vectors (see here) I would always work with std::vector (and use std::vector::data to pass the C pointers to GSL functions). For these two exceptions, you should use Armadillo Linear Algebra Package or Blaze if you want to work in C++ (otherwise you would need to write a wrapper or code like C).