diff options
Diffstat (limited to 'docs/examples/userguide/memoryviews')
-rw-r--r-- | docs/examples/userguide/memoryviews/add_one.pyx | 24 | ||||
-rw-r--r-- | docs/examples/userguide/memoryviews/copy.pyx | 24 | ||||
-rw-r--r-- | docs/examples/userguide/memoryviews/custom_dtype.pyx | 26 | ||||
-rw-r--r-- | docs/examples/userguide/memoryviews/memory_layout.pyx | 22 | ||||
-rw-r--r-- | docs/examples/userguide/memoryviews/memory_layout_2.pyx | 12 | ||||
-rw-r--r-- | docs/examples/userguide/memoryviews/memview_to_c.pyx | 56 | ||||
-rw-r--r-- | docs/examples/userguide/memoryviews/not_none.pyx | 22 | ||||
-rw-r--r-- | docs/examples/userguide/memoryviews/np_flag_const.pyx | 14 | ||||
-rw-r--r-- | docs/examples/userguide/memoryviews/quickstart.pyx | 2 | ||||
-rw-r--r-- | docs/examples/userguide/memoryviews/slicing.pyx | 20 | ||||
-rw-r--r-- | docs/examples/userguide/memoryviews/transpose.pyx | 12 | ||||
-rw-r--r-- | docs/examples/userguide/memoryviews/view_string.pyx | 18 |
12 files changed, 139 insertions, 113 deletions
diff --git a/docs/examples/userguide/memoryviews/add_one.pyx b/docs/examples/userguide/memoryviews/add_one.pyx index cbe65b069..7de7a1274 100644 --- a/docs/examples/userguide/memoryviews/add_one.pyx +++ b/docs/examples/userguide/memoryviews/add_one.pyx @@ -1,12 +1,12 @@ -import numpy as np
-
-def add_one(int[:,:] buf):
- for x in range(buf.shape[0]):
- for y in range(buf.shape[1]):
- buf[x, y] += 1
-
-# exporting_object must be a Python object
-# implementing the buffer interface, e.g. a numpy array.
-exporting_object = np.zeros((10, 20), dtype=np.intc)
-
-add_one(exporting_object)
+import numpy as np + +def add_one(int[:,:] buf): + for x in range(buf.shape[0]): + for y in range(buf.shape[1]): + buf[x, y] += 1 + +# exporting_object must be a Python object +# implementing the buffer interface, e.g. a numpy array. +exporting_object = np.zeros((10, 20), dtype=np.intc) + +add_one(exporting_object) diff --git a/docs/examples/userguide/memoryviews/copy.pyx b/docs/examples/userguide/memoryviews/copy.pyx index 9f000a3b4..9eb1307bf 100644 --- a/docs/examples/userguide/memoryviews/copy.pyx +++ b/docs/examples/userguide/memoryviews/copy.pyx @@ -1,12 +1,12 @@ -import numpy as np
-
-cdef int[:, :, :] to_view, from_view
-to_view = np.empty((20, 15, 30), dtype=np.intc)
-from_view = np.ones((20, 15, 30), dtype=np.intc)
-
-# copy the elements in from_view to to_view
-to_view[...] = from_view
-# or
-to_view[:] = from_view
-# or
-to_view[:, :, :] = from_view
+import numpy as np + +cdef int[:, :, :] to_view, from_view +to_view = np.empty((20, 15, 30), dtype=np.intc) +from_view = np.ones((20, 15, 30), dtype=np.intc) + +# copy the elements in from_view to to_view +to_view[...] = from_view +# or +to_view[:] = from_view +# or +to_view[:, :, :] = from_view diff --git a/docs/examples/userguide/memoryviews/custom_dtype.pyx b/docs/examples/userguide/memoryviews/custom_dtype.pyx new file mode 100644 index 000000000..d54d7bbc4 --- /dev/null +++ b/docs/examples/userguide/memoryviews/custom_dtype.pyx @@ -0,0 +1,26 @@ +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 diff --git a/docs/examples/userguide/memoryviews/memory_layout.pyx b/docs/examples/userguide/memoryviews/memory_layout.pyx index 5c2818dc0..8f9d8a23c 100644 --- a/docs/examples/userguide/memoryviews/memory_layout.pyx +++ b/docs/examples/userguide/memoryviews/memory_layout.pyx @@ -1,11 +1,11 @@ -from cython cimport view
-
-# direct access in both dimensions, strided in the first dimension, contiguous in the last
-cdef int[:, ::view.contiguous] a
-
-# contiguous list of pointers to contiguous lists of ints
-cdef int[::view.indirect_contiguous, ::1] b
-
-# direct or indirect in the first dimension, direct in the second dimension
-# strided in both dimensions
-cdef int[::view.generic, :] c
+from cython cimport view + +# direct access in both dimensions, strided in the first dimension, contiguous in the last +cdef int[:, ::view.contiguous] a + +# contiguous list of pointers to contiguous lists of ints +cdef int[::view.indirect_contiguous, ::1] b + +# direct or indirect in the first dimension, direct in the second dimension +# strided in both dimensions +cdef int[::view.generic, :] c diff --git a/docs/examples/userguide/memoryviews/memory_layout_2.pyx b/docs/examples/userguide/memoryviews/memory_layout_2.pyx index 1cb039dd4..71d2cceb2 100644 --- a/docs/examples/userguide/memoryviews/memory_layout_2.pyx +++ b/docs/examples/userguide/memoryviews/memory_layout_2.pyx @@ -1,6 +1,6 @@ -from cython cimport view
-
-# VALID
-cdef int[::view.indirect, ::1, :] a
-cdef int[::view.indirect, :, ::1] b
-cdef int[::view.indirect_contiguous, ::1, :] c
+from cython cimport view + +# VALID +cdef int[::view.indirect, ::1, :] a +cdef int[::view.indirect, :, ::1] b +cdef int[::view.indirect_contiguous, ::1, :] c diff --git a/docs/examples/userguide/memoryviews/memview_to_c.pyx b/docs/examples/userguide/memoryviews/memview_to_c.pyx index c5abc19ac..ad6190cc7 100644 --- a/docs/examples/userguide/memoryviews/memview_to_c.pyx +++ b/docs/examples/userguide/memoryviews/memview_to_c.pyx @@ -1,28 +1,28 @@ -cdef extern from "C_func_file.c":
- # C is include here so that it doesn't need to be compiled externally
- pass
-
-cdef extern from "C_func_file.h":
- void multiply_by_10_in_C(double *, unsigned int)
-
-import numpy as np
-
-def multiply_by_10(arr): # 'arr' is a one-dimensional numpy array
-
- if not arr.flags['C_CONTIGUOUS']:
- arr = np.ascontiguousarray(arr) # Makes a contiguous copy of the numpy array.
-
- cdef double[::1] arr_memview = arr
-
- multiply_by_10_in_C(&arr_memview[0], arr_memview.shape[0])
-
- return arr
-
-
-a = np.ones(5, dtype=np.double)
-print(multiply_by_10(a))
-
-b = np.ones(10, dtype=np.double)
-b = b[::2] # b is not contiguous.
-
-print(multiply_by_10(b)) # but our function still works as expected.
+cdef extern from "C_func_file.c": + # C is include here so that it doesn't need to be compiled externally + pass + +cdef extern from "C_func_file.h": + void multiply_by_10_in_C(double *, unsigned int) + +import numpy as np + +def multiply_by_10(arr): # 'arr' is a one-dimensional numpy array + + if not arr.flags['C_CONTIGUOUS']: + arr = np.ascontiguousarray(arr) # Makes a contiguous copy of the numpy array. + + cdef double[::1] arr_memview = arr + + multiply_by_10_in_C(&arr_memview[0], arr_memview.shape[0]) + + return arr + + +a = np.ones(5, dtype=np.double) +print(multiply_by_10(a)) + +b = np.ones(10, dtype=np.double) +b = b[::2] # b is not contiguous. + +print(multiply_by_10(b)) # but our function still works as expected. diff --git a/docs/examples/userguide/memoryviews/not_none.pyx b/docs/examples/userguide/memoryviews/not_none.pyx index ae3b6c936..f6c0fed8a 100644 --- a/docs/examples/userguide/memoryviews/not_none.pyx +++ b/docs/examples/userguide/memoryviews/not_none.pyx @@ -1,11 +1,11 @@ -import numpy as np
-
-def process_buffer(int[:,:] input_view not None,
- int[:,:] output_view=None):
-
- if output_view is None:
- # Creating a default view, e.g.
- output_view = np.empty_like(input_view)
-
- # process 'input_view' into 'output_view'
- return output_view
+import numpy as np + +def process_buffer(int[:,:] input_view not None, + int[:,:] output_view=None): + + if output_view is None: + # Creating a default view, e.g. + output_view = np.empty_like(input_view) + + # process 'input_view' into 'output_view' + return output_view diff --git a/docs/examples/userguide/memoryviews/np_flag_const.pyx b/docs/examples/userguide/memoryviews/np_flag_const.pyx index 03f0ea4a3..54eb3d338 100644 --- a/docs/examples/userguide/memoryviews/np_flag_const.pyx +++ b/docs/examples/userguide/memoryviews/np_flag_const.pyx @@ -1,7 +1,7 @@ -import numpy as np
-
-cdef const double[:] myslice # const item type => read-only view
-
-a = np.linspace(0, 10, num=50)
-a.setflags(write=False)
-myslice = a
+import numpy as np + +cdef const double[:] myslice # const item type => read-only view + +a = np.linspace(0, 10, num=50) +a.setflags(write=False) +myslice = a diff --git a/docs/examples/userguide/memoryviews/quickstart.pyx b/docs/examples/userguide/memoryviews/quickstart.pyx index 58335c0cf..a313859d9 100644 --- a/docs/examples/userguide/memoryviews/quickstart.pyx +++ b/docs/examples/userguide/memoryviews/quickstart.pyx @@ -6,7 +6,7 @@ narr = np.arange(27, dtype=np.dtype("i")).reshape((3, 3, 3)) cdef int [:, :, :] narr_view = narr # Memoryview on a C array -cdef int carr[3][3][3] +cdef int[3][3][3] carr cdef int [:, :, :] carr_view = carr # Memoryview on a Cython array diff --git a/docs/examples/userguide/memoryviews/slicing.pyx b/docs/examples/userguide/memoryviews/slicing.pyx index a6134aae2..d7bd896e6 100644 --- a/docs/examples/userguide/memoryviews/slicing.pyx +++ b/docs/examples/userguide/memoryviews/slicing.pyx @@ -1,10 +1,10 @@ -import numpy as np
-
-exporting_object = np.arange(0, 15 * 10 * 20, dtype=np.intc).reshape((15, 10, 20))
-
-cdef int[:, :, :] my_view = exporting_object
-
-# These are all equivalent
-my_view[10]
-my_view[10, :, :]
-my_view[10, ...]
+import numpy as np + +exporting_object = np.arange(0, 15 * 10 * 20, dtype=np.intc).reshape((15, 10, 20)) + +cdef int[:, :, :] my_view = exporting_object + +# These are all equivalent +my_view[10] +my_view[10, :, :] +my_view[10, ...] diff --git a/docs/examples/userguide/memoryviews/transpose.pyx b/docs/examples/userguide/memoryviews/transpose.pyx index 7611529c2..8a53f7140 100644 --- a/docs/examples/userguide/memoryviews/transpose.pyx +++ b/docs/examples/userguide/memoryviews/transpose.pyx @@ -1,6 +1,6 @@ -import numpy as np
-
-array = np.arange(20, dtype=np.intc).reshape((2, 10))
-
-cdef int[:, ::1] c_contig = array
-cdef int[::1, :] f_contig = c_contig.T
+import numpy as np + +array = np.arange(20, dtype=np.intc).reshape((2, 10)) + +cdef int[:, ::1] c_contig = array +cdef int[::1, :] f_contig = c_contig.T diff --git a/docs/examples/userguide/memoryviews/view_string.pyx b/docs/examples/userguide/memoryviews/view_string.pyx index 7aace3ea5..9fdeae053 100644 --- a/docs/examples/userguide/memoryviews/view_string.pyx +++ b/docs/examples/userguide/memoryviews/view_string.pyx @@ -1,9 +1,9 @@ -cdef bint is_y_in(const unsigned char[:] string_view):
- cdef int i
- for i in range(string_view.shape[0]):
- if string_view[i] == b'y':
- return True
- return False
-
-print(is_y_in(b'hello world')) # False
-print(is_y_in(b'hello Cython')) # True
+cdef bint is_y_in(const unsigned char[:] string_view): + cdef int i + for i in range(string_view.shape[0]): + if string_view[i] == b'y': + return True + return False + +print(is_y_in(b'hello world')) # False +print(is_y_in(b'hello Cython')) # True |