使用LAPACK在C语言中计算矩阵的逆

24

我希望能够在C/C++中使用lapack计算一般的NxN矩阵的逆。

据我了解,使用dgetri函数进行求逆是lapack中的一种方法,但是我无法弄清楚它的所有参数应该是什么。

以下是我的代码:

void dgetri_(int* N, double* A, int* lda, int* IPIV, double* WORK, int* lwork, int* INFO);

int main(){

    double M [9] = {
        1,2,3,
        4,5,6,
        7,8,9
    };

    return 0;
}

如何使用dgetri_获得3x3矩阵M的逆矩阵?

4个回答

27

以下是使用C/C++中的LAPACK计算矩阵逆的可行代码:

#include <cstdio>

extern "C" {
    // LU decomoposition of a general matrix
    void dgetrf_(int* M, int *N, double* A, int* lda, int* IPIV, int* INFO);

    // generate inverse of a matrix given its LU decomposition
    void dgetri_(int* N, double* A, int* lda, int* IPIV, double* WORK, int* lwork, int* INFO);
}

void inverse(double* A, int N)
{
    int *IPIV = new int[N];
    int LWORK = N*N;
    double *WORK = new double[LWORK];
    int INFO;

    dgetrf_(&N,&N,A,&N,IPIV,&INFO);
    dgetri_(&N,A,&N,IPIV,WORK,&LWORK,&INFO);

    delete[] IPIV;
    delete[] WORK;
}

int main(){

    double A [2*2] = {
        1,2,
        3,4
    };

    inverse(A, 2);

    printf("%f %f\n", A[0], A[1]);
    printf("%f %f\n", A[2], A[3]);

    return 0;
}

3
您不需要为IPIV变量分配N+1个整数(只需要N个无符号整数即可)。此外,我不建议使用这种函数来计算多个逆矩阵。请一次性分配您需要的所有数据,并仅在最后释放它们。 - matovitch
你可能需要使用 delete[] IPIVdelete [] work - Reb.Cabin
4
没有 C/C++ 这种语言。你展示的代码是 C++ 的,但问题是关于 C 语言的。请注意区分。 - too honest for this site
将这个有效的C代码变得简单,只需要将new调用更改为malloc,将delete[]调用更改为frees(并且摆脱extern "C")。 - alfalfasprout

19

首先,M必须是一个二维数组,如double M[3][3]。从数学上讲,您的数组是一个1x9向量,无法求逆。

  • N是指向矩阵的秩的int类型指针-在这种情况下,N=3。

  • A是指向矩阵的LU分解的指针,您可以通过运行LAPACK例程dgetrf来获得它。

  • LDA是矩阵的“前导元素”的整数,它允许您选择一个更大的矩阵的子集,如果您只想反转一小部分。如果要反转整个矩阵,则LDA应该等于N。

  • IPIV是矩阵的枢轴索引,换句话说,它是一系列指令,告诉您要交换哪些行以反转矩阵。IPIV应该由LAPACK例程dgetrf生成。

  • LWORK和WORK是LAPACK使用的“工作空间”。如果要反转整个矩阵,则LWORK应该是一个等于N^2的int类型,而WORK应该是具有LWORK元素的double数组。

  • INFO只是一个状态变量,用于告诉您操作是否成功完成。由于并非所有矩阵都可逆,我建议您将其发送到某种错误检查系统。INFO=0表示操作成功,INFO=-i表示第i个参数具有不正确的输入值,而INFO> 0表示矩阵不可逆。

因此,对于您的代码,我会这样做:

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

    }

22
关于1x9或3x3的问题,实际上在内存布局方面并没有区别。事实上,BLAS/LAPACK例程不使用2D数组,而是使用1D数组,并对您的布局方式做出假设。但请注意,BLAS和LAPACK将假定使用FORTRAN顺序(最快更改行)而非C顺序。 - MRocklin
你可能需要使用 LAPACKE_dgetrf 而不是 Fortran 程序中的 dgetrf_。同样,对于 dgetri 也是如此。否则,你必须使用地址来调用所有内容。 - Reb.Cabin

