[英]where is cula “culaSgesv” answer for X?
I just downloaded Cula and I want to use it's implemented functions for solving system of linear equation I looked into Examples Directory and I saw below code but it's very confusing when they want to obtain X solution of A*X=B they just copy B in X and since A is identity diagonal matrix so the answer IS, "B" and in this line of code nothing happens 我刚刚下载了Cula,我想使用它的已实现函数来求解线性方程组,我查看了示例目录,然后看到了下面的代码,但是当他们想要获得A * X = B的X解时,他们只是将B复制到其中,这非常令人困惑X,由于A是单位对角矩阵,所以答案为IS,“ B”,在这一行代码中没有任何反应
status = culaSgesv(N, NRHS, A, N, IPIV, X, N);
(changing X to B didn't help!) (将X更改为B并没有帮助!)
would you please tell me whats going on? 你能告诉我发生了什么吗? Please tell me how can I get the answer "X" from this? 请告诉我如何从中得到答案“ X”?
if anyone need any further information please just tell me. 如果有人需要任何进一步的信息,请告诉我。
#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);
}
You mention Sgesv
but the sample code you have shown is for Dgesv
. 您提到了Sgesv
但是显示的示例代码是针对Dgesv
。 Nevertheless, the answer is the same. 然而,答案是相同的。
According to the Netlib LAPACK reference , the B
matrix of RHS vectors is passed to the function as the 6th parameter: 根据Netlib LAPACK参考 ,将RHS向量的B
矩阵作为第六个参数传递给函数:
[in,out] B [进,出] 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.
And the X
matrix is returned in the same parameter location. X
矩阵在相同的参数位置返回。 So B
when passed to the function contains the NxNRHS right-hand-side vectors, and the same parameter returns the X
result. 因此,当B
传递给函数时,它包含NxNRHS右侧向量,并且相同的参数返回X
结果。
In the code you have shown, they are actually passing a variable called X
and after the result is returned (in the same variable X
) they are comparing it against a variable named B
which is perhaps confusing, but the concept is the same. 在您显示的代码中,他们实际上传递了一个名为X
的变量,并在返回结果后(在同一变量X
)将它们与一个名为B
的变量进行比较,这可能会令人困惑,但是概念是相同的。
Since the A
matrix in the example is the identity matrix, the correct solution of Ax = b
is x=b
由于示例中的A
矩阵是单位矩阵,因此Ax = b
的正确解是x=b
For the general case, you should pass your B
matrix of RHS vectors in the 6th parameter location. 对于一般情况,应在第6个参数位置传递RHS向量的B
矩阵。 Upon completion of the function, the result ( X
) will be returned in the same parameter. 函数完成后,结果( X
)将以相同的参数返回。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.