If you want to stick to GSL, but not set all vector coordinates individually you can use gsl_vector_view
obtained from gsl_vector_subvector
.
To this end, allocate the output gsl_vector large enough to hold the concatenation of all your different vectors. Then for each of these use gsl_vector_subvector
to obtain a gsl_vector_view to the portion of the output vector. You can then gsl_vector_memcpy
from each of your input vectors to that corresponding portion. Note that a gsl_vector_view is a struct which contains a gsl_vector called vector
:
#include <stdio.h>
#include <gsl/gsl_vector.h>
#define length1 4
#define length2 6
int main () {
/* allocate all vectors */
gsl_vector
*vectorIn1 = gsl_vector_alloc( length1 ),
*vectorIn2 = gsl_vector_alloc( length2 ),
*vectorOut = gsl_vector_alloc( length1+length2 );
/* fill input vectors with some test data */
for ( size_t index = 0; index < length1; ++index ) {
gsl_vector_set( vectorIn1, index, -(double)index );
}
for ( size_t index = 0; index < length2; ++index ) {
gsl_vector_set( vectorIn2, index, (double)index );
}
/* perform the copy to portions of the output */
{
gsl_vector_view
viewOut1 = gsl_vector_subvector( vectorOut, 0, length1 ),
viewOut2 = gsl_vector_subvector( vectorOut, length1, length2 );
gsl_vector_memcpy( &viewOut1.vector, vectorIn1 );
gsl_vector_memcpy( &viewOut2.vector, vectorIn2 );
}
/* display the result to see it is correct */
for ( size_t index = 0; index < length1 + length2; ++index ) {
printf( "%3.1f\n", gsl_vector_get( vectorOut, index ) );
}
/* be nice and tidy: release resources after use */
gsl_vector_free( vectorOut );
gsl_vector_free( vectorIn2 );
gsl_vector_free( vectorIn1 );
}