Given the following minimal reproducible example:
import numpy as np
from numba import jit
# variable number of dimensions
n_t = 8
# q is just a partition of n
q_ddl = 2
n_ddl = 3
np.random.seed(42)
df = np.random.rand(q_ddl*n_t,q_ddl*n_t)
# index array
# ddl_nl is a set of np.arange(n_ddl), ex: [0,1] ; [0,2] or even [0] ...
ddl_nl = np.array([0,1])
ij = np.asarray(np.meshgrid(ddl_nl,ddl_nl,indexing='ij'))
@jit(nopython=True)
def foo(df,ij):
out = np.zeros((n_t,n_ddl,n_ddl))
for i in range(0,n_t):
d_i = np.zeros((n_ddl,n_ddl))
# (q_ddl,q_ddl) non zero values into (n_ddl,n_ddl) shape
d_i[ij[0], ij[1]] = df[i::n_t,i::n_t]
# to check possible solutions
out[i,...] = d_i
return out
out_foo = foo(df,ij)
The function foo is working well when @jit(nopython=True) is disabled but it is throwing the following error when enabled:
TypeError: unsupported array index type array(int64, 2d, C) in UniTuple(array(int64, 2d, C) x 2)
which happened during the broadcasting operation d_i[ij[0], ij[1]] = df[i::n_t,i::n_t]. Then, I did try to flatten the 2d index arrays ij with something like d_i[ij[0].ravel(), ij[1].ravel()] = df[i::n_t,i::n_t].ravel() which gives me the same output but now another error:
NotImplementedError: only one advanced index supported
So I finally tried to dodge this by using a classical 2 nested for loops structure:
tmp = df[i::n_t,i::n_t]
for k,r in enumerate(ddl_nl):
for l,c in enumerate(ddl_nl):
d_i[r,c] = tmp[k,l]
which is working with the decorator enabled and gives the intended result.
But I can't stop thinking if there is any numba-compatible alternatives for this numpy 2d-array broadcasting operation that I am missing here? Any help will be greatly appreciated.