Background
The deltas of an array of integers is the array formed by getting the differences of consecutive elements. For example, [1, 2, 4, 7, 3, 9, 6]
has the following deltas: [1, 2, 3, -4, 6, -3]
.
We will now define the deltas of a matrix of integers as the deltas of each row and each column it contains.
As an example:
Row deltas:
1 2 3 4 │ => [1, 1, 1]
4 5 6 7 │ => [1, 1, 1]
7 1 8 2 │ => [-6, 7, -6]
Column deltas (the matrix' columns have been rotated into rows for simplicity):
1 4 7 │ => [3, 3]
2 5 1 │ => [3, -4]
3 6 8 │ => [3, 2]
4 7 2 │ => [3, -5]
Which gives us the following list of matrix deltas:
[[1, 1, 1], [1, 1, 1], [-6, 7, -6], [3, 3], [3, -4], [3, 2], [3, -5]]
And as we don't want them to be nested, we flatten that list:
[1, 1, 1, 1, 1, 1, -6, 7, -6, 3, 3, 3, -4, 3, 2, 3, -5]
Task
Your task is to sum all the deltas of a matrix given as input. Note that the matrix will only consist of non-negative integers.
Rules
All standard rules apply.
You may assume the matrix contains at least two values on each row and column, so the minimum size will be 2x2.
You may take the matrix in any reasonable format, as long as you specify it.
You may not assume that the matrix is square.
If it might help you reduce your byte count, you may optionally take the number of rows and the number of columns as input as well (Looking at you C!).
This is code-golf, so the shortest code (in bytes), in each language wins!
Test Cases
Input => Output [[1, 2], [1, 2]] => 2 [[8, 7, 1], [4, 1, 3], [5, 5, 5]] => -9 [[1, 2, 3], [4, 5, 6], [7, 8, 9]] => 24 [[9, 9, 9, 9, 9], [9, 9, 9, 9, 9]] => 0 [[1, 3, 14], [56, 89, 20], [99, 99, 99]] => 256 [[1, 2, 3, 4], [4, 5, 6, 7], [7, 1, 8, 2]] => 9 [[13, 19, 478], [0, 12, 4], [45, 3, 6], [1, 2, 3]] => -72