簡體   English   中英

GSL:如何在 GSL 中獲得 LDLT 分解

[英]GSL: how to get LDLT decomposition in GSL

我想從LDLT分解中得到矩陣Q的矩陣l and d scipy.linalg.ldl()的結果相同,代碼如下:

#include <gsl/gsl_math.h>                                                                                                                                                                                        #include <gsl/gsl_sf.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_linalg.h>
#include <gsl/gsl_cdf.h>
#include <gsl/gsl_errno.h>
                                                                                                                                                                                                                 int main() {
      const int dim = 3;
      double pars[3] = { 0, 0, 0 };
      double Q[9] = {
          2,-1,0,
          -1,3,-1,
          0,-1,4
      };
      int i=0,j=0;

      // Gaussian Multivariate distribution
      gsl_matrix *L = gsl_matrix_calloc(dim, dim);
      gsl_vector *S = gsl_vector_calloc(dim);
      gsl_permutation * perm = gsl_permutation_calloc(dim);

      for(i=0;i<dim*dim;i++) L->data[i]=Q[i];

>>    gsl_linalg_ldlt_decomp(L);

      for(i=0;i<3;i++){
          for(j=0;j<3;j++){
              printf("%.4f ", L->data[i*3+j]);
          }
          printf("\n");
      }

      printf("\n S=");
      for(i=0;i<3;i++)
          printf("%.4f ", S->data[i]);
      printf("\n");
  }

我的編譯參數是gcc ldl.c -lm -llapack -lblas -lgsl

但它回來了;

ldl.c: In function ‘main’:
ldl.c:39:5: warning: implicit declaration of function ‘gsl_linalg_ldlt_decomp’; did you mean ‘gsl_linalg_PTLQ_decomp’? [-Wimplicit-function-declaration]
   39 |     gsl_linalg_ldlt_decomp(L);
      |     ^~~~~~~~~~~~~~~~~~~~~~
      |     gsl_linalg_PTLQ_decomp
/usr/bin/ld: /tmp/ccSWXMMb.o: in function `main':
ldl.c:(.text+0x170): undefined reference to `gsl_linalg_ldlt_decomp'
collect2: error: ld returned 1 exit status

為什么 ? 我該怎么辦?

也許通過命令行編譯 GSL 程序的最佳方式是使用 GSL 本身提供的標志。 它們可以通過

gsl-config

效用。 在您的情況下,按如下方式使用它(前提是您的外殼可以正確處理反引號):

gcc ldl.c `gsl-config --libs`

如果沒有,直接使用gsl-config --libs的輸出作為命令行參數。

無論如何,您的代碼在我的系統上編譯並運行沒有任何問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM