Pairwise element operations in a matrix via operation matrices

import numpy as np from operator import iadd, isub, imul, itruediv, imod np.set_printoptions(precision=4, suppress=True) rows, cols = 5, 5 A = np.arange(rows * cols).reshape(rows, cols) """ [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14] [15 16 17 18 19] [20 21 22 23 24]] """ row_ops = np.array([ [iadd, isub, imul, itruediv], [iadd, imul, iadd, iadd], [isub, imod, iadd, isub], [imul, imul, itruediv, imod], [iadd, imul, iadd, isub] ]) """ Idea: Store result of each in-between operation in the first participating element (in-place) The following notation is only to clarify the individual pairwise operations. The results of each operation will not be combined. [[ 0 + 1 - 2 * 3 / 4] [ 5 + 6 * 7 + 8 + 9] [10 - 11 % 12 + 13 - 14] [15 * 16 * 17 / 18 % 19] [20 + 21 * 22 + 23 - 24]] Intended result: [[ 1 -1 6 0.75] [ 11 42 15 17] [-1 11 25 -1] [240 272 0.9444 18] [41 462 45 -1]] Note: The initial matrix changes its shape from (row, col) to (rows, cols-1). """ col_ops = np.array([ [imul, iadd, iadd, isub, imod], [iadd, imul, iadd, isub, itruediv], [imul, iadd, isub, imod, imul], [itruediv, imod, iadd, isub, imul] ]) """ [[ 0 1 2 3 4] * + + - % [ 5 6 7 8 9] + * + - / [10 11 12 13 14] * + - % * [15 16 17 18 19] / % + - * [20 21 22 23 24]] Intended result: [[ 0 7 9 -5 4] [ 15 66 19 -5 0.6429] [150 27 -5 13 266] [0.75 16 39 -5 456]] Note: The initial matrix changes its shape from (row, col) to (rows-1, cols). """ # Store results of row and col operations in matrices Arow = np.zeros((rows, cols-1), dtype=float) # On the last column no operation can be performed Acol = np.zeros((rows-1, cols), dtype=float) # On the last row no operation can be performed for idx in range(len(A.ravel())): i, j = divmod(idx, cols) if j + 1 < cols: Arow[i, j] = row_ops[i, j](A[i, j], A[i, j+1]) if i + 1 < rows: Acol[i, j] = col_ops[i, j](A[i, j], A[i+1, j]) print(Arow) # Arow is now equal to our intended result print(Acol) # Acol is now equal to our intended result