我刚刚下载了Cula,我想使用它实现的函数来解决线性方程的系统我查看了示例目录,我看到下面的代码,但是当他们想要获得A * X = B的X解时它们很混乱他们只是在X中复制B,因为A是对齐矩阵,所以答案是“B”,在这行代码中没有任何反应
status = culaSgesv(N, NRHS, A, N, IPIV, X, N);
(将X更改为B没有帮助!)
你能告诉我发生什么事吗?请告诉我如何从中获得答案“X”?如果有人需要任何进一步的信息,请告诉我。
#ifdef CULA_PREMIUM
void culaDoubleExample()
{
#ifdef NDEBUG
int N = 4096;
#else
int N = 780;
#endif
int NRHS = 1;
int i;
culaStatus status;
culaDouble* A = NULL;
culaDouble* B = NULL;
culaDouble* X = NULL;
culaInt* IPIV = NULL;
culaDouble one = 1.0;
culaDouble thresh = 1e-6;
culaDouble diff;
printf("-------------------\n");
printf(" DGESV\n");
printf("-------------------\n");
printf("Allocating Matrices\n");
A = (culaDouble*)malloc(N*N*sizeof(culaDouble));
B = (culaDouble*)malloc(N*sizeof(culaDouble));
X = (culaDouble*)malloc(N*sizeof(culaDouble));
IPIV = (culaInt*)malloc(N*sizeof(culaInt));
if(!A || !B || !IPIV)
exit(EXIT_FAILURE);
printf("Initializing CULA\n");
status = culaInitialize();
checkStatus(status);
// Set A to the identity matrix
memset(A, 0, N*N*sizeof(culaDouble));
for(i = 0; i < N; ++i)
A[i*N+i] = one;
// Set B to a random matrix (see note at top)
for(i = 0; i < N; ++i)
B[i] = (culaDouble)rand();
memcpy(X, B, N*sizeof(culaDouble));
memset(IPIV, 0, N*sizeof(culaInt));
printf("Calling culaDgesv\n");
DWORD dw1 = GetTickCount();
status = culaDgesv(N, NRHS, A, N, IPIV, X, N);
DWORD dw2 = GetTickCount();
cout<<"Time difference is "<<(dw2-dw1)<<" milliSeconds"<<endl;
if(status == culaInsufficientComputeCapability)
{
printf("No Double precision support available, skipping example\n");
free(A);
free(B);
free(IPIV);
culaShutdown();
return;
}
checkStatus(status);
printf("Verifying Result\n");
for(i = 0; i < N; ++i)
{
diff = X[i] - B[i];
if(diff < 0.0)
diff = -diff;
if(diff > thresh)
printf("Result check failed: i=%d X[i]=%f B[i]=%f", i, X[i], B[i]);
}
printf("Shutting down CULA\n\n");
culaShutdown();
free(A);
free(B);
free(IPIV);
}
答案 0 :(得分:1)
您提到了Sgesv
,但您显示的示例代码是Dgesv
。不过,答案是一样的。
根据Netlib LAPACK reference,RHS向量的B
矩阵作为第6个参数传递给函数:
[in,out] B
B is DOUBLE PRECISION array, dimension (LDB,NRHS)
On entry, the N-by-NRHS matrix of right hand side matrix B.
On exit, if INFO = 0, the N-by-NRHS solution matrix X.
X
矩阵在同一参数位置返回。因此传递给函数时B
包含NxNRHS右侧向量,同一参数返回X
结果。
在您显示的代码中,它们实际上传递了一个名为X
的变量,在返回结果后(在同一个变量X
中),它们将它与名为{{1的变量进行比较这可能令人困惑,但概念是一样的。
由于示例中的B
矩阵是单位矩阵,A
的正确解是Ax = b
对于一般情况,您应该在第6个参数位置传递x=b
RHS向量矩阵。完成该函数后,结果(B
)将在同一参数中返回。