如何获得gsl_vector_view
的大小?我具有以下用于采样多元高斯向量的功能
void mvnrnd(gsl_vector *x, gsl_matrix *Sigma,gsl_vector *Mu, int K, const gsl_rng *seed){
gsl_matrix *A = gsl_matrix_alloc(K, K);
gsl_matrix_memcpy(A, Sigma);
gsl_linalg_cholesky_decomp (A);
for (int k=0; k<K; k++){
gsl_vector_set (x, k, gsl_ran_ugaussian (seed));
}
gsl_blas_dtrmv (CblasLower, CblasNoTrans, CblasNonUnit, A, x);
gsl_vector_add (x, Mu);
}
为了传递向量和矩阵,我正在做以下事情:
gsl_matrix *MuH = gsl_matrix_calloc(Kest*Kest,1);
gsl_matrix *vecH = gsl_matrix_calloc(Kest*Kest,1);
gsl_matrix *SigmaH = gsl_matrix_calloc(Kest*Kest,Kest*Kest);
gsl_matrix_memcpy (SigmaH, &Q_view.matrix);
gsl_vector_view vecH_view = gsl_matrix_subcolumn (vecH, 0, 0, Kest*Kest);
gsl_vector_view MuH_view = gsl_matrix_column (MuB, 0);
mvnrnd(&vecH_view.vector, SigmaH , &MuH_view.vector, Kest*Kest, seed);
但我收到此错误:
gsl: ./oper_source.c:27: ERROR: vectors must have same length
Default GSL error handler invoked.
所以我想打印gsl_vector_view
。怎么做呢?
我使用了& vecH_view.vector->size
,它返回错误。
答案 0 :(得分:1)
“试错”始终对我有用。我在下面提供一个最小的示例:
#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
int main()
{
gsl_matrix * y = gsl_matrix_calloc(6,6);
gsl_vector_view z = gsl_matrix_column(y,0);
printf("size = %ld\n",(&z.vector)->size);
gsl_matrix_free(y);
}
$ gcc -Wall -pedantic-errors -O0 se.c -lgsl -lgslcblas -lm && ./a.out
size = 6