相当于Python中APL的编码/解码



我很好奇是否有人回忆起APL中的解码((函数?在n维数组中的维度和展平的等价维度之间进行转换是有用的。

例如,如果我有一个4维数组,我可以使用如下解码来导出扁平文件中的位置:

A = 4 x 5 x 2 x 11 is a 4-d array
B = 440 element flattened version of A
4 5 2 11 decode 1 2 1 3 = 110 + 44 + 11 + 3 = 168th element of B

我记得我必须根据指数根加上±1。

我应该注意。。。解码相当容易。实际上,对我有用的是encode((等价物(从平面文件索引移回数组索引(。那么如何从第168个元素移回[1,2,1,3]

虽然我不知道Python,但我知道APL,基于这些知识,我认为这些方法会奏效:

从数组索引转换为平面索引

arrayShape = [4, 5, 2, 11]
strides = [5×2×11, 2×11, 11, 1] = [110, 22, 11, 1] 
arrayIndex = [1, 2, 1, 3]
flatIndex = ∑[1×110, 2×22, 1×11, 3×1] = 168

从平面索引转换为数组索引

arrayShape = [4, 5, 2, 11]
strides = [5×2×11, 2×11, 11, 1] = [110, 22, 11, 1] 
flatIndex = 168
How many times can we subtract 110? 1, using 110 with remainder 58
How many times can we subtract  22? 2, using  44 with remainder 14
How many times can we subtract  11? 1, using  11 with remainder 3
How many times can we subtract   1? 3, using   3 with no remainder
arrayIndex = [1, 2, 1, 3]

最新更新