- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我一直在为物理学研究项目编写程序。该程序是用 C 语言编写的,但使用了一个 fortran 函数(称为“zgesv”,来自 LAPACK 和 BLAS 库)。
这个想法是求解一个方程组。 LHS.X = vector “X”的 RHS。 int INFO 被传递给 zgesv。如果方程无法求解(即 LHS 是单数),info 应该返回一个值 != 0;
尝试通过将我的双 * 传递给求解函数(解决方案 1,在以下代码中)以“正常”方式运行我的程序,INFO 返回为 0 - 即使 LHS 是单数。不仅如此,如果我打印出解决方案,那将是数字的灾难——有些小,有些为零,有些很大。
如果我通过以下方式手动创建 LHS 和 RHS LHS[] = {值 1, 值 2, ...}; RHS[] = {值 1, 值 2, ...};然后 INFO 按预期返回 3,解等于 RHS(这也是我所期望的。)
如果我声明数组 LHS2[] = {值 1, 值 2, ...}; RHS2[] = {值 1, 值 2, ...};并将它们逐个元素复制到 LHS 和 RHS 中,然后 INFO 返回为 8(这对我来说很奇怪,它与之前的情况不同.),解等于RHS。
我觉得这一定是两种声明数组的方式之间的根本区别。我真的无法访问函数 zgesv 以使其采用我想要的类型,因为 A) 它是科学界的标准,并且 B) 它是用 fortran 编写的——我从未学过。
谁能解释一下这是怎么回事?是否有一个简单的(最好是计算成本低的)修复方法?我应该只将我的双 * 复制到数组 [] 中吗?
这是我的程序(为测试而修改):
#include <stdlib.h>
#include <math.h>
#define PI 3.1415926535897932384626433832795029L
#define ERROR_VALUE 911.911
int* getA(int N, char* argv[])
{
int i;
int* AMatrix;
AMatrix = malloc(N * N * sizeof(int));
if (AMatrix == NULL)
{
printf("Failed to allocate memory for AMatrix. Exiting.");
exit (EXIT_FAILURE);
}
for (i = 0; i < N * N; i++)
{
AMatrix[i] = atoi(argv[i + 1]);
}
return AMatrix;
}
double* generateLHS(int N, int* AMatrix, int TAPs[], long double kal)
{
double S, C;
S = sinl(kal);
C = cosl(kal);
printf("According to C, Sin(Pi/2) = %.25lf and Cos(Pi/2) = %.25lf", S, C);
// S = 1;
// C = 0;
double* LHS;
LHS = malloc(N * N * 2 * sizeof(double));
if (LHS == NULL)
{
printf("Failed to allocate memory for LHS. Exiting.");
exit (EXIT_FAILURE);
}
int i;
for (i = 0; i < N * N; i++)
{
LHS[2 * i] = -1 * AMatrix[i];
LHS[(2 * i) + 1] = 0;
}
for (i = 0; i <= 2 * N * N - 2; i = i + (2 * N) + 2)
{
LHS[i] = LHS[i] + (2 * C);
}
int j;
for (i = 0; i <= 3; i++)
{
j = 2 * N * TAPs[i] + 2 * TAPs[i];
LHS[j] = LHS[j] - C;
LHS[j + 1] = LHS[j + 1] - S;
}
return LHS;
}
double* generateRHS(int N, int inputTailAttachmentPoint, long double kal)
{
double* RHS;
RHS = malloc(2 * N * sizeof(double));
int i;
for (i = 0; i < 2 * N; i++)
{
RHS[i] = 0.0;
}
RHS[2 * inputTailAttachmentPoint + 1] = - 2 * sin(kal);
return RHS;
}
double* solveUsingLUD(int N, double* LHS, double* RHS)
{
int INFO; /*Info is changed by ZGELSD to 0 if the computation was carried out successfully. Else it changes to some none-zero integer. */
int ione = 1;
int LDA = N;
int LDB = N;
int n = N;
int* IPV = malloc(N * sizeof(int));
if (IPV == NULL)
{
printf("Failed to allocate memory for IPV. Exiting.");
exit (EXIT_FAILURE);
}
zgesv_(&n, &ione, LHS, &LDA, IPV, RHS, &LDB, &INFO);
free(IPV);
if (INFO != 0)
{
printf("\n ERROR: info = %d\n", INFO);
}
return RHS;
}
void printComplexVectors(int numberOfRows, double* matrix)
{
int i;
for (i = 0; i < 2 * numberOfRows - 1; i = i + 2)
{
printf("%f + %f*i \n", matrix[i], matrix[i + 1]);
}
printf("\n");
}
int main(int argc, char* argv[])
{
int N = 8;
int* AMatrix;
AMatrix = getA(N, argv);
int TAPs[]={4,4,4,3};
long double kal = PI/2;
double *LHS, *RHS;
LHS = generateLHS(N, AMatrix, TAPs,kal);
int i;
RHS = generateRHS(N, TAPs[0],kal);
printf("\n LHS = \n{{");
for (i = 0; i < 2 * N * N - 1;)
{
printf("%lf + ", LHS[i]);
i = i + 1;
printf("%lfI", LHS[i]);
i = i + 1;
if ((int)(.5 * i) % N == 0)
{
printf("},\n{");
}
else
{
printf(",");
}
}
printf("}");
double LHS2[] = {0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,-1.000000,0.000000,0.000000,0.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,-0.000000,-3.000000,0.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,-1.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.00000};
double RHS2[] ={0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000,-2.000000,0.000000,0.000000,0.000000,0.000000,0.000000,0.000000};
printf("comparing LHS and LHS2\n");
for (i = 0; i < 2 * N * N;)
{
if (LHS[i] != LHS2[i]) {
printf( "LHS difference at index %d\n", i);
printf("LHS[%d] = %.16lf\n", i, LHS[i]);
printf("LHS2[%d] = %.16lf\n", i, LHS2[i]);
printf("The difference is %.16lf\n", LHS[i] - LHS2[i]);
}
i = i + 1;
}
printf("\n");
printf("comparing RHS and RHS2\n");
for (i = 0; i < 2 * N;)
{
if (RHS[i] != RHS2[i]) {
printf( "RHS difference at index %d\n", i);
printf("RHS[%d] = %.16lf\n", i, RHS[i]);
printf("RHS2[%d] = %.16lf\n", i, RHS2[i]);
printf("The difference is %.16lf", RHS[i] - RHS2[i]);
}
i = i + 1;
}
printf("\n");
double *solution;
solution = solveUsingLUD(N,LHS,RHS);
printf("\n Solution = \n{");
for (i = 0; i < 2 * N - 1;)
{
printf("{%.16lf + ", solution[i]);
i = i + 1;
printf("%.16lfI},", solution[i]);
i = i + 1;
printf("\n");
}
solution = solveUsingLUD(N,LHS2,RHS2);
printf("Solution2 = \n{");
for (i = 0; i < 2 * N - 1;)
{
printf("{%lf + ", solution[i]);
i = i + 1;
printf("%lfI},", solution[i]);
i = i + 1;
printf("\n");
}
for (i = 0; i < 2 * N * N;)
{
LHS[i] = LHS2[i];
i = i + 1;
}
for (i = 0; i < 2 * N;)
{
RHS[i] = RHS2[i];
i = i + 1;
}
solution = solveUsingLUD(N,LHS,RHS);
printf("Solution3 = \n{");
for (i = 0; i < 2 * N - 1;)
{
printf("{%lf + ", solution[i]);
i = i + 1;
printf("%lfI},", solution[i]);
i = i + 1;
printf("\n");
}
return 0;
}
我用的是编译行
gcc -lm -llapack -lblas PiecesOfCprogarm.c -Wall -g
然后我执行:
./a.out 0 0 0 0 1 1 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0
给出输出
According to C, Sin(Pi/2) = 1.0000000000000000000000000 and Cos(Pi/2) = -0.0000000000000000000271051
LHS =
{{-0.000000 + 0.000000I,0.000000 + 0.000000I,0.000000 + 0.000000I,0.000000 + 0.000000I,-1.000000 + 0.000000I,-1.000000 + 0.000000I,0.000000 + 0.000000I,0.000000 + 0.000000I},
{0.000000 + 0.000000I,-0.000000 + 0.000000I,0.000000 + 0.000000I,0.000000 + 0.000000I,0.000000 + 0.000000I,-1.000000 + 0.000000I,-1.000000 + 0.000000I,-1.000000 + 0.000000I},
{0.000000 + 0.000000I,0.000000 + 0.000000I,-0.000000 + 0.000000I,0.000000 + 0.000000I,0.000000 + 0.000000I,-1.000000 + 0.000000I,-1.000000 + 0.000000I,-1.000000 + 0.000000I},
{0.000000 + 0.000000I,0.000000 + 0.000000I,0.000000 + 0.000000I,-0.000000 + -1.000000I,0.000000 + 0.000000I,0.000000 + 0.000000I,-1.000000 + 0.000000I,-1.000000 + 0.000000I},
{-1.000000 + 0.000000I,0.000000 + 0.000000I,0.000000 + 0.000000I,0.000000 + 0.000000I,0.000000 + -3.000000I,0.000000 + 0.000000I,-1.000000 + 0.000000I,-1.000000 + 0.000000I},
{-1.000000 + 0.000000I,-1.000000 + 0.000000I,-1.000000 + 0.000000I,0.000000 + 0.000000I,0.000000 + 0.000000I,-0.000000 + 0.000000I,0.000000 + 0.000000I,0.000000 + 0.000000I},
{0.000000 + 0.000000I,-1.000000 + 0.000000I,-1.000000 + 0.000000I,-1.000000 + 0.000000I,-1.000000 + 0.000000I,0.000000 + 0.000000I,-0.000000 + 0.000000I,0.000000 + 0.000000I},
{0.000000 + 0.000000I,-1.000000 + 0.000000I,-1.000000 + 0.000000I,-1.000000 + 0.000000I,-1.000000 + 0.000000I,0.000000 + 0.000000I,0.000000 + 0.000000I,-0.000000 + 0.000000I},
{}comparing LHS and LHS2
LHS difference at index 0
LHS[0] = -0.0000000000000000
LHS2[0] = 0.0000000000000000
The difference is -0.0000000000000000
LHS difference at index 18
LHS[18] = -0.0000000000000000
LHS2[18] = 0.0000000000000000
The difference is -0.0000000000000000
LHS difference at index 36
LHS[36] = -0.0000000000000000
LHS2[36] = 0.0000000000000000
The difference is -0.0000000000000000
LHS difference at index 54
LHS[54] = -0.0000000000000000
LHS2[54] = 0.0000000000000000
The difference is -0.0000000000000000
LHS difference at index 72
LHS[72] = 0.0000000000000000
LHS2[72] = -0.0000000000000000
The difference is 0.0000000000000000
LHS difference at index 90
LHS[90] = -0.0000000000000000
LHS2[90] = 0.0000000000000000
The difference is -0.0000000000000000
LHS difference at index 108
LHS[108] = -0.0000000000000000
LHS2[108] = 0.0000000000000000
The difference is -0.0000000000000000
LHS difference at index 126
LHS[126] = -0.0000000000000000
LHS2[126] = 0.0000000000000000
The difference is -0.0000000000000000
comparing RHS and RHS2
Solution =
{{1.0000000000000000 + -0.0000000000000000I},
{-1.0000000000000000 + -0.0000000000000000I},
{-0.0000000000000000 + 0.0000000000000000I},
{-0.0000000000000000 + 0.0000000000000000I},
{0.6000000000000000 + 0.2000000000000000I},
{-0.0000000000000000 + -0.0000000000000000I},
{-6854258945071195.0000000000000000 + 4042255275298396.0000000000000000I},
{6854258945071195.0000000000000000 + -4042255275298396.0000000000000000I},
ERROR: info = 3
Solution2 =
{{0.000000 + 0.000000I},
{0.000000 + 0.000000I},
{0.000000 + 0.000000I},
{0.000000 + 0.000000I},
{0.000000 + -2.000000I},
{0.000000 + 0.000000I},
{0.000000 + 0.000000I},
{0.000000 + 0.000000I},
ERROR: info = 8
Solution3 =
{{0.000000 + 0.000000I},
{0.000000 + 0.000000I},
{0.000000 + 0.000000I},
{0.000000 + 0.000000I},
{0.000000 + -2.000000I},
{0.000000 + 0.000000I},
{0.000000 + 0.000000I},
{0.000000 + 0.000000I},
最佳答案
您的代码中存在一些语法错误(不匹配的括号和引号),但我想这些是您在此处复制代码时出现的。
我的猜测是以下可能导致问题:
solveSystemByLUD(int N, double *LHS, double* RHS, ...)
{
...
}
这声明了一个返回 int
的函数,但您返回的是 double *
。添加返回类型并查看是否有任何变化。
编辑:
在您澄清之后仍然存在一些缺陷 - 由于这一行代码仍然无法编译:
* SUBROUTINE ZGESV( N, Nrhs, A, LDA, IPIV, B, LDB, INFO ) Solves A * X = B and stores the answer in B. If the solver worked, INFO is set to 0.
*/
缺少开场白。
您还将 sin() 和 cos() 的结果分配给整数变量(默认情况下 C 和 S 是整数):
S = sin(kal);
C = cos(kal);
从您的代码来看,这不是您想要的。
最后要注意的是,在 LHS2
中缺少最后一个元素(sizeof(LHS2)/sizeof(double)
是 127 而不是 2 * 8 * 8
= 128).这意味着您正在读取和写入超出数组末尾的位置,这会导致未定义的行为并可能导致您看到的问题。
编辑 2:
还有一件事:您正在读取函数 getA() 中的 argv[0]
,它始终是可执行文件的路径。您应该从 argv[1]
开始阅读。为了安全起见,您应该检查用户是否提供了足够的参数 (argc - 1 >= N * N
)。
关于C - 指向 double 和 double 组的指针之间的细微差别。将一个转换为另一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6394859/
我是一名优秀的程序员,十分优秀!