import math
kx = [[-1,0,1],[-2,0,2],[-1,0,1]]
ky = [[-1,-2,-1],[0,0,0],[1,2,1]]
def response(patch, kernel):
    if len(patch)!=3 or any(len(row)!=3 for row in patch):
        raise ValueError('3x3 patch required')
    if not all(math.isfinite(x) for row in patch for x in row):
        raise ValueError('finite values required')
    return sum(patch[r][c]*kernel[r][c] for r in range(3) for c in range(3))
for patch in ([[0,0,10]]*3, [[10,0,0]]*3, [[5,5,5]]*3):
    gx,gy = response(patch,kx),response(patch,ky)
    print('gx gy magnitude',gx,gy,math.hypot(gx,gy))
assert response([[0,0,10]]*3,kx)==40
assert response([[10,0,0]]*3,kx)==-40
assert response([[0,0,10]]*3,ky)==0
assert response([[5,5,5]]*3,kx)==0
