Cython with Numpy, Complex Numbers

817 views
Skip to first unread message

Till Stensitzki

unread,
Feb 2, 2010, 12:23:13 PM2/2/10
to cython-users
Hello,
i am trying to subistute a small bit of Python Code with Cython for a
speed up. While Cython itself dosen't complain, but gcc does.

from __future__ import division
import numpy

cimport numpy
cimport cython

def calc_shg(numpy.ndarray[numpy.complex128_t, ndim = 1] par,
numpy.ndarray[numpy.complex128_t, ndim = 1] spectrum,
numpy.ndarray[numpy.complex128_t, ndim = 1] phase,
numpy.ndarray[numpy.complex128_t, ndim = 1] filter, float
best_fit):
cdef int pix
cdef int i
cdef numpy.ndarray[numpy.complex128_t, ndim = 1] shaped_pulse

shaped_pulse = numpy.zeros(4096)
par = numpy.exp(-1j * par * 0.002 * numpy.pi)
for pix in xrange(128):
for i in xrange(25 - 1):
filter[2048 - 25 * 64 + pix * 25 + i] = par
[pix]
for i in xrange(4096):
shaped_pulse[i] = numpy.fft.ifft(spectrum[i] * (phase[i] * filter
[i]))
return - sum(numpy.real(shaped_pulse) ** 4) / best_fit

Error:

Sim_Cython.c: In function `__pyx_pf_10Sim_Cython_calc_shg':
Sim_Cython.c:1187: error: incompatible type for argument 1 of
`__pyx_t_double_complex_mul'
Sim_Cython.c:1187: error: incompatible type for argument 2 of
`__pyx_t_double_complex_mul'
Sim_Cython.c:1187: error: incompatible type for argument 1 of
`__pyx_t_double_complex_mul'
Sim_Cython.c:1187: error: incompatible type for argument 1 of
`__pyx_t_double_complex_mul'
Sim_Cython.c:1187: error: incompatible type for argument 2 of
`__pyx_t_double_complex_mul'
Sim_Cython.c:1187: error: incompatible type for argument 1 of
`__pyx_t_double_complex_mul'
Sim_Cython.c:1198: error: incompatible types in assignment

Dag Sverre Seljebotn

unread,
Feb 2, 2010, 1:58:30 PM2/2/10
to cython...@googlegroups.com
Till Stensitzki wrote:
> Hello,
> i am trying to subistute a small bit of Python Code with Cython for a
> speed up. While Cython itself dosen't complain, but gcc does.
>
Looks like a bug to me. Can you have a look in the C file and paste the
section around line number 1187 (from, and including, the
corresponding-Cython-code comment, and ending at the next such comment.)

Dag Sverre

Till Stensitzki

unread,
Feb 2, 2010, 7:06:43 PM2/2/10
to cython-users
I put the whole file on pastebin:

http://pastebin.com/m76bf4f0b

Compilier: gcc 4.4.0, cython 0.12.

On 2 Feb., 19:58, Dag Sverre Seljebotn <da...@student.matnat.uio.no>
wrote:

Dag Sverre Seljebotn

unread,
Feb 3, 2010, 3:20:48 AM2/3/10
to cython...@googlegroups.com
Till Stensitzki wrote:
> I put the whole file on pastebin:
>
> http://pastebin.com/m76bf4f0b
>
> Compilier: gcc 4.4.0, cython 0.12.
>
I ended up compiling your original example, and it compiles for me so I
cannot reproduce. That was using Cython 0.12.1. Can you confirm that the
issue is fixed in 0.12.1?

If not, it might be some platform-dependant issues with C complex numbers...

Dag Sverre

Till Stensitzki

unread,
Feb 3, 2010, 5:08:52 AM2/3/10
to cython-users
Oh, i see that i missed some part of the error message, sorry:

Sim_Cython.c: At top level:
C:/Python26/lib/site-packages/numpy/core/include/numpy/
__multiarray_api.h:969: warning: '_import_array' defined but not used
Sim_Cython.c:781: warning: '__pyx_k_3' defined but not used
Sim_Cython.c:782: warning: '__pyx_k_4' defined but not used
Sim_Cython.c:802: warning: '__pyx_k_24' defined but not used
Sim_Cython.c:808: warning: '__pyx_k_26' defined but not used
Sim_Cython.c:809: warning: '__pyx_k_27' defined but not used
error: command 'gcc' failed with exit status 1

But these are only warings, i think.
Maybe my build file is wrong? But i ll try to 0.12.1 today.

from distutils.core import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy as np
setup(
cmdclass = {'build_ext': build_ext},
ext_modules = [Extension("Sim_Cython", ["Sim_Cython.pyx"])],
include_dirs = ['.', np.get_include()]
)

On 3 Feb., 09:20, Dag Sverre Seljebotn <da...@student.matnat.uio.no>

Till Stensitzki

unread,
Feb 3, 2010, 10:54:19 AM2/3/10
to cython-users
Ok, i tried cython 0.12.1, but still the same error.

Till Stensitzki

unread,
Feb 4, 2010, 9:52:10 AM2/4/10
to cython-users
Ok, i resolved the problem. I am a little bit perplexed why it works
now:
I comment out the par=... line, it compiles. I but it back in, it
compiles.
Maybe some kind of leftover file which prevented compiling.
Reply all
Reply to author
Forward
0 new messages