Cython: passing multiple numpy arrays in one argument using condensed types

I rewrote the algorithm from C to Cython so that I can use fused types and simplify the call with python. The algorithm can work with several arrays along with some other parameters. Arrays are taken as a pointer to pointers (for example). I figured I would call cython code from python, providing a few arrays as a tuple of numpy arrays, but doing this becomes messy with smooth types. Here is a simple example of how I am working now:

import numpy
cimport numpy

ctypedef fused test_dtype:
    numpy.float32_t
    numpy.float64_t

cdef int do_stuff(test_dtype **some_arrays):
    if test_dtype is numpy.float32_t:
        return 1
    elif test_dtype is numpy.float64_t:
        return 2
    else:
        return -1

def call_do_stuff(tuple some_arrays):
    cdef unsigned int num_items = len(some_arrays)
    cdef void **the_pointer = <void **>malloc(num_items * sizeof(void *))
    if not the_pointer:
        raise MemoryError("Could not allocate memory")
    cdef unsigned int i
    cdef numpy.ndarray[numpy.float32_t, ndim=2] tmp_arr32
    cdef numpy.ndarray[numpy.float64_t, ndim=2] tmp_arr64
    if some_arrays[0].dtype == numpy.float32:
        for i in range(num_items):
            tmp_arr32 = some_arrays[i]
            the_pointer[i] = &tmp_arr32[0, 0]
        return do_stuff(<numpy.float32_t **>the_pointer)
    elif some_arrays[0].dtype == numpy.float64:
        for i in range(num_items):
            tmp_arr64 = some_arrays[i]
            the_pointer[i] = &tmp_arr64[0, 0]
        return do_stuff(<numpy.float64_t **>cols_pointer)
    else:
        raise ValueError("Array data type is unknown")

I understand that I can specify the type in the tuple, but no more complicated than the "object" if I understand it correctly. Does anyone know of a cleaner way to do what I'm trying to do? Any other cython tips are appreciated.

, fill_value , . , test_dtype fill, , C . , numpy.nan numpy.float64(numpy.nan) .

+4
1

Python NumPy 10 ( C, ++, Matlab Fortran 10 ) :

C, ++ Fortran, Cython. , , snipplets . ++ STL ( Boost, ).

API NumPy C. PyArrayObject ( NumPy, C) , . PyArray_TYPE() PyArrayObject *. numpy.float64 NPY_FLOAT64, numpy.float32, NPY_FLOAT32 .. C ++ typedefs, C ++: PyArray_TYPE (x) == NPY_FLOAT64, C ++ - npy_float64. , C ++, NumPy, .

switch PyArray_TYPE (x), NPY_FLOAT64, NPY_FLOAT32 .. ++ . , , .

http://docs.scipy.org/doc/numpy/reference/c-api.html

Cython C ++ API Python C, , . , " " ++, Cython - Python. ++ Cython ++. , Cython - C Python, ++, ++ - , .

, : , , . C ++, Cython, numpy.ndarray - PyArrayObject * dtype. switch.

+5

Source: https://habr.com/ru/post/1569656/


All Articles