Discussion:
manipulating complex numbers with cython
Lev Givon
2008-01-07 15:43:21 UTC
Permalink
I am currently attempting to rewrite an algorithm that processes
complex numbers with pyrex or cython. Although it seems that neither
currently supports straightforward interfacing with the C complex
number type, does anyone know of any provisional examples of
converting between C complex numbers and the equivalents provided by
python and/or the numpy module? The one or two perfunctory examples I
have come across online have not been especially enlightening.

L.G.
William Stein
2008-01-07 16:17:52 UTC
Permalink
Post by Lev Givon
I am currently attempting to rewrite an algorithm that processes
complex numbers with pyrex or cython. Although it seems that neither
currently supports straightforward interfacing with the C complex
number type, does anyone know of any provisional examples of
converting between C complex numbers and the equivalents provided by
python and/or the numpy module? The one or two perfunctory examples I
have come across online have not been especially enlightening.
I wrote a Cython wrapper for Sage (sagemath.org) of the GSL
(http://www.gnu.org/software/gsl/)
double-precision complex number type:
http://www.sagemath.org/hg/sage-main/file/addc96bacb8b/sage/rings/complex_double.pyx
http://www.sagemath.org/hg/sage-main/file/addc96bacb8b/sage/rings/complex_double.pxd
Stefan Behnel
2008-01-07 16:20:35 UTC
Permalink
Hi,
Post by Lev Givon
I am currently attempting to rewrite an algorithm that processes
complex numbers with pyrex or cython. Although it seems that neither
currently supports straightforward interfacing with the C complex
number type, does anyone know of any provisional examples of
converting between C complex numbers and the equivalents provided by
python and/or the numpy module? The one or two perfunctory examples I
have come across online have not been especially enlightening.
You can access the numeric parts of a Python complex numbers straight away:

http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/Manual/extension_types.html#ExternalExtTypes

The rest of the interfacing should than be as simple as a C function (or
macro). To import the 'complex' C type from complex.h, try something like this:

cdef extern from "complex.h":
ctypedef int complex

but then take a little care that you do not let Cython generate conversion
code automatically as the above will let it think it knows what to do when you
assign a complex to an int.

Stefan
Robert Bradshaw
2008-01-09 21:00:58 UTC
Permalink
Post by Stefan Behnel
Hi,
Post by Lev Givon
I am currently attempting to rewrite an algorithm that processes
complex numbers with pyrex or cython. Although it seems that neither
currently supports straightforward interfacing with the C complex
number type, does anyone know of any provisional examples of
converting between C complex numbers and the equivalents provided by
python and/or the numpy module? The one or two perfunctory examples I
have come across online have not been especially enlightening.
You can access the numeric parts of a Python complex numbers
http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/
Doc/Manual/extension_types.html#ExternalExtTypes
The rest of the interfacing should than be as simple as a C
function (or
macro). To import the 'complex' C type from complex.h, try
ctypedef int complex
but then take a little care that you do not let Cython generate conversion
code automatically as the above will let it think it knows what to do when you
assign a complex to an int.
Rather than declaring it as an int, one is better off declaring it as
a struct or void* so that automatic conversion doesn't happen. If one
knows the structure of a C complex, one can declare it as the "right"
structure (with the named members) and access them like that.

- Robert
Lev Givon
2008-01-10 00:08:30 UTC
Permalink
Post by Stefan Behnel
Hi,
Post by Lev Givon
I am currently attempting to rewrite an algorithm that processes
complex numbers with pyrex or cython. Although it seems that neither
currently supports straightforward interfacing with the C complex
number type, does anyone know of any provisional examples of
converting between C complex numbers and the equivalents provided by
python and/or the numpy module? The one or two perfunctory examples I
have come across online have not been especially enlightening.
http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/version/Doc/Manual/extension_types.html#ExternalExtTypes
The rest of the interfacing should than be as simple as a C function (or
ctypedef int complex
but then take a little care that you do not let Cython generate conversion
code automatically as the above will let it think it knows what to do when you
assign a complex to an int.
Rather than declaring it as an int, one is better off declaring it as a
struct or void* so that automatic conversion doesn't happen. If one knows
the structure of a C complex, one can declare it as the "right" structure
(with the named members) and access them like that.
- Robert
I was able to successfully declare it as a struct containing no
declared members in my pyrex code and then manipulate instances of the
defined complex type using C macros I defined to wrap the appropriate
mathematical operations. I can't specify any named members within the
pyrex type declaration because instances of the complex data type
defined in complex.h cannot be treated as a struct within the
generated C code (at least insofar as gcc 4 is concerned). The latter
isn't really an issue, however, because complex.h provides functions
to extract the real and imaginary portions of a complex number.

L.G.

Loading...