hey i have been trying to write a function which should take an initial integer series and outputs a matrix with different unique permutations according to a specific idea.
I have made a first sketch here:
(
~getMatrix = { |series|
var matrixSize = series.size;
var matrix = Array.fill(matrixSize, {
Array.fill(matrixSize, { 0 } );
});
// Fill first row and column with initial series
matrix.put(0, series);
matrix.do { |row, i|
row.put(0, series[i]);
};
// Calculate differences and update matrix
(1..matrixSize - 1).do { |i|
(1..matrixSize - 1).do { |j|
var rowDif = (matrix[i-1][0] - matrix[i][0]).abs;
matrix[i][j] = (matrix[i-1][j] + rowDif) % matrixSize;
};
};
matrix;
};
~series = [3, 7, 1, 6, 5, 2, 4];
~getMatrix.(~series);
)
The desired output for the input data [3, 7, 1, 6, 5, 2, 4]
should be:
// desired output:
[
[3, 7, 1, 6, 5, 2, 4],
[7, 4, 5, 3, 2, 6, 1],
[1, 5, 6, 4, 3, 7, 2],
[6, 3, 4, 2, 1, 5, 7],
[5, 2, 3, 1, 7, 4, 6],
[2, 6, 7, 5, 4, 1, 3],
[4, 1, 2, 7, 6, 3, 5]
];
I have thought it through already, this is what should happen:
After filling the first row and the first column with the initial series, calculate the absolute differece between [0][0] and [1][0]
(3 - 7).abs; // -> 4
or in other terms:
([0][0] - [1][0]).abs
use this absolute difference and add it to item:
[0][1] % matrixSize and put it into [1][1]
[0][2] % matrixSize and put it into [1][2]
[0][n] % matrixSize and put it into [1][n]
(7 + 4).mod(7) // [1][1]
(1 + 4).mod(7) // [1][2]
(6 + 4).mod(7) // [1][3]
(5 + 4).mod(7) // [1][4]
(2 + 4).mod(7) // [1][5]
(4 + 4).mod(7) // [1][6]
or in other terms:
([0][1] + rowDif) % matrixSize // [1][1]
([0][2] + rowDif) % matrixSize // [1][2]
([0][n] + rowDif) % matrixSize // [1][n]
then calcute the next absolute difference between [1][0] and [2][0] and use this absolute difference and add it to item [1][1] mod(matrixSize) and put it into [2][1]
and so on until all remaining items of the matrix are filled.
Could someone help me to calculate the absolute differences and the updating of the matrix correctly? many thanks
EDIT: i have updated the code, i think the absolute differences are calculated correctly now. but the addition does not work. in general i think there is also a way more elegant way to do this.