Commit 369b41c5 authored by Nicolas Hug's avatar Nicolas Hug Committed by GitHub

[DOC] Add doc for memory views with custom numpy dtype (GH-2813)

Closes https://github.com/cython/cython/issues/2760
parent f965f790
import numpy as np
CUSTOM_DTYPE = np.dtype([
('x', np.uint8),
('y', np.float32),
])
a = np.zeros(100, dtype=CUSTOM_DTYPE)
cdef packed struct custom_dtype_struct:
# The struct needs to be packed since by default numpy dtypes aren't
# aligned
unsigned char x
float y
def sum(custom_dtype_struct [:] a):
cdef:
unsigned char sum_x = 0
float sum_y = 0.
for i in range(a.shape[0]):
sum_x += a[i].x
sum_y += a[i].y
return sum_x, sum_y
......@@ -78,6 +78,12 @@ three dimensional buffer into a function that requires a two
dimensional buffer will raise a ``ValueError``.
To use a memory view on a numpy array with a custom dtype, you'll need to
declare an equivalent packed struct that mimics the dtype:
.. literalinclude:: ../../examples/userguide/memoryviews/custom_dtype.pyx
Indexing
--------
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment