0

I am trying to calculate the transpose of a Cython typed memoryview slice (eg. a 2D part of a 3D array). I am encountering a transpose error specifically when I try to transpose a 2D slice of a 3D memoryview array.

Here is my test code

import numpy as np
cimport numpy as cnp
cnp.import_array()
cimport cython

cdef int size_dim1 = 2
cdef int size_dim2 = 2
cdef int size_dim3 = 2

cdef int[:, ::1] arr2D = np.asarray(np.zeros((size_dim2, size_dim3), dtype = np.int32))
cdef int[:, :, ::1] arr3D = np.asarray(np.zeros((size_dim1, size_dim2, size_dim3), dtype = np.int32))

arr2D.T # works fine

arr3D.T # works fine

arr3D[0].T # error

The error I get for arr3D[0].T is ValueError: Cannot transpose memoryview with indirect dimensions.

I found the error in the Cython source code (source code link, line 938) and the error is raised when one of the .suboffsets of the memoryview slice is >=0. But I checked all of the .suboffsets of arr3D[0] and they are all -1 (for the first 2 dimensions). So I don't understand how this error is being raised.

Eric
  • 25
  • 4
  • On a first look that seems like a bug (and so the best thing is to report it on GitHub). It's possible `arr3D[0, :, :].T` might work (but I haven't tested it) – DavidW Aug 21 '23 at 21:29
  • @DavidW Doing `arr3D[0, :, :].T` gives the same error as above. I'll report it as a bug. – Eric Aug 22 '23 at 16:03
  • Thanks - it's possible there's a good reason for it that'll be revealed under close investigation, but it definitely seems like a bug. – DavidW Aug 22 '23 at 18:57

0 Answers0