我需要将gsl_vector
传递给期望C样式数组的函数,反之亦然。
缓慢的方法(涉及深层复制)应该是:
const size_t n = 4;
gsl_vector gx; // initialize and fill
gsl_vector gy; // initialize
double in[n], out[n];
for(size_t i = 0; i < n; ++i)
in[i] = gsl_vector_get(gx, i);
func(in, out, n);
for(size_t i = 0; i < n; ++i)
gsl_vector_set(gy, i, out[i]);
。
我可以这样做:
const size_t n = 4;
gsl_vector gx; // initialize and fill
gsl_vector gy; // initialize
func(gx.data, gy.data, n);
答案 0 :(得分:2)
您可以从C数组开始,然后使用gsl_vector_view_array(文档here)将其转换为没有深层副本的gsl_vector!然后,您可以在gsl中运行所需的计算,然后,您可以将相同的数组传递给任何C函数。
// something like
int size = 10
double* xarray = new double[size] // you can use malloc here. Irrelevant to the answer
gsl_vector_view xarray_gsl = gsl_vector_view_array ( xarray, size );
// Now xarray_gsl.vector is a gsl_vector that you can send to any gsl routine
// After that you can send the original xarray to any C function
// No deep copies are involved