I'm trying to convert a python class to C for time-complexity improvement using Cython. My most used variables are the class variables defined in the init method and therefore I would like to define them as cdef double. I have tried everything I can find but nothing let's me convert the code. It seems as if this should be the way to do it:
class Wall(object):
cdef double min_x, max_x, a, b, c
def __init__(self, start, stop):
self.min_x = min(start[0], stop[0])
self.max_x = max(start[0], stop[0])
self.a = (stop[1]-start[1])/(stop[0]-start[0])
self.b = -1
self.c = start[1]-self.a*start[0]
But I get the following error:
Error compiling Cython file: ------------------------------------------------------------ ... class Wall(object): cdef double min_x, max_x, a, b, c ^ ------------------------------------------------------------ wall_class_cy.pyx:9:9: cdef statement not allowed here
What am I doing wrong?
You need to make the class variables public and provide a C declaration for the class as well. Try this:
cdef class Wall(object):
cdef public double min_x, max_x, a, b, c
def __init__(self, start, loop):
...