-1

Im trying to convert a python class to C for time-complexity improvment using Cython. My most used variables are the class varibales 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?

Kind regards, Jakob

JakobVinkas
  • 1,003
  • 7
  • 23
  • I vote to close this as a typo, because it should be `cdef class Wall`, i.e. `cdef` was forgotten. – ead Feb 21 '20 at 10:28

1 Answers1

1

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):
        ...
alec
  • 5,799
  • 1
  • 7
  • 20