M
Modular12mo ago
Jona

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.
4 Replies
benny
benny12mo ago
if you are looking for pure efficiency, although it isn’t fun, you could hard code all of these in you could also flatten this to a one dimensional list and just loop over row+col for index otherwise you’d have to implement a custom struct, there are some examples you can find on github, and even some on the mojo docs specifically for matrices (see the example of matmul)
ModularBot
ModularBot12mo ago
Congrats @benny, you just advanced to level 1!
Jona
JonaOP12mo ago
Alright, I guess I'll do that, thank you. Just wanted to check if there was some "proper" way to do this that I was missing
benny
benny12mo ago
Modular Docs - Matrix multiplication in Mojo
Learn how to leverage Mojo's various functions to write a high-performance matmul.
Want results from more Discord servers?
Add your server