在gsl中分配复杂值 [英] Assigning complex values in gsl

查看:145
本文介绍了在gsl中分配复杂值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将GSL用于复数复数向量复数矩阵在我的项目中。我使用的是VS2010,我在配置属性> C / C ++>常规>其他包含目录中添加了库的地址。但是我有一个愚蠢的问题。据我了解,我不能使用 = 分配两个 gsl_complex gsl_vector_complex gsl_matrix_complex 彼此。

I am trying to use GSL for complex numbers, complex vectors and complex matrices in my project. I am using VS2010 and I added the address of library in Configuration Properties>C/C++>General>Additional Include Directories. But I have a stupid problem. As far as I understood, I can not use = to assign two gsl_complex, gsl_vector_complex or gsl_matrix_complex to each other.

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

For vectors I have to use gsl_vector_complex_set and for matrices gsl_matrix_complex_set. But for gsl_complex, I only found GSL_SET_COMPLEX in which I should give the real and imaginary parts seperatly as 2 arguments:

GSL_SET_COMPLEX (zp, real, imaginary)

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

In my code I have such function:

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)));
    }
}

使用此命令,我会遇到以下错误:

Using this, I get following errors:


错误LNK1120:2个未解析的外部引用。

error LNK1120: 2 Unresolved external references.

错误LNK2001:未解析的外部符号 _hypot。

error LNK2001: Unresolved external symbol "_hypot".

error LNK2001:无法解析的外部符号 _log1p。

error LNK2001: Unresolved external symbol "_log1p".

error LNK2001:无法解析的外部符号 _log1p。

error LNK2001: Unresolved external symbol "_log1p".

我不了解这些错误的原因。但我这样重写我的代码:

I didn't understand the reason behind these errors. But I rewrite my code like this:

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的第二行被注释时,没有错误。但是当我取消注释时,我得到以下信息:

Here when the second line in for is commented, there's no error. But when I uncomment it I get the following:


error LNK1120:2个未解析的外部引用。

error LNK1120: 2 non-resolved external references.

error LNK2001:未解析的外部符号 _log1p。

error LNK2001: Unresolved external symbol "_log1p".

错误LNK2019:引用未解析的外部符号 _hypot

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

error LNK2019:引用了未解析的外部符号 _log1p,错误代码为 _gsl_complex_div。在函数 _gsl_complex_logabs中。

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

我没有任何 _gsl_complex_div _gsl_complex_logabs 函数。因此,我很确定问题出在这里。但是我也不能在这里使用GSL_SET_COMPLEX。

I don't have any _gsl_complex_div or _gsl_complex_logabs function in my code. So I am pretty sure that the problem is with assignment here. But I can not use GSL_SET_COMPLEX here too.

有人可以帮我吗?真的没有办法直接为gsl_complex赋值吗?

Can someone help me with this? Is there really no way to assign a value to gsl_complex directly?

推荐答案

如果在这里发布所有代码会更好,因此,我立即从最低GSL中的示例。我做了一些小的更改:

It would be better if you published all your code here, therefore, I immediately used this code from the lowest of the examples of the GSL. I made some small changes:

#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;
}

此代码的输出:
(从红色箭头下方,与GSL示例中的预期输出不匹配)
要获取我的输出,您需要使用IDE(我使用Visual Studio 2015):

Output of this code: (from the red arrow and below there is a mismatch with the expected output in the GSL example) To get my output, you need to IDE (I use Visual Studio 2015):


  1. 插入 your_application属性页->配置属性-> VC ++目录-> (右窗格)可执行目录行中的类型: C:\用户\ ...(您的GSL构建目录路径)... \gsl \发布; $(ExecutablePath)

  2. 同上。在包含目录行中键入: C:\Users\ ...(您的GSL构建目录路径)... \gsl; $(IncludePath )

  3. 同上。在库目录行中键入: C:\用户\ ...(您的GSL构建目录路径)... > gsl\发布; $(LibraryPath)

  4. 在下面的左窗格中,选择C / C ++->处理器-> (右窗格)行 Preprocessor Defenition 中的类型: WIN32; _DEBUG; _CONSOLE; GSL_DLL;%(PreprocessorDefinitions)(我使用调试模式,创建了空控制台应用程序)。保存设置(按应用和确定按钮)

  5. 复制并放入应用程序项目文件夹 gsl.dll 来自 C:\Users\ ...(您的GSL构建目录路径)... \gsl\发布目录

  6. 可以运行您的应用程序

  7. 注意!:首先,最好使用编译器重建GSL。

  1. into "your_application" Property pages -> Configuration Properties -> VC++ Directories -> (right pane)in line Executable Directories type: C:\Users\ ...(your GSL build directory path)... \gsl\Release;$(ExecutablePath)
  2. ibid. in line Include Directories type: C:\Users\ ...(your GSL build directory path)... \gsl;$(IncludePath)
  3. ibid. in line Library Directories type: C:\Users\ ...(your GSL build directory path)... \gsl\Release;$(LibraryPath)
  4. Below, in the left pane, select C/C++ -> Peprocessor -> (right pane) in line Preprocessor Defenition type: WIN32;_DEBUG;_CONSOLE;GSL_DLL;%(PreprocessorDefinitions) (I use Debug mode, created empty console application). Save settings (press "Apply" and "OK" buttons)
  5. Copy and put into Debug directories of your application project folder gsl.dll and gslcblas.dll from C:\Users\ ...(your GSL build directory path)... \gsl\Release directory
  6. Buld your application and run it
  7. NOTE!: In the beginning it is best to rebuild GSL with your compiler for the target application - then the work will be guaranteed.

祝您好运!

这篇关于在gsl中分配复杂值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