--
---
You received this message because you are subscribed to the Google Groups "cython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cython-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/db7c1ae6-c15e-4337-a9f8-ae5bb1ad1a85n%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/cython-users/6d4e968a-4998-46e6-9670-9d37252a3d3e%40d-woods.co.uk.
Dear Adam Li,
Please accept my apologies for the delayed response — I was away for a while and couldn’t reply sooner. Thank you for your patience.
Regarding your question: your overall design is reasonable, but there are a few important points about reshaping typed memoryviews in Cython:
Cython memoryviews do not support .reshape() the way NumPy arrays do, so
reshaped_X = self.X.reshape(-1, 4, 3)
will not work.
Reshaping a memoryview never makes a copy, simply because memoryviews cannot be reshaped. If you reshape the array on the NumPy side, NumPy will only create a view if the data is C-contiguous; otherwise, it will copy.
Performance-wise, your approach is fine as long as the reshaping is done in NumPy before passing the array to your extension class, or through a safe manual cast when you’re certain about the memory layout.
In short: reshape the NumPy array first, then pass it as a memoryview to Cython for efficient, zero-copy access.
If you’d like, I can provide a corrected Cython snippet as well.