2016-04-01 8 views
1

Ich versuche Standard Quicksort in Cython mit Memoryviews zu implementieren. Hier ist mein Code:Cython memoryviews error: Ungültiger Index für memoryview angegeben

def quicksort_cython(double[:] l): 
    _quicksort(l, 0, len(l) - 1) 


cdef void _quicksort(double[:] l, double start, double stop): 
    cdef double pivot, left, right, tmp 
    if stop - start > 0: 
     pivot = l[start] 
     left = start 
     right = stop 
     while left <= right: 
      while l[left] < pivot: 
       left += 1 
      while l[right] > pivot: 
       right -= 1 
      if left <= right: 
       tmp = l[left] 
       l[left] = l[right] 
       l[right] = tmp 
       left += 1 
       right -= 1 
     _quicksort(l, start, right) 
     _quicksort(l, left, stop) 

jedoch bei der Erstellung mit Standard setup.py Datei und python setup.py build_ext --inplace Befehl, den ich mehrere Fehler in Bezug auf die memoryview Zugang erhalten:

Error compiling Cython file: 
------------------------------------------------------------ 
... 


cdef void _quicksort(double[:] l, double start, double stop): 
    cdef double pivot, left, right, tmp 
    if stop - start > 0: 
     pivot = l[start] 
        ^
------------------------------------------------------------ 

quicksort_cython_opt3.pyx:9:23: Invalid index for memoryview specified 

Kann mir jemand sagen, was mache ich falsch? Außerdem werden alle Tipps zur Verbesserung der Performance geschätzt, da ich neu bei Cython bin. Danke!

Antwort

2

In der Regel Doppel kann nicht Indizes sein (sollte ints sein). Ich nehme an, das ist das Problem ... obwohl ich mit cyston memoryviews nicht vertraut bin.

+0

Yeah .. Wechsel zu Int erlaubt mir, diesen Code zu kompilieren. Außerdem musste ich den Typ im übergebenen numpy-Array angeben: 'np.array (l, dtype = np.int32)', damit es funktioniert. Vielleicht war es offensichtlich, aber ich habe das in der Dokumentation nicht gefunden, danke. –

+0

Ich bin froh, dass ich helfen konnte! – mwm314