normalized_bimodal_kernel_2D , . .
centerlizing_2D , , UB, . stride_tricks, , , numpy.
def centerlizing_2D_opt(U,a,dx):
nx,ny=U.shape
x,y = np.meshgrid(np.arange(-nx//2, nx+nx//2, dx),
np.arange(-nx//2, ny+ny//2, dx),
sparse=True)
k = normalized_bimodal_kernel_2D(x, y, a, x0=nx//2, y0=ny//2)
sx, sy = k.strides
UB = as_strided(k, shape=(nx, ny, nx*2, ny*2), strides=(sy, sx, sx, sy))
return UB[:, :, nx:0:-1, ny:0:-1]
assert np.allclose(centerlizing_2D(U,10,1), centerlizing_2D_opt(U,10,1))
, :
%timeit centerlizing_2D(U,10,1)
%timeit centerlizing_2D_opt(U,10,1)
RC_2D, centerlizing_2D:
def RC_2D_opt(U,a,dx):
UB_tmp = centerlizing_2D_opt(U, a, dx)
U_tmp = U[:, :, None, None] - U[None, None, :, :]
UB = np.sum(U_tmp * UB_tmp, axis=(0, 1))
return UB
assert np.allclose(RC_2D(U,10,1), RC_2D_opt(U,10,1))
%timeit RC_2D(U,10, 1):