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

发布于 2025-01-28 10:55:03 字数 2263 浏览 4 评论 0原文

我想从LDLT分解中获取矩阵q的矩阵l和dscipy.linalg.linalg.dl()的相同结果,这是代码:

#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");
  }

我的编译args是gcc> gcc ldl.c -lm -llm -llapack -lblas -lglas -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

为什么 ?我做什么?

I want to get the matrix l and d of matrix Q from LDLT decomposition. The same result of scipy.linalg.ldl(),here is the code:

#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");
  }

my compile args is gcc ldl.c -lm -llapack -lblas -lgsl

But it returns;

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

WHy ? What shoud I do?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白衬杉格子梦 2025-02-04 10:55:03

通过命令行编译GSL程序的最佳方法也许是使用GSL本身提供的标志。可以通过公用事业读取它们

gsl-config


在您的情况下,如下所示(如果您的外壳可以正确处理反向引用):

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

如果没有,请直接将gsl-config的输出直接用作命令行参数。

无论如何,您的代码会在我的系统上编译并运行,没有任何问题。

Perhaps the best way of compiling GSL programs via the command-line is by using the flags provided by GSL itself. They can be read via the

gsl-config

utility.
In your case use it as follows (providing your shell can properly process the backquoteses):

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

If not, use the output of gsl-config --libs directly as the command line arguments.

Anyway, your code compiles and runs on my system without any problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文