?gesv
Solve a system of linear equations
, where
is an
matrix, and
and
are
matrices. The routine computes the LU factorization of
with partial pivoting and row interchanges, factoring
as
, where
is a permutation matrix,
is a unit lower triangular matrix, and
is an upper triangular matrix. It then uses the LU factorization results to solve the system of linear equations.
Interface Definition
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);
Parameters
|
Parameter |
Type |
Description |
Input/Output |
|---|---|---|---|
|
n |
Integer |
Order of matrix A. n ≥ 0. |
Input |
|
nrhs |
Integer |
Number of items on the right, that is, the number of columns in matrix B. nrhs ≥ 0. |
Input |
|
a |
|
Matrix with a size of (lda,n).
|
Input/Output |
|
lda |
Integer |
Leading dimension of matrix A. lda ≥ max(1, n). |
Input |
|
ipiv |
Integer array |
The array dimension is n. Array storing the pivot indices of the permutation matrix P. The i-th row of the matrix is interchanged with the ipiv(i)-th row. |
Output |
|
b |
|
The matrix dimension is (ldb, nrhs).
|
Input |
|
ldb |
Integer |
Leading dimension of matrix B. ldb ≥ max(1, n). |
Input |
|
x |
|
Array with a size of ldx*nrhs. If info=0, it contains the n*nrhs solution matrix. |
Output |
|
ldx |
Integer |
Leading dimension of matrix X. ldx ≥ max(1, n). |
Input |
|
work |
|
Array with a size of n*nrhs. It is used to store residual vectors. |
Output |
|
swork |
|
Array with a size of n*(n+nrhs). Stores the single-precision matrix and single-precision solution results. |
Output |
|
rwork (only for complex types) |
Double-precision floating-point array |
Work array, with a size of n. |
Output |
|
iter |
Integer |
|
Output |
|
info |
Integer |
Execution results:
|
Output |
Dependency
#include "klapack.h"
Examples
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






