import numpy as np
from scipy import signal
s1 = rho[1:nx - 1, 2:nz - 3]
s2 = rho[0:nx - 2, 2:nz - 3]
kernel = c[:, (None)][::-1]
a = np.zeros((nx, nz))
b = np.zeros((nx, nz))
a[2:nx - 3, 2:nz - 3] = signal.convolve2d(s1, kernel, boundary='symm', mode
    ='valid')
b[2:nx - 3, 2:nz - 3] = signal.convolve2d(s2, kernel, boundary='symm', mode
    ='valid')
Оптимизировать двойной цикл в python