Commit dae4c13b authored by Lars Buitinck's avatar Lars Buitinck

make sure memoryview example actually runs (fix typing errors)

parent cfac0913
...@@ -28,20 +28,21 @@ Quickstart ...@@ -28,20 +28,21 @@ Quickstart
:: ::
from cython.view cimport array as cvarray from cython.view cimport array as cvarray
from libc.stdint cimport int32_t
import numpy as np import numpy as np
# Memoryview on a NumPy array # Memoryview on a NumPy array
narr = np.arange(27).reshape((3,3,3)) narr = np.arange(27, dtype=np.int32).reshape((3, 3, 3))
cdef int [:, :, :] narr_view = narr cdef int32_t [:, :, :] narr_view = narr
# Memoryview on a C array # Memoryview on a C array
cdef int carr[3][3][3] cdef int32_t carr[3][3][3]
cdef int [:, :, :] carr_view = carr cdef int32_t [:, :, :] 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(int32_t), format="i")
cdef int [:, :, :] cyarr_view = cyarr cdef int32_t [:, :, :] 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:", narr.sum() print "Numpy sum of the Numpy array before assignments:", narr.sum()
...@@ -63,8 +64,8 @@ Quickstart ...@@ -63,8 +64,8 @@ Quickstart
print "Numpy sum of Numpy array after assignments:", narr.sum() print "Numpy sum of Numpy array after assignments:", 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 int32_t sum3d(int32_t[:, :, :] arr) nogil:
cdef int total = 0 cdef int32_t 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]
...@@ -82,7 +83,7 @@ Quickstart ...@@ -82,7 +83,7 @@ Quickstart
# ... and of course, a memoryview. # ... and of course, a memoryview.
print "Memoryview sum of C memoryview is", sum3d(carr_view) print "Memoryview sum of C memoryview is", sum3d(carr_view)
This code gives output:: This code should give the following output::
Numpy sum of the Numpy array before assignments: 351 Numpy sum of the Numpy array before assignments: 351
Numpy sum of Numpy array after assignments: 81 Numpy sum of Numpy array after assignments: 81
......
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