关于Lapack中的函数 cgetri dgetri sgetri zgetri 的参数解释

ubuntu 

$ sudo apt-get install build-essential

相关官方文档:

Determining the Block Size for Block Algorithms

先运行一个完整的示例:

// LAPACK debug code
//compile with: gfortran helloDgetrf.c -llapack -lblas -o helloDgetrf
 
#include <stdio.h>
#include <lapacke.h>
 
//print matrix A(mxn)
lapack_int printMatrix(int matrix_layout, lapack_int m, lapack_int n, double* a_matrix, lapack_int lda){
 
        if(matrix_layout == LAPACK_COL_MAJOR){
                for(int i=0;i<m;i++){
                        printf("\n");
                        for(int j=0;j<n; j++){
                                printf("  %8.4f  ",a_matrix[i + j*lda]);
                        }
                }
        }else if(matrix_layout == LAPACK_ROW_MAJOR){
                for(int i=0;i<m;i++){
                        printf("\n");
                        for(int j=0;j<n; j++){
                                printf("  %8.4f  ",a_matrix[i*lda + j]);
                        }
                }
 
        }else{
                printf("Parameter matrix_layout is error!\n");
 
                return -1;
        }
 
        return 0;
}
 
int main()
{
        int dim = 2;
        int nrhs = 1;
        int LDA = dim;
        int LDB = dim;
        int info=dim+1;
        double A[4] = {1.0, 1.0, 1.0, -1.0};
        /**   A*x = b    **/
 
        int ipiv[3]={0};
 
        printf("Origin Matrix A=\n");
 
//      lapack_int printMatrix(int matrix_layout, lapack_int m, lapack_int n, double* a_matrix, lapack_int lda)
        printMatrix(LAPACK_COL_MAJOR, dim, dim, A, dim);
 
        //lapack_int LAPACKE_dgetrf( int matrix_layout, lapack_int m, lapack_int n, double* a, lapack_int lda, lapack_int* ipiv )
        //lapack_int LAPACKE_dgetri( int matrix_layout, lapack_int n, double* a, lapack_int lda, const lapack_int* ipiv )
 
        info = LAPACKE_dgetrf(LAPACK_COL_MAJOR, dim, dim, A, LDA, ipiv);
 
        if(info != 0){  printf("Singular Matrix, INFO = %d\n",info); }
 
        info = LAPACKE_dgetri(LAPACK_COL_MAJOR, dim, A, dim, ipiv);
 
        if(info != 0){  printf("Singular Matrix, INFO = %d\n",info); }
 
        printf("info=%d\n LU_aa:\n",info);
        printMatrix(LAPACK_COL_MAJOR, dim, dim, A, dim);
        printf("\n\n");
 
        return(0);
}

c — Computando o inverso de uma matriz usando lapack em C

https://stackoverflow.com/questions/3519959/computing-the-inverse-of-a-matrix-using-lapack-in-c

int main(){

    double M[3][3] = { {1 , 2 , 3},
                       {4 , 5 , 6},
                       {7 , 8 , 9}}
    double pivotArray[3]; //since our matrix has three rows
    int errorHandler;
    double lapackWorkspace[9];

    // dgetrf(M,N,A,LDA,IPIV,INFO) means invert LDA columns of an M by N matrix 
    // called A, sending the pivot indices to IPIV, and spitting error 
    // information to INFO.
    // also don't forget (like I did) that when you pass a two-dimensional array
    // to a function you need to specify the number of "rows"
    dgetrf_(3,3,M[3][],3,pivotArray[3],&errorHandler);
    //some sort of error check

    dgetri_(3,M[3][],3,pivotArray[3],9,lapackWorkspace,&errorHandler);
    //another error check

}

First, M has to be a two-dimensional array, like double M[3][3]. 
Your array is, mathematically speaking, a 1x9 vector, which is not invertible.

N is a pointer to an int for the order of the matrix - in this case, N=3.

A is a pointer to the LU factorization of the matrix, 
which you can get by running the LAPACK routine dgetrf.

LDA is an integer for the "leading element" of the matrix, 
which lets you pick out a subset of a bigger matrix if you want to just invert a little piece. 
If you want to invert the whole matrix, LDA should just be equal to N.

IPIV is the pivot indices of the matrix, in other words, 
it's a list of instructions of what rows to swap in order to invert the matrix. 
IPIV should be generated by the LAPACK routine dgetrf.

LWORK and WORK are the "workspaces" used by LAPACK. 
If you are inverting the whole matrix, LWORK should be an int equal to N^2, 
and WORK should be a double array with LWORK elements.

INFO is just a status variable to tell you whether the operation completed successfully.
 Since not all matrices are invertible, 
I would recommend that you send this to some sort of error-checking system. 
INFO=0 for successful operation, INFO=-i if the i'th argument had an incorrect input value, 
and INFO > 0 if the matrix is not invertible.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值