Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cython
Commits
369b41c5
Commit
369b41c5
authored
Dec 06, 2021
by
Nicolas Hug
Committed by
GitHub
Dec 06, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[DOC] Add doc for memory views with custom numpy dtype (GH-2813)
Closes
https://github.com/cython/cython/issues/2760
parent
f965f790
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
32 additions
and
0 deletions
+32
-0
docs/examples/userguide/memoryviews/custom_dtype.pyx
docs/examples/userguide/memoryviews/custom_dtype.pyx
+26
-0
docs/src/userguide/memoryviews.rst
docs/src/userguide/memoryviews.rst
+6
-0
No files found.
docs/examples/userguide/memoryviews/custom_dtype.pyx
0 → 100644
View file @
369b41c5
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
docs/src/userguide/memoryviews.rst
View file @
369b41c5
...
...
@@ -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
--------
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment