我正试图使用GNU科学库编写一个简单的程序来对复杂(厄米)哈密顿矩阵进行对角化。我已经搜索了文档和互联网,但我根本找不到为gsl_matrix分配复杂值的过程。
它会是像这样简单的东西吗:
gsl_complex Hij = gsl_complex_rect(double x, double y)
gsl_matrix_set(H, i, j, Hij)
这假设gsl_matrix_set检测复数(相对于实数)并自动处理内存分配。
或者,它会是这样的吗:
gsl_matrix_set(H, i, j, x)
gsl_matrix_set(H, i, j+1, y)
假设复数作为一对数字存储在内存中,就像其他地方的惯例一样。但是,那么本征系统例程将如何进行区分呢?我真的被这件简单的事情弄糊涂了。
如何将复数分配给gsl_matrix?
使用gsl_matrix_complex_set(gsl_matrix_complex H, size_t n, size_t m, gsl_complex z);
,例如将矩阵中的所有条目设置为$n+im$:
#include<complex.h>//Required for better complex notation (a+I*b)
#include <gsl/gsl_math.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_complex.h>
#include <stdio.h>
int main()
{
int N =5;
int M =6;
gsl_matrix_complex* H = gsl_matrix_complex_alloc (N, M);
gsl_matrix_complex_set_zero(H);//From my testing, the matrix is usually 0 when it is created, but the documentation does not GUARANTEE this
//You could also use:
//gsl_matrix_complex_set_all(H, 0+0*I);//c complex notation! Requires that <complex.h> was included prior to <gsl_complex.h>!!!
//Loop through all our states and calculate all matrix elements
for (int n = 0; n < N;++n)
for (int m = 0; m < M;++m)
{
gsl_complex z= (n+I*m);//Likely reall the result of <i | H | j> calculation, in practice we know the Hamiltonian is hermitian, so we could skip half the calculations, but lets just set this to i+I j
gsl_matrix_complex_set(H, n, m, z);
}
//Lets just print the result, to see that this worked
for (int n = 0; n < N;++n)
{
for (int m = 0; m < M;++m)
{
gsl_complex z=gsl_matrix_complex_get(H, n, m);
printf("%f+i %f ",GSL_REAL(z),GSL_IMAG(z));
}
printf ("n");
}
gsl_matrix_complex_free(H);
return 0;
}
我知道这个问题已经过时了,但它仍然会出现在搜索结果中。