首页 > 解决方案 > 如何使用gsl在cholesky中打印较低的矩阵?

问题描述

我正在使用 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;
}

标签: c++gsl

解决方案


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


推荐阅读