Well, actually it is very simple with support for smooth types:
This is included in your code.
from cython cimport floating def cythonfloating_memoryview(floating[:, :] array): cdef int i, j for i in range(array.shape[0]): for j in range(array.shape[1]): array[i, j] += 10
Of course, there are many ways to do this:
Name this fuzed.pyx . There is no need to compile or run cython ; It is processed by pyximport . However, do not use pyximport for production code, since usually you only send .c files.
from cython cimport floating from numpy import float32_t, float64_t, ndarray ctypedef fused myfloating: float32_t float64_t def cythonfloating_memoryview(floating[:, :] array):
and here is a little script test:
Name this test.py and run it like a regular Python script:
import pyximport pyximport.install() import fuzed import numpy functions = [ fuzed.cythonfloating_memoryview, fuzed.cythonfloating_buffer, fuzed.myfloating_memoryview, fuzed.myfloating_buffer, ] for function in functions: floats_32 = numpy.zeros((100, 100), "float32") doubles_32 = numpy.zeros((100, 100), "float64") function(floats_32) function(doubles_32) print(repr(floats_32)) print(repr(doubles_32))
It is worth noting that fused types specialize in compilation and are constant for a particular function call. The empty Numpy array you make is always of type double , but you assign it to either a 32-bit float or a 64-bit float. Here is what you should do:
from cython cimport floating import numpy def do_some_things(floating[:] input): cdef floating[:] output if floating is float: output = numpy.empty(10, dtype="float32") elif floating is double: output = numpy.empty(10, dtype="float64") else: raise ValueError("Unknown floating type.") return output
and some tests to prove the point:
import pyximport pyximport.install() #>>> (None, None) import floatingtest import numpy floatingtest.do_some_things(numpy.empty(10, dtype="float32")) #>>> <MemoryView of 'ndarray' at 0x7f0a514b3d88> floatingtest.do_some_things(numpy.empty(10, dtype="float32")).itemsize #>>> 4 floatingtest.do_some_things(numpy.empty(10, dtype="float64")) #>>> <MemoryView of 'ndarray' at 0x7f0a514b3d88> floatingtest.do_some_things(numpy.empty(10, dtype="float64")).itemsize #>>> 8