gpt4 book ai didi

c++ - 如何使用 gsl 在 cholesky 中打印下矩阵?

转载 作者:行者123 更新时间:2023-11-30 05:01:15 25 4
gpt4 key购买 nike

我将 gsl 与 c++ 结合使用,通过 Cholesky 方法求解线性系统,一切正常,但我想获取并打印下矩阵 L。我在官方文档中找不到有关此问题的任何信息。这是我的代码:

#include <iostream>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_linalg.h>


using namespace std;

int main(void) {

double a_data[] = { 4, -1, 1,
-1, 4.25, 2.75,
1, 2.75, 3.5};

double b_data[] = {1, 2, 3};

gsl_matrix_view A = gsl_matrix_view_array (a_data, 3, 3);
gsl_vector_view b = gsl_vector_view_array (b_data, 3);
gsl_permutation * p = gsl_permutation_alloc (3);
gsl_vector *x = gsl_vector_alloc (3);
gsl_matrix *L = gsl_matrix_alloc (3, 3);



gsl_linalg_pcholesky_decomp(& A.matrix, p);
gsl_linalg_pcholesky_solve(& A.matrix, p, &b.vector, x);
cout<<"x = "<<endl;
gsl_vector_fprintf (stdout, x, "%g");
cout<<"p = "<<endl;
gsl_permutation_fprintf (stdout, p, "%d");
cout<<endl;
cout<<"L = "<<endl;
gsl_matrix_fprintf (stdout, L, "%g");

gsl_permutation_free (p);

return 0;
}

最佳答案

在原矩阵A中覆盖下矩阵L,矩阵A的下三角部分,存储L。

关于c++ - 如何使用 gsl 在 cholesky 中打印下矩阵?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50397996/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com