From: Goffioul M. <gof...@im...> - 2004-05-07 09:05:22
|
> Michael Goffioul asks: > >Is it possible to use complex numbers with FFI? What I want to=20 > >do is to pass=20 > >complex numbers from LISP to C code (whatever the format=20 > >received by the C function) >=20 > Since there's no standard format for complex numbers in C,=20 "complex" type modifier is beginning to appear (I don't know if it's a recent standard from *C99* or a gcc extensions), but I don't to which memory model it corresponds. Anyway, I guess that mapping a LISP complex object to double* is reasonable. > the CLISP FFI doesn't provide standardized means to pass such objetcs. > My guess is that a good enough approach is to pass real and=20 > imaginary part separately, each using a known format (e.g.=20 > double-float) and COERCE to this chosen format on the Lisp side. >=20 > Is there anything else that would make you happy? Well, yes: a MatLisp version that work with CLISP... :-) >=20 > You could also define a C-STRUCT {double real, imag}, write=20 > two functions to convert Common Lisps COMPLEX numbers to that=20 > structure and vice-versa and pass along FFI pointers to that=20 > a structure. > You should then investigate DEF-C-TYPE and DEF-C-STRUCT and=20 > the constructors automatically generated by the FFI. Probably=20 > this approach looks like a better design than the above. >=20 > Don't the library you want to interface to provide some=20 > representation of complex numbers already? The target math library is BLAS/LAPACK, which are FORTRAN libraries. In FORTRAN, complex numbers are represented as real and imag part next to each other: for example, a single complex number can be mapped to a real array of 2 elements (this would corresponds the structure you mentionned above). Writing conversion routines is a possibility, but I fear that efficiency would be low, especially if you want to convert large complex arrays (if you need to do it in a loop). Is there a way to make C code access LISP variables directly (without copying)? Michael. |