c - 在 gsl 中分配复数

标签 c visual-studio dll complex-numbers gsl

我正在尝试在我的项目中将 GSL 用于复数复数 vector 复数矩阵。我使用的是 VS2010,我在 Configuration Properties>C/C++>General>Additional Include Directories 中添加了库的地址。但是我有一个愚蠢的问题。据我所知,我不能使用 = 将两个 gsl_complexgsl_vector_complexgsl_matrix_complex 分配给彼此.

对于 vector ,我必须使用 gsl_vector_complex_set,对于矩阵,我必须使用 gsl_matrix_complex_set。但是对于 gsl_complex,我只找到了 GSL_SET_COMPLEX,其中我应该将实部和虚部分别作为 2 个参数给出:

GSL_SET_COMPLEX (zp, real, imaginary)

在我的代码中我有这样的功能:

gsl_complex cmx, cmx2;
void vector_complex_exp(gsl_vector_complex *v)
{
    for (i = 0; i < v->size; i++)
    {
        gsl_vector_complex_set(v, i, gsl_complex_exp(gsl_vector_complex_get(v, i)));
    }
}

使用这个,我得到以下错误:

error LNK1120: 2 Unresolved external references.

error LNK2001: Unresolved external symbol "_hypot".

error LNK2001: Unresolved external symbol "_log1p".

error LNK2001: Unresolved external symbol "_log1p".

我不明白这些错误背后的原因。但是我这样重写我的代码:

void vector_complex_exp(gsl_vector_complex *v)
{
    for (i = 0; i < v->size; i++)
    {
        cmx = gsl_vector_complex_get(v, i);
        //cmx2 = gsl_complex_exp(cmx);
        gsl_vector_complex_set(v, i, cmx2);
    }
}

这里当注释掉for中的第二行时,没有报错。但是当我取消注释时,我得到以下信息:

error LNK1120: 2 non-resolved external references.

error LNK2001: Unresolved external symbol "_log1p".

error LNK2019: Reference to non-resolved external symbol "_hypot" in function "_gsl_complex_div".

error LNK2019: Reference to non-resolved external symbol "_log1p" in function "_gsl_complex_logabs".

我的代码中没有任何 _gsl_complex_div_gsl_complex_logabs 函数。所以我很确定问题出在这里的分配上。但是我也不能在这里使用 GSL_SET_COMPLEX。

有人可以帮我解决这个问题吗?真的没有办法直接给gsl_complex赋值吗?

最佳答案

如果你把你所有的代码都发布在这里会更好,因此,我立即使用了来自the lowest of the examples的代码。 GSL的。我做了一些小改动:

#include <stdio.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_eigen.h>

int main(void)
{
double data[] = { -1.0, 1.0, -1.0, 1.0,
    -8.0, 4.0, -2.0, 1.0,
    27.0, 9.0, 3.0, 1.0,
    64.0, 16.0, 4.0, 1.0 };

gsl_matrix_view m
    = gsl_matrix_view_array(data, 4, 4);

gsl_vector_complex *eval = gsl_vector_complex_alloc(4);
gsl_matrix_complex *evec = gsl_matrix_complex_alloc(4, 4);

gsl_eigen_nonsymmv_workspace * w = gsl_eigen_nonsymmv_alloc(4);

gsl_eigen_nonsymmv(&m.matrix, eval, evec, w);

gsl_eigen_nonsymmv_free(w);

gsl_eigen_nonsymmv_sort(eval, evec, GSL_EIGEN_SORT_ABS_DESC);

{
    int i, j;

    for (i = 0; i < 4; i++)
    {
        gsl_complex eval_i
            = gsl_vector_complex_get(eval, i);
        gsl_vector_complex_view evec_i
            = gsl_matrix_complex_column(evec, i);

        printf("\n eigenvalue = %g + %gi\n",
            GSL_REAL(eval_i), GSL_IMAG(eval_i));
        printf(" eigenvector = \n");
        for (j = 0; j < 4; ++j)
        {
            gsl_complex z =
                gsl_vector_complex_get(&evec_i.vector, j);
            printf("        %g + %gi\n", GSL_REAL(z), GSL_IMAG(z));
        }
    }
}

gsl_vector_complex_free(eval);
gsl_matrix_complex_free(evec);

system("pause");

return 0;
}

Output of this code: (从红色箭头和下方看,与 GSL 示例中的预期输出不匹配) 要获得我的输出,您需要 IDE(我使用 Visual Studio 2015):

  1. 进入“your_application”属性页 -> 配置属性 -> VC++ 目录 ->(右 Pane )在 可执行目录 行中键入:C:\Users\... (您的 GSL 构建目录路径)...\gsl\Release;$(ExecutablePath)
  2. 同上。在 Include Directories 行中键入:C:\Users\...(您的 GSL 构建目录路径)...\gsl;$(IncludePath)
  3. 同上。在 Library Directories 行中键入:C:\Users\...(您的 GSL 构建目录路径)...\gsl\Release;$(LibraryPath)
  4. 在下方的左 Pane 中,选择 C/C++ -> Peprocessor ->(右 Pane ),在 Preprocessor Defenition 行中键入:WIN32;_DEBUG;_CONSOLE;GSL_DLL;%(PreprocessorDefinitions )(我使用 Debug模式,创建了空的控制台应用程序)。保存设置(按“应用”和“确定”按钮)
  5. C:\Users\ 复制并放入应用程序项目文件夹 gsl.dllgslcblas.dll 的调试目录... (你的 GSL 构建目录路径)...\gsl\Release 目录
  6. 构建您的应用程序并运行它
  7. 注意!开始时最好使用您的编译器为目标应用程序重新构建 GSL - 然后工作将得到保证。

祝你好运!

关于c - 在 gsl 中分配复数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46526274/

相关文章:

c# - Visual Studio 2005 上的编译时间非常慢

c++ - visual c++ - 缺少不相关的 DLL

c - 如何将 .so 文件链接到 .so 文件

c# - 如何为动态加载的 native dll 调整 %PATH%?

c - 如何成功输出一个函数调用?

c++ - _asm 在 C 代码中不起作用,如何启用它

C编程: Stackoverflow is a cause of abrupt termination

VC2012 中与 fwrite 的混淆

c - BST 实现函数中的段错误

c++ - Boost 线程和 UPX 压缩 == 不是有效的 win32 应用程序?