Jona
Jona
MModular
Created by Jona on 12/19/2023 in #questions
Implementing a simple lookup matrix
I have a precomputed (=handwritten) simple matrix that I want to use in my code. I have currently written it as follows (without a type):
let conversion_matrices = [
#LEFT
[[[0,0], [0,1], [0,2], [0,3]],
[[1,0], [1,1], [1,2], [1,3]],
[[2,0], [2,1], [2,2], [2,3]],
[[3,0], [3,1], [3,2], [3,3]]],
#RIGHT
[[[0,3], [0,2], [0,1], [0,0]],
[[1,3], [1,2], [1,1], [1,0]],
[[2,3], [2,2], [2,1], [2,0]],
[[3,3], [3,2], [3,1], [3,0]]],
#UP
[[[3,0], [2,0], [1,0], [0,0]],
[[3,1], [2,1], [1,1], [0,1]],
[[3,2], [2,2], [1,2], [0,2]],
[[3,3], [2,3], [1,3], [0,3]]],
#DOWN
[[[0,0], [1,0], [2,0], [3,0]],
[[0,1], [1,1], [2,1], [3,1]],
[[0,2], [1,2], [2,2], [3,2]],
[[0,3], [1,3], [2,3], [3,3]]]
]
let conversion_matrices = [
#LEFT
[[[0,0], [0,1], [0,2], [0,3]],
[[1,0], [1,1], [1,2], [1,3]],
[[2,0], [2,1], [2,2], [2,3]],
[[3,0], [3,1], [3,2], [3,3]]],
#RIGHT
[[[0,3], [0,2], [0,1], [0,0]],
[[1,3], [1,2], [1,1], [1,0]],
[[2,3], [2,2], [2,1], [2,0]],
[[3,3], [3,2], [3,1], [3,0]]],
#UP
[[[3,0], [2,0], [1,0], [0,0]],
[[3,1], [2,1], [1,1], [0,1]],
[[3,2], [2,2], [1,2], [0,2]],
[[3,3], [2,3], [1,3], [0,3]]],
#DOWN
[[[0,0], [1,0], [2,0], [3,0]],
[[0,1], [1,1], [2,1], [3,1]],
[[0,2], [1,2], [2,2], [3,2]],
[[0,3], [1,3], [2,3], [3,3]]]
]
The envisioned example use of this would be something like this:
let conversion_matrix = conversion_matrices[direction]
for row in range(4):
for column in range(4):
let something_x = conversion_matrix[row][column][0]
let something_y = conversion_matrix[row][column][1]
let conversion_matrix = conversion_matrices[direction]
for row in range(4):
for column in range(4):
let something_x = conversion_matrix[row][column][0]
let something_y = conversion_matrix[row][column][1]
The original conversion_matrices becomes a ListLiteral, which seems clearly unusable for this usecase given its lack of getitem implementation. I believe using a StaticTuple should probably work, but would require explicitly initializing the tuple for each nested tuple, explicitly listing the nested types repeatedly for every individual tuple. This is probably what I will fall back on if I find nothing else, but I wonder if there is a proper way to do this that doesn't lead to huge chunks of nested and repeated type definitions. I don't have a lot of experience writing python code, so this might not even be a mojo specific question. In any case, I would be very thankful for a pointer in the right direction.
7 replies