1

我尝试使用函数 cblas_cgemm(); 对矩阵乘法进行矩阵运算。但是与手动计算相比,我得到的答案是不正确的。我试图简化我的代码而不在输入中使用虚构的术语,但问题仍然存在。我应该进行哪些更改才能获得正确的输出。这是我的代码。

#include<stdio.h>
#include<math.h>
#include<complex.h>
#include "cblas.h"

void main()
{
 int i,j;
 double complex A[2][2]={1,2,
                         3,4};
 double complex B[2][2]={4,5,
                         6,7};
 double complex W[2][2]={0,0,
                         0,0};

 const int m1=2;
 const int n1=2;
 const int k1=2;

 const int lda1=2;
 const int ldb1=2;
 const int ldc1=2;
 const double alpha=1.0;
 const double beta=0.0;

 cblas_cgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,m1,n1,k1,&alpha,A,lda1,B, ldb1 ,&beta,W, ldc1);

 for(i=0;i<m1;++i)
  {
  for(j=0;j<n1;++j)
    printf("%lf %lf\n" ,creal(W[i][j]),cimag(W[i][j]));
  printf("\n");
   }
 }

我的输出为

-119296.000000 0.000000
-188416.000000 0.000000 0.000000 0.000000
0.000000 0.000000
我提到了这个网站lapack:cblas_cgemm 请帮助 我使用 cblas_dgemm() 的代码如下

//Y := alpha*A*X + beta*Y, or   y := alpha*A**T*x + beta*y,
#include<stdio.h>
#include "cblas.h"
const double A[3][1]={
                      1,
                      2,
                      3
                       };
const double X[1][4]={
1,2,3,4,
};
double Y[3][4]={
0,0,0,0,
0,0,0,0,
0,0,0,0
};
int main()
{
 const int m=3;
const int k=1;const int n=4;
const int lda=1;
const int ldb=4;
const int ldc=4;
int incX,incY;
const double alpha=1.0;
const double beta=0.0;
incX=1;incY=1;
int i,j;
for(i=0;i<m;++i)
   {for(j=0;j<k;++j)
    printf("%lf \t" ,A[i][j]);
putchar('\n');
}
cblas_dgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,m,n,k,alpha,A, lda,X, ldb ,beta,Y, ldc);
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
printf("%lf\t" ,Y[i][j]);
printf("\n");
}
return 0;
}

我的输出为

hp@hp-hp-notebook:〜/beam forming/programs/studentProjectDetails $ ./dgemm_trial 1.000000
2.000000
2.000000 3.000000
1.000000 2.000000 2.000000 3.000000 4.000000
2.000000 2.000000 4.000000 4.000000 6.000000
6.000000 8.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000近报

4

2 回答 2

0

请参阅BLAS 和 Lapack 的命名约定。由于矩阵的类型是double complexcblas_zgemm()应该使用 代替cblas_cgemm()。事实上,z对于双精度复数和c单精度复数。

此外,标量alphabeta也必须是 类型double complex。请参阅 fortran 例程的源代码zgemm()以检查此类内容:COMPLEX*16对应于double complex.

于 2018-01-14T15:23:16.917 回答
0

第一个问题:您的复数应根据中所示的特定复数布局声明和使用cblas.h。您的代码表明您需要一个 2x2 矩阵,并且必须用八个总值(四个实数和四个虚数)指定一个 2x2 复数值矩阵。

*
 * A note on complex data layouts:
 *
 * In order to allow straightforward interoperation with other libraries and
 * complex types in C and C++, complex data in BLAS is passed through an opaque
 * pointer (void *).  The layout requirements on this complex data are that
 * the real and imaginary parts are stored consecutively in memory, and have
 * the alignment of the corresponding real type (float or double).  The BLAS
 * complex interfaces are compatible with the following types:
 *
 *     - The C complex types, defined in <complex.h>.
 *     - The C++ std::complex types, defined in <complex>.
 *     - The LAPACK complex types, defined in <Accelerate/vecLib/clapack.h>.
 *     - The vDSP types DSPComplex and DSPDoubleComplex, defined in <Accelerate/vecLib/vDSP.h>.
 *     - An array of size two of the corresponding real type.
 *     - A structure containing two elements, each of the corresponding real type.
 * 

第二个问题:BLAS 例程不是为使用二维数组而设计的。相反,您应该声明一个长的一维数组。这就是 LDA 参数的用途。正确传递二维数组依赖于编译器将以特定顺序布置二维数组的假设,这可能是正确的,也可能不是正确的,并导致未定义的行为。

于 2018-01-12T17:25:16.597 回答