'How to create Cython functions that accept 32 bit and 64 bit int numpy arrays (templates?)

Say I want to define the following cython function

import numpy as np
cimport numpy as cnp
def manipulate_arrays(cnp.int_t [::1] arr1, cnp.int_t[::1] arr2, cnp.int_t[::1] arr3):
    # do something with the arrays
    return None

The arrays arr1, arr2 and arr3 come from Python and they can be of either int32 or int64, depending on what numpy functions decide is best to create them.

Examples where this issue my occur:

  • Most times when creating a numpy array with dtype=int or dtype=np.int_, we get longs.
  • Based on this SO answer, cnp.int_t is a long, which I never know if it's a 32 bit or 64 bit integer (it's OS dependent?)
  • np.argsort returns 64 bit integers in a test that I made, i.e. a long long
  • Sometimes indptr and indices arrays of scipy.sparse matrices can be int32 or int64

I don't want to pollute the python code by specifying .astype(np.int64) in everything.

The question is then how to make generic numpy arrays of int's acceptable to cython functions.

From what I have understood, this is called template programming and in Cython this is made using fused types.

For this particular example I would have to do:

import numpy as np
cimport numpy as cnp
ctypedef fused A:
    cnp.int32_t
    cnp.int64_t
ctypedef fused B:
    cnp.int32_t
    cnp.int64_t
ctypedef fused C:
    cnp.int32
    cnp.int64
def manipulate_arrays(A[::1] arr0, B[::1] arr1, C[::1] arr2):
    # do something with the arrays
    return None

But the issue with this is that if the function has n different arrays, will I have to create by hand n different fused types? According to the documentation:

Note that in Cython 0.20.x and earlier, the compiler generated the full cross product of all type combinations when a fused type was used by more than one memory view in a type signature, e.g.

I actually WANT the full cross product. Is this just a bad design choice by me?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source