获取gsl_vector_view的大小

getting the size of gsl_vector_view

本文关键字:vector gsl 获取 view      更新时间:2023-10-16

如何获得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,它返回错误。

"

反复试验"总是对我有用。我在下面提供了一个最小的例子:

#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 = %ldn",(&z.vector)->size);
    gsl_matrix_free(y);
}
$ gcc -Wall -pedantic-errors -O0 se.c -lgsl -lgslcblas -lm && ./a.out
size = 6