我试图用C语言中BLAS库的zgemm函数将一个复数向量乘以一个复数矩阵。
这是我正在使用的代码:

void dot(complexArray* mat1, char transa, complexArray* mat2, char transb, complexArray* out)
{
ptrdiff_t m = (ptrdiff_t)mat1->m;
ptrdiff_t n = (ptrdiff_t)mat2->n;
ptrdiff_t k = (ptrdiff_t)mat1->n;

ptrdiff_t lda = (ptrdiff_t)mat1->m;
ptrdiff_t ldb = (ptrdiff_t)mat2->m;
ptrdiff_t ldc = (ptrdiff_t)out->m;

//scalar factors
double alpha[2] = {1,0};
double beta[2] = {0,0};

//BLAS routine for complex matrix multiplication
zgemm(&transa, &transb, &m, &n, &k, alpha, mat1->data, &lda, mat2->data,
        &ldb, beta, out->data, &ldc);
}

complexArray结构定义如下:
typedef struct
{
double* data;
size_t m;
size_t n;
} complexArray;

但是,如果我通过
dot(array1, 'C', array2, 'N', resultArray);

其中array1.m==r,array1.n==1和array2.m==array2.n==r
216692908932268360000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000 + 4451969616001722900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0000i

由于在变量transa(当然是不同形状的向量/矩阵组合)中使用参数为“N”的同一函数,我担心我误解了transa参数。有没有提示错误在哪里?

最佳答案

好吧,我想我在FORTRAN文档中找到了BLAS函数的答案(参见dgemm documentation
参数M、N和K(在我的例子中是M、N和K)不是指原始矩阵的行和列,而是指最终使用的矩阵,即转置矩阵或共轭矩阵。因此,如果对TRANSA或TRANSB参数使用'T'或'C'选项,请确保还分别交换M和N或N和K。

10-06 04:34