我想调用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)
这是我的代码截图
答案 0 :(得分:0)
我认为这个想法是内存分配,例如,
如果在下面的代码中,您使用的是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
至double
和cblas_cgemm
至cblas_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" );
}
}
输出:
a Matrix is:
( 1.00, 2.00)
b Matrix is:
( 3.00, 4.00)
Product a and b is:
( -5.00, 10.00)
A Matrix is:
( 1.00, 1.00) ( 2.00, 2.00) ( -2.00, -2.00) ( 0.00, 0.00)
( -3.00, -3.00) ( 4.00, 4.00) ( 7.00, 7.00) ( 2.00, 2.00)
( 6.00, 6.00) ( 0.00, 0.00) ( 3.00, 3.00) ( 1.00, 1.00)
B Matrix is:
( -1.00, -1.00) ( 3.00, 3.00)
( 0.00, 0.00) ( 9.00, 9.00)
( 1.00, 1.00) (-11.00,-11.00)
( 4.00, 4.00) ( -5.00, -5.00)
Product A and B is:
( 0.00, -6.00) ( 0.00, 86.00)
( 0.00, 36.00) ( 0.00,-120.00)
( 0.00, 2.00) ( 0.00,-40.00)