Commit a171e51a authored by gabrieldemarmiesse's avatar gabrieldemarmiesse

Changed according to the code review.

parent 5e03b2ed
# cython: infer_types=True # cython: infer_types=True
import numpy as np import numpy as np
cimport cython
ctypedef fused my_type: ctypedef fused my_type:
int int
......
...@@ -9,15 +9,15 @@ def naive_convolve(int [:,:] f, int [:,:] g): ...@@ -9,15 +9,15 @@ def naive_convolve(int [:,:] f, int [:,:] g):
# We don't need to check for the type of NumPy array here because # We don't need to check for the type of NumPy array here because
# a check is already performed when calling the function. # a check is already performed when calling the function.
cdef int x, y, s, t, v, w, s_from, s_to, t_from, t_to cdef Py_ssize_t x, y, s, t, v, w, s_from, s_to, t_from, t_to
cdef int vmax = f.shape[0] cdef Py_ssize_t vmax = f.shape[0]
cdef int wmax = f.shape[1] cdef Py_ssize_t wmax = f.shape[1]
cdef int smax = g.shape[0] cdef Py_ssize_t smax = g.shape[0]
cdef int tmax = g.shape[1] cdef Py_ssize_t tmax = g.shape[1]
cdef int smid = smax // 2 cdef Py_ssize_t smid = smax // 2
cdef int tmid = tmax // 2 cdef Py_ssize_t tmid = tmax // 2
cdef int xmax = vmax + 2*smid cdef Py_ssize_t xmax = vmax + 2*smid
cdef int ymax = wmax + 2*tmid cdef Py_ssize_t ymax = wmax + 2*tmid
h_np = np.zeros([xmax, ymax], dtype=DTYPE) h_np = np.zeros([xmax, ymax], dtype=DTYPE)
cdef int [:,:] h = h_np cdef int [:,:] h = h_np
......
...@@ -13,27 +13,24 @@ def naive_convolve(f, g): ...@@ -13,27 +13,24 @@ def naive_convolve(f, g):
# can only be used at the top indentation level (there are non-trivial # can only be used at the top indentation level (there are non-trivial
# problems with allowing them in other places, though we'd love to see # problems with allowing them in other places, though we'd love to see
# good and thought out proposals for it). # good and thought out proposals for it).
#
# For the indices, the "int" type is used. This corresponds to a C int,
# other C types (like "unsigned int") could have been used instead.
# Purists could use "Py_ssize_t" which is the proper Python type for
# array indices.
cdef int x, y, s, t, v, w, s_from, s_to, t_from, t_to
cdef int vmax = f.shape[0] # Py_ssize_t is the proper C type for Python array indices.
cdef int wmax = f.shape[1] cdef Py_ssize_t x, y, s, t, v, w, s_from, s_to, t_from, t_to
cdef int smax = g.shape[0]
cdef int tmax = g.shape[1] cdef Py_ssize_t vmax = f.shape[0]
cdef int smid = smax // 2 cdef Py_ssize_t wmax = f.shape[1]
cdef int tmid = tmax // 2 cdef Py_ssize_t smax = g.shape[0]
cdef int xmax = vmax + 2*smid cdef Py_ssize_t tmax = g.shape[1]
cdef int ymax = wmax + 2*tmid cdef Py_ssize_t smid = smax // 2
cdef Py_ssize_t tmid = tmax // 2
cdef Py_ssize_t xmax = vmax + 2*smid
cdef Py_ssize_t ymax = wmax + 2*tmid
h = np.zeros([xmax, ymax], dtype=DTYPE) h = np.zeros([xmax, ymax], dtype=DTYPE)
# It is very important to type ALL your variables. You do not get any # It is very important to type ALL your variables. You do not get any
# warnings if not, only much slower code (they are implicitly typed as # warnings if not, only much slower code (they are implicitly typed as
# Python objects). # Python objects).
# For the value variable, we want to use the same data type as is # For the value variable, we want to use the same data type as is
# stored in the array, so we use "DTYPE_t" as defined above. # stored in the array, so we use int because it correspond to np.intc.
# NB! An important side-effect of this is that if "value" overflows its # NB! An important side-effect of this is that if "value" overflows its
# datatype size, it will simply wrap around like in C, rather than raise # datatype size, it will simply wrap around like in C, rather than raise
# an error like in Python. # an error like in Python.
......
.. _working-numpy:
======================= =======================
Working with NumPy Working with NumPy
======================= =======================
......
This diff is collapsed.
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