1

I have a list of varying size that contains numpy arrays with the same data type and shape. I would like to process this data using a function written in Cython without copying the data. Both memoryviews and the Python buffer protocol seem to support this kind of data using indirect for the first dimension. So I was hoping that something like this could work:

%%cython
from cython.view cimport indirect
def test(list a):
    cdef double[::indirect, :] x
    x = a
    x[0, 0] = 42

Unfortunately it doesn't.

Is there a way to convert this list of numpy arrays into such a memoryview?

unique2
  • 2,162
  • 2
  • 18
  • 23
  • Sorry, can you give a little more context about what you're trying to do? Is `a` supposed to be a list of lists or something? – ngoldbaum Jun 07 '18 at 20:59
  • Oh I see, a list of numpy arrays. So you want to be able to represent ragged arrays in cython? – ngoldbaum Jun 07 '18 at 20:59
  • They are not jagged. The numpy arrays have all the same data type and shape. However the underlying data sits at irregular intervals in memory, so I cannot use a strided memoryview to represent it. – unique2 Jun 07 '18 at 21:11
  • 2
    I'm afraid there is no easy way. This solution (https://stackoverflow.com/a/12991519/5769463) introduces its own container which make it possible to create indirect typed memory views. But I haven't tested it. – ead Jun 08 '18 at 06:27

0 Answers0