用 LAPACKE 求解线性方程
Solving linear equation with LAPACKE
我正在尝试求解一些线性方程(对称的、三对角的和正的)。我必须使用 LAPACKE。我的代码如下:
#include <lapacke.h>
#include <stdio.h>
void print_mtrx(double * mtrx, int n, int m)
{
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
printf("%f ", mtrx[i*m+j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
double matrix[5*5] = {
2, 0, 0, 0, 0,
0, 2, 0, 0, 0,
0, 0, 2, 0, 0,
0, 0, 0, 2, 0,
0, 0, 0, 0, 2
};
double rozw[5] = {1,2,3,4,5};
double matrix2[5*5] = {
7, 0, 0, 0, 0,
0, 7, 0, 0, 0,
0, 0, 7, 0, 0,
0, 0, 0, 7, 0,
0, 0, 0, 0, 7
};
LAPACKE_dptsv(LAPACK_COL_MAJOR, 5, 5, matrix, matrix2, rozw, 5);
print_mtrx(matrix, 5, 5);
print_mtrx(matrix2, 5, 5);
print_mtrx(rozw, 5, 1);
}
LAPACKE的函数好像什么都不做,没有任何错误。主要问题是,我不知道函数参数代表什么。我搜索了很长时间,但没有真正的文档。这是我设法找到或猜测的内容:
- int matrix_order -- LAPACK_COL_MAJOR或LAPACK_ROW_MAJOR,矩阵在内存中是如何表示的
- lapack_int n -- 矩阵的大小(即列数)
- lapack_int nrhs -- 不确定,可能是矢量 b
的大小
- double* d -- 方程矩阵
- double* e -- 不知道。
- double* b -- 来自 d
的方程解的向量
- lapack_int ldb -- b 的引导方向(为什么?它与 nrhs 不相同,而 nrhs 本身与 n 相同?)
我怎样才能找到这些论点的真正含义?我怎样才能让我的代码工作?
因此必须查看纯 LAPACK (http://www.netlib.org/lapack/explore-html/d0/dea/dptsv_8f.html#af1bd4c731915bd8755a4da8086fd79a8) 的文档,并忽略不正确的(在 LAPACKE 的情况下)关于 LDB 大于或等于 max(1,N) 的注释。
正确的程序如下:
#include <lapacke.h>
#include <stdio.h>
void print_mtrx(double * mtrx, int n, int m)
{
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
printf("%f ", mtrx[i*m+j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
double diagonal[5] = {5,1,5,1,5};
double subdiagonal[4] = {0,0,0,0};
double solution[5] = {1,2,3,4,5};
LAPACKE_dptsv(LAPACK_ROW_MAJOR, 5 /*size of matrix*/, 1 /*number of columns in solution*/,
diagonal, subdiagonal, solution, 1 /*leading dimension of solution vector*/);
print_mtrx(solution, 5, 1);
}
当谈到 BLAS and/or LAPACK 的文档时,Intel is probably the most comprehensive out there. You can look up the docs for ?ptsv 解释了每个参数的用途。
(提示:在 Google 中搜索 BLAS 或 LAPACK 时,请务必删除 s
/d
/c
/z
前缀。)
这是相关的片段:
The routine solves for X
the real or complex system of linear equations A*X = B
, where A
is an n
-by-n
symmetric/Hermitian positive-definite tridiagonal matrix, the columns of matrix B
are individual right-hand sides, and the columns of X
are the corresponding solutions.
A
is factored as A = L*D*LT
(real flavors) or A = L*D*LH
(complex flavors), and the factored form of A
is then used to solve the system of equations A*X = B
.
Input Parameters
n
: The order of matrix A
; n ≥ 0
.
nrhs
: The number of right-hand sides, the number of columns in B
; nrhs ≥ 0
.
d
: Array, dimension at least max(1, n)
. Contains the diagonal elements of the tridiagonal matrix A
.
e
, b
: Arrays: e(n - 1)
, b(ldb,*)
. The array e
contains the (n - 1)
subdiagonal elements of A
. The array b contains the matrix B
whose columns are the right-hand sides for the systems of equations. The second dimension of b
must be at least max(1,nrhs)
.
ldb
: The leading dimension of b
; ldb ≥ max(1, n)
.
Output Parameters
d
: Overwritten by the n
diagonal elements of the diagonal matrix D
from the L*D*LT
(real) / L*D*LH
(complex) factorization of A
.
e
: Overwritten by the (n - 1)
subdiagonal elements of the unit bidiagonal factor L
from the factorization of A
.
b
: Overwritten by the solution matrix X
.
info
: If info = 0
, the execution is successful. If info = -i
, the i
-th parameter had an illegal value. If info = i
, the leading minor of order i
(and therefore the matrix A
itself) is not positive-definite, and the solution has not been computed. The factorization has not been completed unless i = n
.
我正在尝试求解一些线性方程(对称的、三对角的和正的)。我必须使用 LAPACKE。我的代码如下:
#include <lapacke.h>
#include <stdio.h>
void print_mtrx(double * mtrx, int n, int m)
{
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
printf("%f ", mtrx[i*m+j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
double matrix[5*5] = {
2, 0, 0, 0, 0,
0, 2, 0, 0, 0,
0, 0, 2, 0, 0,
0, 0, 0, 2, 0,
0, 0, 0, 0, 2
};
double rozw[5] = {1,2,3,4,5};
double matrix2[5*5] = {
7, 0, 0, 0, 0,
0, 7, 0, 0, 0,
0, 0, 7, 0, 0,
0, 0, 0, 7, 0,
0, 0, 0, 0, 7
};
LAPACKE_dptsv(LAPACK_COL_MAJOR, 5, 5, matrix, matrix2, rozw, 5);
print_mtrx(matrix, 5, 5);
print_mtrx(matrix2, 5, 5);
print_mtrx(rozw, 5, 1);
}
LAPACKE的函数好像什么都不做,没有任何错误。主要问题是,我不知道函数参数代表什么。我搜索了很长时间,但没有真正的文档。这是我设法找到或猜测的内容:
- int matrix_order -- LAPACK_COL_MAJOR或LAPACK_ROW_MAJOR,矩阵在内存中是如何表示的
- lapack_int n -- 矩阵的大小(即列数)
- lapack_int nrhs -- 不确定,可能是矢量 b 的大小
- double* d -- 方程矩阵
- double* e -- 不知道。
- double* b -- 来自 d 的方程解的向量
- lapack_int ldb -- b 的引导方向(为什么?它与 nrhs 不相同,而 nrhs 本身与 n 相同?)
我怎样才能找到这些论点的真正含义?我怎样才能让我的代码工作?
因此必须查看纯 LAPACK (http://www.netlib.org/lapack/explore-html/d0/dea/dptsv_8f.html#af1bd4c731915bd8755a4da8086fd79a8) 的文档,并忽略不正确的(在 LAPACKE 的情况下)关于 LDB 大于或等于 max(1,N) 的注释。
正确的程序如下:
#include <lapacke.h>
#include <stdio.h>
void print_mtrx(double * mtrx, int n, int m)
{
int i, j;
for(i = 0; i < n; i++)
{
for(j = 0; j < m; j++)
{
printf("%f ", mtrx[i*m+j]);
}
printf("\n");
}
printf("\n");
}
int main()
{
double diagonal[5] = {5,1,5,1,5};
double subdiagonal[4] = {0,0,0,0};
double solution[5] = {1,2,3,4,5};
LAPACKE_dptsv(LAPACK_ROW_MAJOR, 5 /*size of matrix*/, 1 /*number of columns in solution*/,
diagonal, subdiagonal, solution, 1 /*leading dimension of solution vector*/);
print_mtrx(solution, 5, 1);
}
当谈到 BLAS and/or LAPACK 的文档时,Intel is probably the most comprehensive out there. You can look up the docs for ?ptsv 解释了每个参数的用途。
(提示:在 Google 中搜索 BLAS 或 LAPACK 时,请务必删除 s
/d
/c
/z
前缀。)
这是相关的片段:
The routine solves for
X
the real or complex system of linear equationsA*X = B
, whereA
is ann
-by-n
symmetric/Hermitian positive-definite tridiagonal matrix, the columns of matrixB
are individual right-hand sides, and the columns ofX
are the corresponding solutions.
A
is factored asA = L*D*LT
(real flavors) orA = L*D*LH
(complex flavors), and the factored form ofA
is then used to solve the system of equationsA*X = B
.Input Parameters
n
: The order of matrixA
;n ≥ 0
.
nrhs
: The number of right-hand sides, the number of columns inB
;nrhs ≥ 0
.
d
: Array, dimension at leastmax(1, n)
. Contains the diagonal elements of the tridiagonal matrixA
.
e
,b
: Arrays:e(n - 1)
,b(ldb,*)
. The arraye
contains the(n - 1)
subdiagonal elements ofA
. The array b contains the matrixB
whose columns are the right-hand sides for the systems of equations. The second dimension ofb
must be at leastmax(1,nrhs)
.
ldb
: The leading dimension ofb
;ldb ≥ max(1, n)
.Output Parameters
d
: Overwritten by then
diagonal elements of the diagonal matrixD
from theL*D*LT
(real) /L*D*LH
(complex) factorization ofA
.
e
: Overwritten by the(n - 1)
subdiagonal elements of the unit bidiagonal factorL
from the factorization ofA
.
b
: Overwritten by the solution matrixX
.
info
: Ifinfo = 0
, the execution is successful. Ifinfo = -i
, thei
-th parameter had an illegal value. Ifinfo = i
, the leading minor of orderi
(and therefore the matrixA
itself) is not positive-definite, and the solution has not been computed. The factorization has not been completed unlessi = n
.