4
以下是使用OpenBlas接口到LAPACKE的工作版本。 链接OpenBlas库(LAPACKE已包含在内)。
#include <stdio.h>
#include "cblas.h"
#include "lapacke.h"

// inplace inverse n x n matrix A.
// matrix A is Column Major (i.e. firts line, second line ... *not* C[][] order)
// returns:
//   ret = 0 on success
//   ret < 0 illegal argument value
//   ret > 0 singular matrix

lapack_int matInv(double *A, unsigned n)
{
    int ipiv[n+1];
    lapack_int ret;

    ret =  LAPACKE_dgetrf(LAPACK_COL_MAJOR,
                          n,
                          n,
                          A,
                          n,
                          ipiv);

    if (ret !=0)
        return ret;


    ret = LAPACKE_dgetri(LAPACK_COL_MAJOR,
                       n,
                       A,
                       n,
                       ipiv);
    return ret;
}

int main()
{
    double A[] = {
        0.378589,   0.971711,   0.016087,   0.037668,   0.312398,
        0.756377,   0.345708,   0.922947,   0.846671,   0.856103,
        0.732510,   0.108942,   0.476969,   0.398254,   0.507045,
        0.162608,   0.227770,   0.533074,   0.807075,   0.180335,
        0.517006,   0.315992,   0.914848,   0.460825,   0.731980
    };

    for (int i=0; i<25; i++) {
        if ((i%5) == 0) putchar('\n');
        printf("%+12.8f ",A[i]);
    }
    putchar('\n');

    matInv(A,5);

    for (int i=0; i<25; i++) {
        if ((i%5) == 0) putchar('\n');
        printf("%+12.8f ",A[i]);
    }
    putchar('\n');
}

例子:

% g++ -I [OpenBlas path]/include/ example.cpp [OpenBlas path]/lib/libopenblas.a
% a.out

+0.37858900  +0.97171100  +0.01608700  +0.03766800  +0.31239800 
+0.75637700  +0.34570800  +0.92294700  +0.84667100  +0.85610300 
+0.73251000  +0.10894200  +0.47696900  +0.39825400  +0.50704500 
+0.16260800  +0.22777000  +0.53307400  +0.80707500  +0.18033500 
+0.51700600  +0.31599200  +0.91484800  +0.46082500  +0.73198000 

+0.24335255  -2.67946180  +3.57538817  +0.83711880  +0.34704217 
+1.02790497  -1.05086895  -0.07468137  +0.71041070  +0.66708313 
-0.21087237  -4.47765165  +1.73958308  +1.73999641  +3.69324020 
-0.14100897  +2.34977565  -0.93725915  +0.47383541  -2.15554470 
-0.26329660  +6.46315378  -4.07721533  -3.37094863  -2.42580445 

1
这是Spencer Nelson上面例子的可用版本。其中一个谜团是输入矩阵按行主序排列,尽管它似乎调用了底层的Fortran例程dgetri。我认为所有底层的Fortran例程都需要按列主序排列,但我对LAPACK并不是很了解,实际上,我正在使用这个例子来帮助我学习它。但是,除了这个谜团之外:
该示例中的输入矩阵是奇异的。LAPACK试图通过在errorHandler中返回3来告诉您。我将该矩阵中的9更改为19,得到了errorHandler为0表示成功的结果,并将其与Mathematica的结果进行了比较。比较也成功了,并确认了示例中的矩阵应按行主序排列。
以下是工作代码:
#include <stdio.h>
#include <stddef.h>
#include <lapacke.h>

int main() {
    int N = 3;
    int NN = 9;
    double M[3][3] = { {1 , 2 , 3},
                       {4 , 5 , 6},
                       {7 , 8 , 9} };
    int 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_(&N, &N, M[0], &N, pivotArray, &errorHandler);
    printf ("dgetrf eh, %d, should be zero\n", errorHandler);

    dgetri_(&N, M[0], &N, pivotArray, lapackWorkspace, &NN, &errorHandler);
    printf ("dgetri eh, %d, should be zero\n", errorHandler);

    for (size_t row = 0; row < N; ++row)
    {   for (size_t col = 0; col < N; ++col)
        {   printf ("%g", M[row][col]);
            if (N-1 != col)
            {   printf (", ");   }   }
        if (N-1 != row)
        {   printf ("\n");   }   }

    return 0;   }

