Lapack cblas_cgemm计算两个复矩阵乘法

Lapack cblas_cgemm计算两个复矩阵乘法,lapack,Lapack,我想调用cblas_cgemm来计算两个复数矩阵的乘法。当我遇到大尺寸的问题时,现在我试图将矩阵A和矩阵B设置为超级简单,比如说,仅一个元素。这是我的c代码,输出非常奇怪。有人能帮忙吗?非常感谢 A=1+2i B=3+4i 结果应该是-5+10i 输出如下所示: 矩阵是: (1.00,2.00) B矩阵为: (3.00、4.00) A和B的乘积为: (-18.75,-9.38) 这是我的代码截图 我认为这个想法就是内存分配, 如果在下面的代码中使用的是malloc,即result=(micomp

我想调用cblas_cgemm来计算两个复数矩阵的乘法。当我遇到大尺寸的问题时,现在我试图将矩阵A和矩阵B设置为超级简单,比如说,仅一个元素。这是我的c代码,输出非常奇怪。有人能帮忙吗?非常感谢

A=1+2i B=3+4i

结果应该是-5+10i

输出如下所示:

矩阵是:

(1.00,2.00)

B矩阵为:

(3.00、4.00)

A和B的乘积为:

(-18.75,-9.38)

这是我的代码截图


我认为这个想法就是内存分配, 如果在下面的代码中使用的是
malloc
,即
result=(micomplex*)malloc((1*1)*sizeof(micomplex))
然后您有时会得到荒谬的输出,如(36893488147419103232.00,10.00)或不正确的(-3.00,10.00), 但是使用
calloc

result=(micomplex*)calloc(1*1,sizeof(micomplex))
执行代码时,每次(-5.00,10.00)都给出正确的输出

要将代码与
double
一起使用,请进行以下更改:
float
to
double
cblas\u cgemm
to
cblas\u zgemm

#include <stdio.h>
#include <stdlib.h>
#include <cblas.h>

/* Complex datatype */
struct _micomplex { float re, im; };
typedef struct _micomplex micomplex;

void print_matrix( const char* desc, int m, int n, micomplex* a, int lda );

int main()
  {

   int m=3, n=2,k=4;
   float alpha=1.0, beta=0.0;
   micomplex *result;

// result = (micomplex *)malloc((1 * 1) * sizeof(micomplex));  // gives ridiculous output (36893488147419103232.00, 10.00)
   result = (micomplex *)calloc(1 * 1, sizeof(micomplex));   // gives correct output ( -5.00, 10.00)
// the result must be (1 + 2 i) (3 + 4 i) = -5 + 10 i
   micomplex a[1*1]={{1,2}};
   micomplex b[1*1]={{3,4}};

   print_matrix( "a Matrix is: \n", 1, 1, a, 1 );
   print_matrix( "b Matrix is: \n", 1, 1, b, 1 );

   cblas_cgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,1,1,1,&alpha,a,1,b,1,&beta,result,1);
   print_matrix( "Product a and b is: \n", 1, 1, result, 1);
   free(result);

// dgemm(CblasNoTrans,CblasNoTrans,m,n,k,&alpha,A,k,B,n,&beta,result,n);
// A(mxk) B(kxn) C(mxn)

   micomplex A[3*4]={{1,1},{2,2},{-2,-2},{0,0},
                    {-3,-3},{4,4},{7,7},{2,2},
                    {6,6},{0,0},{3,3},{1,1}};

   micomplex B[4*2]={{-1,-1},{3,3},
                     {0,0},{9,9},
                     {1,1},{-11,-11},
                     {4,4},{-5,-5}};

// result = (micomplex *)malloc((m * n) * sizeof(micomplex)); // gives ridiculous output (-8589934592.00, -6.00) (-8589934592.00, 86.00)
   result = (micomplex *)calloc(m * n, sizeof(micomplex));  // gives correct output (  0.00, -6.00) (  0.00, 86.00)

   print_matrix( "A Matrix is: \n", m, k, A, k );
   print_matrix( "B Matrix is: \n", k, n, B, n );

   cblas_cgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,m,n,k,&alpha,A,k,B,n,&beta,result,n);
   print_matrix( "Product A and B is: \n", m, n, result, n);
   free(result);
   exit( 0 );
  }

