Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: ctypes and C99 complex numbers

Reply
Thread Tools

Re: ctypes and C99 complex numbers

 
 
Thomas Heller
Guest
Posts: n/a
 
      08-16-2007
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

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      08-16-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
ctypes and C99 complex numbers Eugen Wintersberger Python 0 08-16-2007 12:07 PM
operations on complex numbers in C99 David Marsh C Programming 27 08-09-2007 11:31 AM
Difference between "library parts" of C99 and "language parts" of C99 albert.neu@gmail.com C Programming 3 03-31-2007 08:14 PM
C99 struct initialization (C99/gcc) jilerner@yahoo.com C Programming 3 02-20-2006 04:41 AM
C99 complex numbers and aliasing Glen Low C Programming 5 08-20-2004 10:13 PM



Advertisments