?getri
Compute the inverse matrix based on the LU factorization result obtained using ?getrf.
Interface Definition
C interface:
void sgetri_(const int *n, float *a, const int *lda, const int *ipiv, float *work, const int *lwork, int *info);
void dgetri_(const int *n, double *a, const int *lda, const int *ipiv, double *work, const int *lwork, int *info);
void cgetri_(const int *n, float _Complex *a, const int *lda, const int *ipiv, float _Complex *work, const int *lwork, int *info);
void zgetri_(const int *n, double _Complex *a, const int *lda, const int *ipiv, double _Complex *work, const int *lwork, int *info);
Fortran interface:
SGETRI(n, a, lda, ipiv, work, lwork, info)
DGETRI(n, a, lda, ipiv, work, lwork, info)
CGETRI(n, a, lda, ipiv, work, lwork, info)
ZGETRI(n, a, lda, ipiv, work, lwork, info)
Parameters
Parameter |
Type |
Description |
Input/Output |
|---|---|---|---|
n |
Integer |
Number of rows or columns in matrix A. |
Input |
a |
|
|
Input/Output |
lda |
Integer |
Leading dimension of matrix A. lda ≥ max(1, n). |
Input |
ipiv |
Integer array |
An array containing pivot indices obtained from ?getrf. Its length is n. For 1 ≤ ipiv ≤ n, row i and row ipiv[i-1] of the matrix are interchanged during factorization. |
Input |
work |
|
Temporary storage space. After lwork=-1 is called, work[0] is the optimal lwork value. |
Output |
lwork |
Integer |
Length of the work array. If lwork = -1, the optimal work size is queried and the result is saved in work[0]. If lwork ≠ -1, the value of lwork must be greater than or equal to n. |
Input |
info |
Integer |
Execution result:
|
Output |
Dependency
#include "klapack.h"
Examples
C interface:
int n = 4;
int lda = 4;
int info = 0;
double *work = NULL;
double qwork;
int lwork = -1;
/*
* Origin A:
* 1.80 2.88 2.05 -0.89
* 5.25 -2.95 -0.95 -3.80
* 1.58 -2.69 -2.90 -1.04
* -1.11 -0.66 -0.59 0.80
* After LU factorization (via getrf, stored in column-major):
* 5.2500 -2.9500 -0.9500 -3.8000
* 0.3429 3.8914 2.3757 0.4129
* 0.3010 -0.4631 -1.5139 0.2948
* -0.2114 -0.3299 0.0047 0.1314
*/
double a[] = {5.2500, 0.3429, 0.3010, -0.2114,
-2.9500, 3.8914, -0.4631, -0.3299,
-0.9500, 2.3757, -1.5139, 0.0047,
-3.8000, 0.4129, 0.2948, 0.1314};
int ipiv[] = {2, 2, 3, 4};
/* Query optimal work size */
dgetri_(&n, a, &lda, ipiv, &qwork, &lwork, &info);
if (info != 0) {
return ERROR;
}
lwork = (int)qwork;
work = (double *)malloc(sizeof(double) * lwork);
/* Calculate inversion */
dgetri_(&n, a, &lda, ipiv, work, &lwork, &info);
free(work);
/*
* Output:
* 1.7718 0.5753 0.0844 4.8145
* -0.1174 -0.4455 0.4113 -1.7122
* 0.1798 0.4526 -0.6675 1.4820
* 2.4941 0.7644 -0.0358 7.6104
*/
Fortran interface:
PARAMETER (n = 4)
PARAMETER (lda = 4)
INTEGER :: info = 0
REAL(8) qwork(1)
REAL(8), ALLOCATABLE :: work(:)
INTEGER :: lwork = -1
* Origin A:
* 1.80 2.88 2.05 -0.89
* 5.25 -2.95 -0.95 -3.80
* 1.58 -2.69 -2.90 -1.04
* -1.11 -0.66 -0.59 0.80
* After LU factorization (via getrf, stored in column-major):
* 5.2500 -2.9500 -0.9500 -3.8000
* 0.3429 3.8914 2.3757 0.4129
* 0.3010 -0.4631 -1.5139 0.2948
* -0.2114 -0.3299 0.0047 0.1314
REAL(8) :: a(n, n)
DATA a / 5.2500, 0.3429, 0.3010, -0.2114,
$ -2.9500, 3.8914, -0.4631, -0.3299,
$ -0.9500, 2.3757, -1.5139, 0.0047,
$ -3.8000, 0.4129, 0.2948, 0.1314 /
INTEGER :: ipiv(n)
DATA ipiv / 2, 2, 3, 4 /
* Query optimal work size
EXTERNAL DGETRI
CALL DGETRI(n, a, lda, ipiv, qwork, lwork, info)
IF (info.NE.0) THEN
CALL EXIT(1)
END IF
lwork = INT(qwork(1))
ALLOCATE(work(lwork))
* Calculate inversion
CALL DGETRI(n, a, lda, ipiv, work, lwork, info)
DEALLOCATE(work)
* Output:
* 1.7718 0.5753 0.0844 4.8145
* -0.1174 -0.4455 0.4113 -1.7122
* 0.1798 0.4526 -0.6675 1.4820
* 2.4941 0.7644 -0.0358 7.6104