我有一个非常简单的问题 - 我只想在C中的gsl中将两个复杂条目的矩阵相乘。所以例如我想定义一个函数
gsl_matrix_complex *multiply( gsl_matrix_complex *A, gsl_matrix_complex *B ) {
???
}
当条目是双打时我已经看到它完成了,但我无法弄清楚适当的概括。任何帮助将不胜感激!
再次感谢。
答案 0 :(得分:2)
To multiply matrices in GSL you need to use the slightly cryptic BLAS interface.
Assuming that the matrices you are going to multiply don't have any known structure (e.g. are symmetric or hermitian etc.). The appropriate function to use would be gsl_blas_zgemm
. The translation of the seemingly random string of letters in the function name is:
z = double precision complex
ge = general matrices
mm = matrix-matrix multiplication
(For a list of all the abbreviations see here)
Using the information in the docs we could then write your function as
#include <gsl/gsl_blas.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_complex_math.h>
gsl_matrix_complex *multiply( gsl_matrix_complex *A, gsl_matrix_complex *B)
{
gsl_matrix_complex *result = gsl_matrix_complex_alloc(A->size1, B->size2);
gsl_blas_zgemm(CblasNoTrans, CblasNoTrans,
GSL_COMPLEX_ONE, A, B,
GSL_COMPLEX_ZERO, result);
return result;
}
Note that instead of allocating the result matrix in the multiplication function it would instead be better practice to pass in one that has already been allocated in the calling function, for example with the signature:
void multiply*(gsl_matrix_complex *A, gsl_matrix_complex *B, gsl_matrix_complex *result);
and then omit the allocation step and the explicit return. The reason for this is that it allows you to minimize the number of heap allocations your code performs as well as making it harder for you to accidentally write code that leaks memory.