I am relatively new to Cython and have encountered an error that my research has failed me on (I am using Python3 in spyder and my Sython version is 0.26)
I tried this:
import cython
@cython.boundscheck(False)
def boundtest():
cdef int r=4
cdef double l[3]
and it works fine. But then I tried this:
import cython
@cython.boundscheck(False)
def boundtest():
cdef int r=4
cdef double l[r]
and I receive the error
[1/1] Cythonizing test.pyx
Error compiling Cython file:
------------------------------------------------------------
...
import cython
@cython.boundscheck(False)
def boundtest():
cdef int r=4
cdef double l[r]
^
------------------------------------------------------------
test.pyx:13:20: Not allowed in a constant expression
The decorator was added due to finding this related stackexchange post and reading the Cython book by Kurt W. Smith. As far as I can tell this should work to tell Cython not to worry about out of bounds errors that may result from having a dynamic indexing variable but for some reason it does not. I have also tried changing boundscheck in the compiler options and globally to no avail.
If it weren't for the Cython documentation claiming to be up to date I would think boundscheck has been depreciated.
UPDATE
I realized I have used import Cython
instead of cimport cython
. I tried again with
cimport cython
But get the same error.
UPDATE 2
On a similar note the code
cdef int N = 3
cdef double[:] lout = array.array('d', N)
throws the error
TypeError: 'int' object is not iterable
I assume for the same reason that C cannot deal with (possibly) dynamic array allocation. Instead we must use
cdef double[:] lout = numpy.empty(N, 'd')
and I assume there is a line that converts N into a static type somewhere before putting it into the C-array