Horizontal, vertical and diagonal difference

import numpy as np def horizontal_difference(a): return (1/4.) * np.array([ [(a[0,0] + a[0,1]) - (a[1,0] + a[1,1]), (a[0,2] + a[0,3]) - (a[1,2] + a[1,3])], [(a[2,0] + a[2,1]) - (a[3,0] + a[3,1]), (a[2,2] + a[2,3]) - (a[3,2] + a[3,3])] ]) def vertical_difference(a): return (1/4.) * np.array([ [(a[0,0] + a[1,0]) - (a[0,1] + a[1,1]), (a[0,2] + a[1,2]) - (a[0,3] + a[1,3])], [(a[2,0] + a[3,0]) - (a[2,1] + a[3,1]), (a[2,2] + a[3,2]) - (a[2,3] + a[3,3])] ]) def diagonal_difference(a): return (1/4.) * np.array([ [(a[0,0] + a[1,1]) - (a[0,1] + a[1,0]), (a[0,2] + a[1,3]) - (a[1,2] + a[0,3])], [(a[2,0] + a[3,1]) - (a[2,1] + a[3,1]), (a[2,2] + a[3,3]) - (a[3,2] + a[2,3])] ]) A = np.array([ [5,7,3,9], [6,3,8,5], [5,7,2,6], [0,8,9,2] ]) print(horizontal_difference(A)) print(vertical_difference(A)) print(diagonal_difference(A)) """ [[ 0.75 -0.25] [ 1. -0.75]] [[ 0.25 -0.75] [-2.5 0.75]] [[-1.25 -2.25] [-0.5 -2.75]] """