如何用GSL数学库求解矩阵方程组?

gsl_matrix *CalculateAxb(const gsl_matrix *A,const gsl_matrix *b)
{
    int i;
    int size1=A->size1;
    gsl_matrix *tempA=gsl_matrix_alloc(size1,size1);
    gsl_matrix_memcpy(tempA,A);
    gsl_matrix *tempb=gsl_matrix_alloc(1,size1);
    gsl_matrix_memcpy(tempb,b);

    gsl_vector *vectorbx=gsl_vector_alloc(size1);
    for(i=0;i<size1;i++)
        gsl_vector_set(vectorbx,i,gsl_matrix_get(tempb,0,i));
    gsl_vector *x=gsl_vector_alloc(size1);
    int s;
    gsl_permutation *px=gsl_permutation_alloc(size1);
    gsl_linalg_LU_decomp(tempA,px,&s);
    gsl_linalg_LU_solve(tempA,px,vectorbx,x);
    gsl_matrix *temp=gsl_matrix_alloc(1,size1);
    for(i=0;i<size1;i++)
        gsl_matrix_set(temp,0,i,gsl_vector_get(x,i));
    gsl_matrix_free(tempA);
    gsl_matrix_free(tempb);
    gsl_vector_free(vectorbx);
    gsl_vector_free(x);
    gsl_permutation_free(px);
    return temp;
}

讲解

gsl_linalg_LU_decomp

函数原型:

int gsl_linalg_LU_decomp(gsl_matrix *A,gsl_permutation *p,int *signum)

1.这个函数将矩阵A进行LU分解。

在线性代数中,LU分解是矩阵分解的一种,可以将一个矩阵分解为一个下三角矩阵和一个上三角矩阵的乘积(有时是它们和一个置换矩阵的乘积)。

这个函数的LU分解则是将一个矩阵和置换矩阵的乘积分解为一个下三角矩阵和一个上三角矩阵即PA=LU。


P:置换矩阵;
L:the lower triangular matrices;
U:the upper triangular matrices.

2.LU分解的算法

部分选主元的高斯消元法 Gaussian Elimination with partial pivoting
(Golub & Van Loan, Matrix Computations, Algorithm 3.4.1)

评论

暂无评论