果冻,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].