about return value from gsl

Q&A's, tips, howto's
Locked
kuan
Posts: 6
Joined: Sat Dec 08, 2012 5:32 pm

about return value from gsl

Post by kuan »

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

Code: Select all

> (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:

Code: Select all

 typedef struct
     {
       double dat[2];
     } gsl_complex;
and the funciton gsl_complex_rect like this:

-- 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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: about return value from gsl

Post by Lutz »

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:

Code: Select all

(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:

Code: Select all

/* 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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Re: about return value from gsl

Post by Lutz »

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:

Code: Select all

(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
Posts: 6
Joined: Sat Dec 08, 2012 5:32 pm

Re: about return value from gsl

Post by kuan »

thank you lutz, it does work.

Locked