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
bf817f15
Commit
bf817f15
authored
Feb 09, 2019
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Avoid signed vs. unsigned comparison warning for loop variables in memoryview tutorial.
parent
a8b33b5b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
52 additions
and
52 deletions
+52
-52
docs/examples/userguide/memoryviews/quickstart.pyx
docs/examples/userguide/memoryviews/quickstart.pyx
+52
-52
No files found.
docs/examples/userguide/memoryviews/quickstart.pyx
View file @
bf817f15
from
cython.view
cimport
array
as
cvarray
from
cython.view
cimport
array
as
cvarray
import
numpy
as
np
import
numpy
as
np
# Memoryview on a NumPy array
# Memoryview on a NumPy array
narr
=
np
.
arange
(
27
,
dtype
=
np
.
dtype
(
"i"
)).
reshape
((
3
,
3
,
3
))
narr
=
np
.
arange
(
27
,
dtype
=
np
.
dtype
(
"i"
)).
reshape
((
3
,
3
,
3
))
cdef
int
[:,
:,
:]
narr_view
=
narr
cdef
int
[:,
:,
:]
narr_view
=
narr
# Memoryview on a C array
# Memoryview on a C array
cdef
int
carr
[
3
][
3
][
3
]
cdef
int
carr
[
3
][
3
][
3
]
cdef
int
[:,
:,
:]
carr_view
=
carr
cdef
int
[:,
:,
:]
carr_view
=
carr
# Memoryview on a Cython array
# Memoryview on a Cython array
cyarr
=
cvarray
(
shape
=
(
3
,
3
,
3
),
itemsize
=
sizeof
(
int
),
format
=
"i"
)
cyarr
=
cvarray
(
shape
=
(
3
,
3
,
3
),
itemsize
=
sizeof
(
int
),
format
=
"i"
)
cdef
int
[:,
:,
:]
cyarr_view
=
cyarr
cdef
int
[:,
:,
:]
cyarr_view
=
cyarr
# Show the sum of all the arrays before altering it
# Show the sum of all the arrays before altering it
print
(
"NumPy sum of the NumPy array before assignments: %s"
%
narr
.
sum
())
print
(
"NumPy sum of the NumPy array before assignments: %s"
%
narr
.
sum
())
# We can copy the values from one memoryview into another using a single
# We can copy the values from one memoryview into another using a single
# statement, by either indexing with ... or (NumPy-style) with a colon.
# statement, by either indexing with ... or (NumPy-style) with a colon.
carr_view
[...]
=
narr_view
carr_view
[...]
=
narr_view
cyarr_view
[:]
=
narr_view
cyarr_view
[:]
=
narr_view
# NumPy-style syntax for assigning a single value to all elements.
# NumPy-style syntax for assigning a single value to all elements.
narr_view
[:,
:,
:]
=
3
narr_view
[:,
:,
:]
=
3
# Just to distinguish the arrays
# Just to distinguish the arrays
carr_view
[
0
,
0
,
0
]
=
100
carr_view
[
0
,
0
,
0
]
=
100
cyarr_view
[
0
,
0
,
0
]
=
1000
cyarr_view
[
0
,
0
,
0
]
=
1000
# Assigning into the memoryview on the NumPy array alters the latter
# Assigning into the memoryview on the NumPy array alters the latter
print
(
"NumPy sum of NumPy array after assignments: %s"
%
narr
.
sum
())
print
(
"NumPy sum of NumPy array after assignments: %s"
%
narr
.
sum
())
# A function using a memoryview does not usually need the GIL
# A function using a memoryview does not usually need the GIL
cpdef
int
sum3d
(
int
[:,
:,
:]
arr
)
nogil
:
cpdef
int
sum3d
(
int
[:,
:,
:]
arr
)
nogil
:
cdef
size_t
i
,
j
,
k
cdef
size_t
i
,
j
,
k
,
I
,
J
,
K
cdef
int
total
=
0
cdef
int
total
=
0
I
=
arr
.
shape
[
0
]
I
=
arr
.
shape
[
0
]
J
=
arr
.
shape
[
1
]
J
=
arr
.
shape
[
1
]
K
=
arr
.
shape
[
2
]
K
=
arr
.
shape
[
2
]
for
i
in
range
(
I
):
for
i
in
range
(
I
):
for
j
in
range
(
J
):
for
j
in
range
(
J
):
for
k
in
range
(
K
):
for
k
in
range
(
K
):
total
+=
arr
[
i
,
j
,
k
]
total
+=
arr
[
i
,
j
,
k
]
return
total
return
total
# A function accepting a memoryview knows how to use a NumPy array,
# A function accepting a memoryview knows how to use a NumPy array,
# a C array, a Cython array...
# a C array, a Cython array...
print
(
"Memoryview sum of NumPy array is %s"
%
sum3d
(
narr
))
print
(
"Memoryview sum of NumPy array is %s"
%
sum3d
(
narr
))
print
(
"Memoryview sum of C array is %s"
%
sum3d
(
carr
))
print
(
"Memoryview sum of C array is %s"
%
sum3d
(
carr
))
print
(
"Memoryview sum of Cython array is %s"
%
sum3d
(
cyarr
))
print
(
"Memoryview sum of Cython array is %s"
%
sum3d
(
cyarr
))
# ... and of course, a memoryview.
# ... and of course, a memoryview.
print
(
"Memoryview sum of C memoryview is %s"
%
sum3d
(
carr_view
))
print
(
"Memoryview sum of C memoryview is %s"
%
sum3d
(
carr_view
))
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