当我试图使用cblas库中的cblas_chpr()函数来计算浮点复向量的相关矩阵时,我遇到了问题。
从Lapack v3.10.0库下载netLib.org后,我编译了它,并将libcblas.a、liblapack.a、liblapacke.a、librefblas.a和libtmglib.aE 210文件复制到我的项目中,并确保库正确链接。
根据描述,cblas_chpr函数计算alpha *x* conjg(x') + A,并将结果存储在A中。
该职能的定义是:
void cblas_chpr(CBLAS_LAYOUT layout, CBLAS_UPLO Uplo,
const CBLAS_INDEX N, const float alpha, const void *X,
const CBLAS_INDEX incX, void *A);其中参数为:
我的职能如下:
/* Number of elements */
int Ne = 10;
/* Set the parameters */
CBLAS_LAYOUT layout = CblasColMajor; /* Layout is column major */
CBLAS_UPLO Uplo = CblasUpper; /* Upper triangle of the matrix */
CBLAS_INDEX N = Ne; /* Number of elements in vector X */
float alpha = 1.0; /* No scaling, alpha = 1.0 */
/* The vector X */
float complex * X = malloc(Ne * sizeof(* X));
/* Set values of X - for illustration purpose only */
for(int i = 0; i < Ne; i++)
{
X[i] = CMPLXF(i, i + 1.0);
}
CBLAS_INDEX incX = 1; /* Use data from every element */
/* The correlation matrix is a Ne x Ne matrix */
float complex ** A = malloc(Ne * sizeof(*A));
for(int i = 0; i < Ne; i++)
{
A[i] = malloc(Ne * sizeof(*A[i]));
}
cblas_chpr(layout, Uplo, N, alpha, X, incX, A);
float complex print_val = A[0][0];
printf("%+.10f %+.10f", crealf(print_val), cimagf(print_val));但是,该程序与No源代码崩溃,因为"chpr_()在0x5555555555e70b“的错误。
我猜我的输入参数不正确。CBLAS是Fortran库的包装器。
以前是否有人遇到过此错误,并知道如何解决?
发布于 2022-02-09 19:15:04
回答我自己的问题以防别人遇到同样的问题。A应该是大小为N* (N + 1) / 2的一维数组。而且,数组A中的每个元素的值必须初始化为零。否则,结果将是错误的。请阅读cblas_chpr()函数的说明,了解为什么会出现这种情况。
/* Number of elements */
int Ne = 10;
/* Set the parameters */
CBLAS_LAYOUT layout = CblasColMajor; /* Layout is column major */
CBLAS_UPLO Uplo = CblasUpper; /* Upper triangle of the matrix */
CBLAS_INDEX N = Ne; /* Number of elements in vector X */
float alpha = 1.0; /* No scaling, alpha = 1.0 */
/* The vector X */
float complex * X = malloc(Ne * sizeof(* X));
/* Set values of X - for illustration purpose only */
for(int i = 0; i < Ne; i++)
{
X[i] = CMPLXF(i, i + 1.0);
}
CBLAS_INDEX incX = 1; /* Use data from every element */
/* Initialize the array that store correlation matrix */
int size_A = Ne * (Ne + 1) / 2;
float complex * A = malloc(size_A * sizeof(*A));
for(int i = 0; i < size_A; i++)
{
A[i] = 0.0;
}
cblas_chpr(layout, Uplo, N, alpha, X, incX, A);
/* Print the first value of the result */
float complex print_val = A[0][0];
printf("%+.10f %+.10f", crealf(print_val), cimagf(print_val));
free(X);
free(A);https://stackoverflow.com/questions/70428353
复制相似问题