Assigning complex values in gsl

前端 未结 1 541
余生分开走
余生分开走 2021-01-27 02:52

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

相关标签:
1条回答
  • 2021-01-27 03:23

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

    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. 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.

    Good luck!

    0 讨论(0)
提交回复
热议问题