Hi. I’ve been struggling to come up with a clean solution for a problem I have and was hoping someone could give me some advice, I’ve looked around for some possible implementations (like jitting
functools.partial functions) but none seem to work.
I have defined a structure that is like a list of arrays but is actually two 1D arrays (kind of like sparse matrices but without the indices, just data and indptr) and a numba jitted function that takes the two arrays (data and indptr), a function and the function arguments (this arguments part is what I’m trying to remove) and applies it to every “element” of that structure. Here’s a snippet:
@njit def apply_func_to_groups(data, indptr, func, *args): ngroups = len(indptr) - 1 result = np.empty_like(data) for i in range(ngroups): group_slice = slice(indptr[i], indptr[i+1]) result[group_slice] = func(data[group_slice], *args) return result
The drawback here is that this gets compiled for functions that take no arguments, functions that take 1 argument, 2 arguments, etc. I’d like to be able to just pass a function with already set default arguments (like what
functools.partial does). Is there a way to achieve it?