about return value from gsl

Started by kuan, December 08, 2012, 09:47:43 AM

Previous topic - Next topic

kuan

I want to do some scientific computing with gsl. but encounter some error. below is my code

> (import LIB "gsl_complex_rect" "void*" "double" "double")
gsl_complex_rect@7FBAD0403FD0
> (unpack "lf lf" (gsl_complex_rect 2.0 3.0))
Segmentation fault (core dumped)

the complex defined in gsl like this:

typedef struct
     {
       double dat[2];
     } gsl_complex;

and the funciton gsl_complex_rect like this:
Quote


 -- Function: gsl_complex gsl_complex_rect (double X, double Y)

     This function uses the rectangular Cartesian components (X,Y) to

     return the complex number z = x + i y.  An inline version of this

     function is used when `HAVE_INLINE' is defined.


how can I print the complex returned by gsl_complex_rect?

thanks .

Lutz

#1
Looking at the source code for gsl-1.15, I see that 'gsl_complex_rect' is compiled as an inline function and should not be imported. On OSX it crashed - CORRECTION the code in the next post works not only on Windows but also OSX.



I guess this inline function exists in the gsl library because many of its functions take complex values. For these functions you could declare a structure using the newLISP function 'struct:



(struct 'complex "double" "double")
(import "ffitest.dylib" "complex_test" "complex" "complex")

(complex_test (pack complex 3.0 4.0)) ;=> (3 4)

; you also could pass the numbers in a list - from a previous result

(complex_test (pack complex '(3 4))) ;=> (3 4)


in this example 'complex_test takes a parameter of type complex and returns a complex value. This is the C-source of gsl_complex complex_test(gel_complex), which I used on Mac OSX:



/* gcc -m32 ffitest.c -shared -o ffitest.dylib */

typedef struct
{
double dat[2];
} gsl_complex;


gsl_complex complex_test(gsl_complex gs)
{
return(gs);
}


the function simply returns its input value and displayed as a list.



See the next post making "gsl_complex_rect" work on Both Mac OSX and Windows.

Lutz

#2
I just realize that on Windows  gsl_complex_rect() is compiled as a real function in "libsl-0.dll" and the following worked for me on XP:



(struct 'complex "double" "double")
(import LIB "gsl_complex_rect" "complex" "double" "double")

(gsl_complex_rect 3.0 4.0) ;=> (3 4)


but when passing complex parameters to gsl library functions, then use the struct method shown in my first reply.



ps: above also works on Mac OSX, make sure the library is compiled without HAVE_INLINE

kuan

#3
thank you lutz, it does work.