?getri
根据?getrf得到的LU分解结果计算逆矩阵。
接口定义
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)
参数
参数名 |
类型 |
描述 |
输入/输出 |
---|---|---|---|
n |
整数型 |
矩阵A的行数或列数。 |
输入 |
a |
|
|
输入/输出 |
lda |
整数型 |
A的leading dimension大小,要求lda ≥ max(1, n)。 |
输入 |
ipiv |
整数型数组 |
?getrf得到的主元索引,长度为n:1 ≤ ipiv ≤ n,表示分解中矩阵第i行与第ipiv[i-1]行交换。 |
输入 |
work |
|
临时存储空间,使用lwork=-1调用后work[0]为最优的lwork值。 |
输出 |
lwork |
整数型 |
work数组的长度。 lwork=-1时查询最优work大小,结果保存在work[0]中,否则要求lwork≥n。 |
输入 |
info |
整数型 |
执行结果:
|
输出 |
依赖
#include "klapack.h"
示例
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