void print_matrix( const char* desc, int m, int n, micomplex* a, int lda ) {
        int i, j;
        printf( "\n %s\n", desc );
        for( i = 0; i < m; i++ ) {
                for( j = 0; j < n; j++ )
                        printf( " (%6.2f,%6.2f)", a[i*lda+j].re, a[i*lda+j].im );
                printf( "\n" );
        }
}

我认为这个想法是内存分配,例如, 如果在下面的代码中使用的是
malloc
,即
result=(micomplex*)malloc((1*1)*sizeof(micomplex))
然后您有时会得到荒谬的输出,如(36893488147419103232.00,10.00)或不正确的(-3.00,10.00), 但是使用
calloc

result=(micomplex*)calloc(1*1,sizeof(micomplex))
执行代码时,每次(-5.00,10.00)都给出正确的输出

要将代码与
double
一起使用,请进行以下更改:
float
to
double
cblas\u cgemm
to
cblas\u zgemm

#include <stdio.h>
#include <stdlib.h>
#include <cblas.h>

/* Complex datatype */
struct _micomplex { float re, im; };
typedef struct _micomplex micomplex;

void print_matrix( const char* desc, int m, int n, micomplex* a, int lda );

int main()
  {

   int m=3, n=2,k=4;
   float alpha=1.0, beta=0.0;
   micomplex *result;

// result = (micomplex *)malloc((1 * 1) * sizeof(micomplex));  // gives ridiculous output (36893488147419103232.00, 10.00)
   result = (micomplex *)calloc(1 * 1, sizeof(micomplex));   // gives correct output ( -5.00, 10.00)
// the result must be (1 + 2 i) (3 + 4 i) = -5 + 10 i
   micomplex a[1*1]={{1,2}};
   micomplex b[1*1]={{3,4}};

   print_matrix( "a Matrix is: \n", 1, 1, a, 1 );
   print_matrix( "b Matrix is: \n", 1, 1, b, 1 );

   cblas_cgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,1,1,1,&alpha,a,1,b,1,&beta,result,1);
   print_matrix( "Product a and b is: \n", 1, 1, result, 1);
   free(result);

// dgemm(CblasNoTrans,CblasNoTrans,m,n,k,&alpha,A,k,B,n,&beta,result,n);
// A(mxk) B(kxn) C(mxn)

   micomplex A[3*4]={{1,1},{2,2},{-2,-2},{0,0},
                    {-3,-3},{4,4},{7,7},{2,2},
                    {6,6},{0,0},{3,3},{1,1}};

   micomplex B[4*2]={{-1,-1},{3,3},
                     {0,0},{9,9},
                     {1,1},{-11,-11},
                     {4,4},{-5,-5}};

// result = (micomplex *)malloc((m * n) * sizeof(micomplex)); // gives ridiculous output (-8589934592.00, -6.00) (-8589934592.00, 86.00)
   result = (micomplex *)calloc(m * n, sizeof(micomplex));  // gives correct output (  0.00, -6.00) (  0.00, 86.00)

   print_matrix( "A Matrix is: \n", m, k, A, k );
   print_matrix( "B Matrix is: \n", k, n, B, n );

   cblas_cgemm(CblasRowMajor,CblasNoTrans,CblasNoTrans,m,n,k,&alpha,A,k,B,n,&beta,result,n);
   print_matrix( "Product A and B is: \n", m, n, result, n);
   free(result);
   exit( 0 );
  }

void print_matrix( const char* desc, int m, int n, micomplex* a, int lda ) {
        int i, j;
        printf( "\n %s\n", desc );
        for( i = 0; i < m; i++ ) {
                for( j = 0; j < n; j++ )
                        printf( " (%6.2f,%6.2f)", a[i*lda+j].re, a[i*lda+j].im );
                printf( "\n" );
        }
}

为什么不把截图中的代码发布到你的问题中,并将其格式化呢?嗨,伙计们,我把float改成double,把cgemm改成zgemm,结果终于正确了!但是,我仍然很好奇为什么float和cgemm给了我荒谬的输出。。。有人吗,有什么提示吗?谢谢为什么不把截图中的代码发布到你的问题中,并将其格式化呢?嗨,伙计们,我把float改成double,把cgemm改成zgemm,结果终于正确了!但是,我仍然很好奇为什么float和cgemm给了我荒谬的输出。。。有人吗,有什么提示吗?谢谢