Re: ctypes and C99 complex numbers

Discussion in 'Python' started by Thomas Heller, Aug 16, 2007.

  1. Eugen Wintersberger schrieb:
    > Hi there
    > I want to use ctypes in connection with C functions that use complex
    > datatypes defined in the C99 standard. Does someone know a simple way
    > how to implement this? Are there any plans to integrate the C99 complex
    > data types in the ctypes module?


    I have investigated the complex data types a bit and came to the conclusion
    that libffi (which ctypes is based upon) would have to be extended to
    support these complex types. I have not looked into the newer libffi
    sources to find out if this support already has been added, ctypes uses
    an older version of libffi.

    By experimenting I found out that on some systems (x86 linux and x86_64 linux)
    I could 'emulate' the complex type by defining a structure:

    class Complex(Structure):
    _fields_ = [("real", c_double), ("imag", c_double)]
    def _get(self):
    ....
    def _set(self, value):
    ....
    value = property(_get, _set)

    but I'm afraid this is not really portable to other platforms.

    Thomas
    Thomas Heller, Aug 16, 2007
    #1
    1. Advertising

  2. Thomas Heller schrieb:
    > Eugen Wintersberger schrieb:
    >> Hi there
    >> I want to use ctypes in connection with C functions that use complex
    >> datatypes defined in the C99 standard. Does someone know a simple way
    >> how to implement this? Are there any plans to integrate the C99 complex
    >> data types in the ctypes module?

    >
    > I have investigated the complex data types a bit and came to the conclusion
    > that libffi (which ctypes is based upon) would have to be extended to
    > support these complex types.


    I think that depends on what kind of support you want to provide. For
    passing _Complex values as parameters or receiving them as results,
    it's certainly necessary to have libffi support.

    OTOH, if you only want to support _Complex as the field type of a
    struct or an array, you can do so without libffi support. To write
    a field at address p with the values r and i, do

    *(double complex*)p = r * i*I;

    To fetch the real and imaginary part of a field at address p, do

    creal(*(double complex*)p)
    cimag(*(double complex*)p)

    Likewise for float complex values (and long double complex, if you
    want to support that).

    Whether such a limited version would be of any use (e.g. to the OP),
    I don't know.

    Regards,
    Martin
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Aug 16, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Glen Low

    C99 complex numbers and aliasing

    Glen Low, Aug 3, 2004, in forum: C Programming
    Replies:
    5
    Views:
    401
    David R Tribble
    Aug 20, 2004
  2. Replies:
    3
    Views:
    3,646
    Chris Torek
    Feb 20, 2006
  3. Replies:
    3
    Views:
    581
    Keith Thompson
    Mar 31, 2007
  4. David Marsh

    operations on complex numbers in C99

    David Marsh, Aug 6, 2007, in forum: C Programming
    Replies:
    27
    Views:
    1,573
    jacob navia
    Aug 9, 2007
  5. Eugen Wintersberger

    ctypes and C99 complex numbers

    Eugen Wintersberger, Aug 16, 2007, in forum: Python
    Replies:
    0
    Views:
    333
    Eugen Wintersberger
    Aug 16, 2007
Loading...

Share This Page