所以我有两个矩阵;一个是2乘2,另一个是2乘1.我想使用外部库lapack来解决线性系统,似乎我需要调用函数dgesv_(),这是
Ax = B
我必须解决x。
所以我真的很困惑他们说的这个函数以及如何将它转换成我现在拥有的两个数组。
#include <stdlib.h>
#include <stdio.h>
static double Angstroms[2];
static double Energy[2];
static double ax[2][2];
void file_input ();
void polynomial ();
int main () {
file_input ();
polynomial ();
return 0;
}
void file_input () {
float a, b;
int i;
FILE * in_file = fopen("H2Mini.txt", "r");
if (outfile == NULL) {
printf ("Error file does not exist");
exit (-1);
}
for (i = 0; i <= 1; i++) {
fscanf(in_file, "%f %f\n", &a, &b);
Angstroms[i] = a;
Energy [i] = b;
}
fclose(in_file);
}
void polynomial () {
int i;
FILE * outfile = fopen("PolyTest2.txt", "w");
if (outfile == NULL) {
printf ("Error file does not exist");
exit (-1);
}
for (i = 0; i <= 1; i++) {
ax[i][0] = 1;
fprintf (outfile, "%.8f ", ax[i][0]);
}
fprintf (outfile, "\n");
for (i = 0; i <= 1; i ++) {
ax[i][1] = Angstroms[i];
fprintf (outfile, "%.8f ", ax[i][1]);
}
}
所以我的文件就是这个
[2.00000000 3.00000000
6.00000000 5.00000000]
ax数组看起来像这样
[1.00000000 1.00000000
2.00000000 6.00000000]
能量阵列就是这个
[3.00000000 5.00000000]
我的ax数组是Ax = b等式中的Ax项,我的b项是能量数组。
我确实查看了他们的功能文档,实现它有点令人困惑。
在dgesv (n, nrhs, a, lda, ipiv, b, ldb, info)
这个代码的任何非常明确的例子都会非常有用!
答案 0 :(得分:0)
我不确定你的意思是'斧头阵列看起来像这样'?不应该Ax
应该是一个向量吗?
但是通常使用C包装器LAPACKE调用dgesv
应该如下所示:
LAPACKE_dgesv(
LAPACK_ROW_MAJOR, // The storage format you use, usually row major in c.
n, // Dimensions of A (nxn)
1, // Number of b's, here always one because you want to solve for a single right hand side.
A, // The input matrix A, it will be destroyed in the call so best make a copy.
n, // LDA basically the length of each column of A in memory. Normally this is also n if A is nxn.
p, // An additional array where lapacke saves pivot ordering. Probably not of any interest to you. Just give it an array of size n.
bToX, // On input input the rhs b, on output this will be the solution vector x
1 );