diff options
author | gabrieldemarmiesse <gabriel.demarmiesse@teraki.com> | 2018-05-26 14:41:16 +0200 |
---|---|---|
committer | gabrieldemarmiesse <gabriel.demarmiesse@teraki.com> | 2018-05-27 14:14:29 +0200 |
commit | f35173fba8614b83d26ce11c9a34af5fc3b3bc71 (patch) | |
tree | 7de1a76a7bcd7eed5aed8ccfc00c8bf39d2e6c4d /docs/examples/userguide/numpy_tutorial/convolve_memview.pyx | |
parent | 45df48301b0ab5dd35e9d68581b57f15b69dd74f (diff) | |
download | cython-f35173fba8614b83d26ce11c9a34af5fc3b3bc71.tar.gz |
Now all the files in the example directory are tested.
Diffstat (limited to 'docs/examples/userguide/numpy_tutorial/convolve_memview.pyx')
-rw-r--r-- | docs/examples/userguide/numpy_tutorial/convolve_memview.pyx | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/docs/examples/userguide/numpy_tutorial/convolve_memview.pyx b/docs/examples/userguide/numpy_tutorial/convolve_memview.pyx new file mode 100644 index 000000000..b23ebc8ba --- /dev/null +++ b/docs/examples/userguide/numpy_tutorial/convolve_memview.pyx @@ -0,0 +1,39 @@ +import numpy as np
+
+DTYPE = np.intc
+
+# It is possible to declare types in the function declaration.
+def naive_convolve(int [:,:] f, int [:,:] g):
+ if g.shape[0] % 2 != 1 or g.shape[1] % 2 != 1:
+ raise ValueError("Only odd dimensions on filter supported")
+
+ # We don't need to check for the type of NumPy array here because
+ # a check is already performed when calling the function.
+ cdef Py_ssize_t x, y, s, t, v, w, s_from, s_to, t_from, t_to
+ cdef Py_ssize_t vmax = f.shape[0]
+ cdef Py_ssize_t wmax = f.shape[1]
+ cdef Py_ssize_t smax = g.shape[0]
+ cdef Py_ssize_t tmax = g.shape[1]
+ 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 = np.zeros([xmax, ymax], dtype=DTYPE)
+ cdef int [:,:] h = h_np
+
+ cdef int value
+ for x in range(xmax):
+ for y in range(ymax):
+ s_from = max(smid - x, -smid)
+ s_to = min((xmax - x) - smid, smid + 1)
+ t_from = max(tmid - y, -tmid)
+ t_to = min((ymax - y) - tmid, tmid + 1)
+ value = 0
+ for s in range(s_from, s_to):
+ for t in range(t_from, t_to):
+ v = x - smid + s
+ w = y - tmid + t
+ value += g[smid - s, tmid - t] * f[v, w]
+ h[x, y] = value
+ return h_np
\ No newline at end of file |