This can be done with the newest version of Numba (0.27) and numpy stride_tricks
. You need to be careful with this and it's a bit ugly. Read the docstring for as_strided
to make sure you understand what's going on since this isn't "safe" since it doesn't check the shape or the strides.
import numpy as np
import numba as nb
a = np.random.randn(20, 10)
b = np.random.randn(20)
c = np.random.randn(10)
def toto(a, b, c):
d = a - b[:, np.newaxis] * c[np.newaxis, :]
return d
@nb.jit(nopython=True)
def toto2(a, b, c):
_b = np.lib.stride_tricks.as_strided(b, shape=(b.shape[0], 1), strides=(b.strides[0], 0))
_c = np.lib.stride_tricks.as_strided(c, shape=(1, c.shape[0]), strides=(0, c.strides[0]))
d = a - _b * _c
return d
x = toto(a,b,c)
y = toto2(a,b,c)
print np.allclose(x, y) # True
reshape() supports contiguous array only
. And of course,toto()
is an example not my actual function – Motch