omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / tasks / matrix-multiplication / oracle.py
| 1 | # Independent reference for "Matrix multiplication" (Python 3, standard library only). |
| 2 | # expected.txt is this program's stdout; omc never produced it. |
| 3 | def matmul(a, b): |
| 4 | # each entry as a dot product of a row of a with a column of b (zip(*b) gives the columns) |
| 5 | return [[sum(x * y for x, y in zip(row, col)) for col in zip(*b)] for row in a] |
| 6 | def show(m): |
| 7 | for row in m: |
| 8 | print("".join(f"{v:5}" for v in row)) |
| 9 | cases = [([[1, 2, 3], [4, 5, 6]], [[7, 8], [9, 10], [11, 12]]), |
| 10 | ([[3 * i - 10 for i in range(r * 3, r * 3 + 3)] for r in range(3)], |
| 11 | [[4 - (5 * i) % 7 for i in range(r * 3, r * 3 + 3)] for r in range(3)])] |
| 12 | for n, (a, b) in enumerate(cases): |
| 13 | if n: |
| 14 | print() |
| 15 | show(a); print("times"); show(b); print("equals"); show(matmul(a, b)) |