我在 Mac 上按以下方式构建并运行它:
gcc main.c -llapacke -llapack
./a.out

我对LAPACKE库进行了nm操作,发现以下结果:

liblapacke.a(lapacke_dgetri.o):
                 U _LAPACKE_dge_nancheck
0000000000000000 T _LAPACKE_dgetri
                 U _LAPACKE_dgetri_work
                 U _LAPACKE_xerbla
                 U _free
                 U _malloc

liblapacke.a(lapacke_dgetri_work.o):
                 U _LAPACKE_dge_trans
0000000000000000 T _LAPACKE_dgetri_work
                 U _LAPACKE_xerbla
                 U _dgetri_
                 U _free
                 U _malloc

我看到有一个LAPACKE [sic]包装器,可以方便地为Fortran提供地址,但我可能不会尝试它,因为我有一种前进的方式。

编辑

这是一个可行的版本,绕过LAPACKE [sic],直接使用LAPACK Fortran例程。 我不明白为什么行主输入会产生正确的结果,但我在Mathematica中再次确认了它。

#include <stdio.h>
#include <stddef.h>

int main() {
    int N = 3;
    int NN = 9;
    double M[3][3] = { {1 , 2 ,  3},
                       {4 , 5 ,  6},
                       {7 , 8 , 19} };
    int pivotArray[3]; //since our matrix has three rows
    int errorHandler;
    double lapackWorkspace[9];
    /* from http://www.netlib.no/netlib/lapack/double/dgetrf.f
      SUBROUTINE DGETRF( M, N, A, LDA, IPIV, INFO )
      *
      *  -- LAPACK routine (version 3.1) --
      *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
      *     November 2006
      *
      *     .. Scalar Arguments ..
      INTEGER            INFO, LDA, M, N
      *     ..
      *     .. Array Arguments ..
      INTEGER            IPIV( * )
      DOUBLE PRECISION   A( LDA, * )
    */

    extern void dgetrf_ (int * m, int * n, double * A, int * LDA, int * IPIV,
                         int * INFO);

    /* from http://www.netlib.no/netlib/lapack/double/dgetri.f
       SUBROUTINE DGETRI( N, A, LDA, IPIV, WORK, LWORK, INFO )
       *
       *  -- LAPACK routine (version 3.1) --
       *     Univ. of Tennessee, Univ. of California Berkeley and NAG Ltd..
       *     November 2006
       *
       *     .. Scalar Arguments ..
       INTEGER            INFO, LDA, LWORK, N
       *     ..
       *     .. Array Arguments ..
       INTEGER            IPIV( * )
       DOUBLE PRECISION   A( LDA, * ), WORK( * )
    */

    extern void dgetri_ (int * n, double * A, int * LDA, int * IPIV,
                         double * WORK, int * LWORK, int * INFO);

    // 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_(&N, &N, M[0], &N, pivotArray, &errorHandler);
    printf ("dgetrf eh, %d, should be zero\n", errorHandler);

    dgetri_(&N, M[0], &N, pivotArray, lapackWorkspace, &NN, &errorHandler);
    printf ("dgetri eh, %d, should be zero\n", errorHandler);

    for (size_t row = 0; row < N; ++row)
    {   for (size_t col = 0; col < N; ++col)
        {   printf ("%g", M[row][col]);
            if (N-1 != col)
            {   printf (", ");   }   }
        if (N-1 != row)
        {   printf ("\n");   }   }
    return 0;   }

像这样构建和运行:

$ gcc foo.c -llapack
$ ./a.out
dgetrf eh, 0, should be zero
dgetri eh, 0, should be zero
-1.56667, 0.466667, 0.1
1.13333, 0.0666667, -0.2
0.1, -0.2, 0.1

编辑

这个谜团似乎不再是谜团了。我认为计算是按列主序进行的,因为必须这样做,但我输入和输出矩阵时却像它们是按行主序排列的。我有两个错误互相抵消,所以事情看起来像是按行排列,即使它们实际上是按列排列的。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接