首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用cblas_chpr函数

使用cblas_chpr函数
EN

Stack Overflow用户
提问于 2021-12-20 21:39:11
回答 1查看 28关注 0票数 0

当我试图使用cblas库中的cblas_chpr()函数来计算浮点复向量的相关矩阵时,我遇到了问题。

Lapack v3.10.0库下载netLib.org后,我编译了它,并将libcblas.aliblapack.aliblapacke.alibrefblas.alibtmglib.aE 210文件复制到我的项目中,并确保库正确链接。

根据描述,cblas_chpr函数计算alpha *x* conjg(x') + A,并将结果存储在A中。

该职能的定义是:

代码语言:javascript
复制
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);

其中参数为:

  • 布局-这是一个emun和两个可能的输入是CblasRowMajor和CblasColMajor。
  • Uplo -这是一个枚举,两个可能的输入是CblasUpper和CblasLower。
  • 矩阵A的阶数和向量x中的元素数。
  • α-向量X被乘以的比例因子。
  • X -向量X。
  • incX -在X中跨出,例如,如果incX为7,则使用每7个元素。
  • A-矩阵A.返回时被结果覆盖.

我的职能如下:

代码语言:javascript
复制
   /* 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库的包装器。

以前是否有人遇到过此错误,并知道如何解决?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-02-09 19:15:04

回答我自己的问题以防别人遇到同样的问题。A应该是大小为N* (N + 1) / 2的一维数组。而且,数组A中的每个元素的值必须初始化为零。否则,结果将是错误的。请阅读cblas_chpr()函数的说明,了解为什么会出现这种情况。

代码语言:javascript
复制
/* 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);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70428353

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档