ゼリー、25 23 21バイト
AṂ×ṠṚ
LHŒRṗ2Ç€ḅLĠịFS€
オンラインでお試しください!
代替バージョン、19バイト
AṂ×ṠṚ
LHŒRṗ2Ç€ĠịFS€
Ġネストされた配列に対して不適切に動作したため、これは機能しませんでした。唯一の違いは、「仕組み」で説明したペア[q、p]が、ソートの前にp + nqにマッピングするのではなく、辞書式にソートされることです。
オンラインでお試しください!
バックグラウンド
まず、要素を座標に置き換え、左方向および下方向に増やして、マトリックスの中心に(0、0)を配置します。
7x7行列Mの場合、次の座標を取得します。
(-3,-3) (-3,-2) (-3,-1) (-3, 0) (-3, 1) (-3, 2) (-3, 3)
(-2,-3) (-2,-2) (-2,-1) (-2, 0) (-2, 1) (-2, 2) (-2, 3)
(-1,-3) (-1,-2) (-1,-1) (-1, 0) (-1, 1) (-1, 2) (-1, 3)
( 0,-3) ( 0,-2) ( 0,-1) ( 0, 0) ( 0, 1) ( 0, 2) ( 0, 3)
( 1,-3) ( 1,-2) ( 1,-1) ( 1, 0) ( 1, 1) ( 1, 2) ( 1, 3)
( 2,-3) ( 2,-2) ( 2,-1) ( 2, 0) ( 2, 1) ( 2, 2) ( 2, 3)
( 3,-3) ( 3,-2) ( 3,-1) ( 3, 0) ( 3, 1) ( 3, 2) ( 3, 3)
次に、各座標ペアの最小絶対値を計算し、両方の座標の符号に乗算し、(i、j)を(sign(i)m、sign(j)m)にマッピングします。ここで、m = min(| i | 、| j |)。
(-3,-3) (-2,-2) (-1,-1) ( 0, 0) (-1, 1) (-2, 2) (-3, 3)
(-2,-2) (-2,-2) (-1,-1) ( 0, 0) (-1, 1) (-2, 2) (-2, 2)
(-1,-1) (-1,-1) (-1,-1) ( 0, 0) (-1, 1) (-1, 1) (-1, 1)
( 0, 0) ( 0, 0) ( 0, 0) ( 0, 0) ( 0, 0) ( 0, 0) ( 0, 0)
( 1,-1) ( 1,-1) ( 1,-1) ( 0, 0) ( 1, 1) ( 1, 1) ( 1, 1)
( 2,-2) ( 2,-2) ( 1,-1) ( 0, 0) ( 1, 1) ( 2, 2) ( 2, 2)
( 3,-3) ( 2,-2) ( 1,-1) ( 0, 0) ( 1, 1) ( 2, 2) ( 3, 3)
同じペアに対応する行列要素を合計する必要があります。合計の順序を決定するために、各ペア(p、q)をp + nqにマッピングします。ここで、nはMの行/列の数です。
-24 -16 -8 0 6 12 18
-16 -16 -8 0 6 12 12
-8 -8 -8 0 6 6 6
0 0 0 0 0 0 0
-6 -6 -6 0 8 8 8
-12 -12 -6 0 8 16 16
-18 -12 -6 0 8 16 24
合計の順序は、被加算数に対応する整数の順序に対応します。
使い方
LHŒRṗ2Ç€ḅLĠịFS€ Main link. Argument: M (matrix)
L Compute n, the length (number of rows) of M.
H Halve it.
ŒR Symmetric range; map t to [-int(t), ..., 0, int(t)].
ṗ2 Compute the second Cartesian power, yielding all pairs [i, j]
with -t ≤ i, j ≤ t.
Ç€ Map the helper link over the resulting array of pairs.
L Yield n.
ḅ Unbase; map each pair [q, p] to (p + nq).
Ġ Group the indices of the resulting array of n² integers by their
corresponding values, ordering the groups by the values.
F Flatten M.
ị Index into the serialized matrix.
S€ Compute the sum of each group.
AṂ×ṠṚ Helper link. Argument: [i, j] (index pair)
A Absolute value; yield [|i|, |j|].
Ṃ Minimum; yield m := min(|i|, |j|).
Ṡ Sign; yield [sign(i), sign(j)].
× Multiply; yield [p, q] := [sign(i)m, sign(j)m].
Ṛ Reverse; yield [q, p].