?gesv
求解线性方程组 ,其中
,其中 是
是 的矩阵,
的矩阵, 和
和 为
为 的矩阵。求解过程中,对
的矩阵。求解过程中,对 使用具有部分旋转和行交换的LU分解,将其分解为
使用具有部分旋转和行交换的LU分解,将其分解为 的形式,其中
的形式,其中 是置换矩阵,
是置换矩阵, 是单位下三角矩阵,
是单位下三角矩阵, 是上三角矩阵。并通过LU分解的结果来求解线性方程组。
是上三角矩阵。并通过LU分解的结果来求解线性方程组。
接口定义
C Interface:
void dsgesv_(const int *n, const int *nrhs, double *a, const int *lda, int *ipiv, const double *b, const int *ldb, double *x, const int *ldx, double *work, float *swork, int *iter, int *info);
void zcgesv_( const int *n, const int *nrhs, double _Complex *a, const int *lda, int *ipiv, const double _Complex *b, const int *ldb, double _Complex *x, const int *ldx, double _Complex *work, float _Complex *swork, double *rwork, int *iter, int *info);
Fortran Interface:
DSGESV(N, NRHS, A, LDA, IPIV, B, LDB, X, LDX, WORK, SWORK, ITER, info);
ZCGESV(N, NRHS, A, LDA, IPIV, B, LDB, X, LDX, WORK, SWORK, RWORK, ITER, info);
参数
| 参数名 | 类型 | 描述 | 输入/输出 | 
|---|---|---|---|
| n | 整数型 | 矩阵A的阶数,要求n≥0。 | 输入 | 
| nrhs | 整数型 | 右端项的数量,即矩阵B的列数,要求nrhs≥0。 | 输入 | 
| a | 
 | 矩阵大小为(lda,n)。 
 | 输入,输出 | 
| lda | 整数型 | A的leading dimension大小,要求lda≥max(1, n)。 | 输入 | 
| ipiv | 整数型数组 | 数组维度为n。 存放了置换矩阵P的主元索引,即矩阵第i行与第ipiv(i)行交换。 | 输出 | 
| b | 
 | 矩阵维度为(ldb,nrhs)。 
 | 输入 | 
| ldb | 整数型 | B的主维大小,要求ldb≥max(1, n)。 | 输入 | 
| x | 
 | 大小为ldx*nrhs。 若info=0时,输出为n*nrhs的解矩阵。 | 输出 | 
| ldx | 整数型 | 矩阵x的主维,ldx≥max(1, n)。 | 输入 | 
| work | 
 | 大小为n*nrhs。 用于保存残差向量。 | 输出 | 
| swork | 
 | 大小为n*(n+nrhs)。 用于存储单精度矩阵和单精度中的解。 | 输出 | 
| rwork(复数类型特有) | 双精度浮点型数组 | 工作数组,大小为n。 | 输出 | 
| iter | 整数型 | 
 | 输出 | 
| info | 整数型 | 执行结果: 
 | 输出 | 
依赖
#include "klapack.h"
示例
C Interface:
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | const int n = 4; const int nrhs = 2; const int lda = 4; const int ldb = 4; const int ldx = 4; int info = 0; int iter = 0; double a[] = {72.1673, 66.1857, 64.7644, 28.0199, 91.4151, 6.5180, 62.8483, 72.4323, 46.5760, 8.6928, 28.9821, 42.1828, 18.6437, 99.8612, 35.6972, 67.9812, 5.0880, 85.5035, 79.2945, 54.5920, 28.6869, 49.7512, 7.5186, 28.6929, 84.6041}; int *ipiv = (int*)malloc(n * sizeof(int)); double b[] = {9.4532, 1.5204, 2.2127, 0.9891, 7.1778, 6.8955, 7.2465, 3.5019, 8.2268, 3.5287}; double *x = (double*)malloc(ldx * nrhs * sizeof(double)); double *work = (double*)malloc(n * nrhs * sizeof(double)); float *swork = (float*)malloc(n * (n + nrhs) * sizeof(float)); dsgesv_(&n, &nrhs, a, &lda, ipiv, b, &ldb, x, &ldx, work, swork, &iter, &info); if (info != 0) { printf("ERROR, info = %d\n", info); } if (iter < 0) { printf("Iterative refinement failed, iter = %d\n", iter); } /* output */ * x * 0.153366 0.083995 * -0.538653 0.096517 * 1.080726 -0.174382 * -0.145340 0.022261 * iter * 2 | 
Fortran Interface:
PARAMETER (n = 4) 
PARAMETER (nrhs = 2) 
PARAMETER (lda = 4)
PARAMETER (ldb = 4)
PARAMETER (ldx = 4)
REAL(8) :: a(lda, n)  
REAL(8) :: b(ldb, n)
REAL(8) :: b(ldx, n)
REAL(8) :: work(nrhs, n)
REAL(4) :: swork(n+nrhs, n)
INTERGER::ipiv(n)
INTEGER :: info = 0
INTEGER :: iter = 0
  
DATA a / 72.1673, 66.1857, 64.7644, 28.0199, 91.4151, 
&         6.5180,  62.8483, 72.4323, 46.5760, 8.6928, 
&        28.9821, 42.1828, 18.6437, 99.8612, 35.6972, 
&        67.9812, 5.0880,  85.5035, 79.2945, 54.5920,
&        28.6869, 49.7512, 7.5186,  28.6929, 84.6041 /
 EXTERNAL DSGESV
 CALL DSGESV(n, nrhs, a, lda, ipiv, b, ldb, x, ldx, work, swork, iter, info);
IF (info.NE.0) THEN 
    CALL EXIT(1) 
END IF
IF (iter.LT.0) THEN 
    CALL EXIT(1) 
END IF
* 
* Output: 
* 0.153366        0.083995
* -0.538653       0.096517
* 1.080726        -0.174382
* -0.145340       0.022261
* iter
* 2
   





