Subject: RE: [clisp-list] FFI with complex numbers?

From: "Goffioul Michael" <[email-masked]>
Date: Fri May 7 02:06:01 2004

> Michael Goffioul asks:
> >Is it possible to use complex numbers with FFI? What I want to 
> >do is to pass 
> >complex numbers from LISP to C code (whatever the format 
> >received by the C function)
> 
> Since there's no standard format for complex numbers in C, 

"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 
> imaginary part separately, each using a known format (e.g. 
> double-float) and COERCE to this chosen format on the Lisp side.
> 
> Is there anything else that would make you happy?

Well, yes: a MatLisp version that work with CLISP... :-)

> 
> You could also define a C-STRUCT {double real, imag}, write 
> two functions to convert Common Lisps COMPLEX numbers to that 
> structure and vice-versa and pass along FFI pointers to that 
> a structure.
> You should then investigate DEF-C-TYPE and DEF-C-STRUCT and 
> the constructors automatically generated by the FFI. Probably 
> this approach looks like a better design than the above.
> 
> Don't the library you want to interface to provide some 
> 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.

---

From: Raymond Toy <[email-masked]>
Date: Mon May 10 11:14:03 2004

>>>>> "Goffioul" == Goffioul Michael <[email-masked]> writes:

    Goffioul> Well, yes: a MatLisp version that work with CLISP... :-)

You could help make that a reality, if it's important to you.  If
you're interested, subscribe to the matlisp mailing lists on
sourceforge.net.

I think there are quite a few people out there who would like such a
thing.

    Goffioul> The target math library is BLAS/LAPACK, which are FORTRAN libraries.
    Goffioul> In FORTRAN, complex numbers are represented as real and imag part
    Goffioul> next to each other: for example, a single complex number can be mapped
    Goffioul> to a real array of 2 elements (this would corresponds the structure
    Goffioul> you mentionned above).
    Goffioul> Writing conversion routines is a possibility, but I fear that efficiency
    Goffioul> would be low, especially if you want to convert large complex arrays
    Goffioul> (if you need to do it in a loop). Is there a way to make C code access
    Goffioul> LISP variables directly (without copying)?

This is certainly possible since this is what Matlisp does.  However,
it does require your Lisp to have a specialized array with
double-float elements that are arranged in one contiguous area, just
like C or Fortran would.  Given this, you only need a routine that can
figure the address of this, and Matlisp would work, once the Matlisp
Fortran FFI is mapped to clisp's FFI.

Ray