Project DelphiTensors Workshop

A 1 costs one multiplication

Read the product off the tensor one cell at a time and you multiply once for every 1 and never for a 0:

for a, b, c in np.argwhere(T): # only the cells that hold a 1 C[c] += flat_a[a] * flat_b[b]

For n × n matrices the tensor has n³ ones, which is why the schoolbook rule costs n³ multiplications: 27 for 3 × 3 and 125 for 5 × 5. The cost does not live in the size of the tensor — the 3 × 3 tensor has 729 cells, and 702 of them cost nothing.

But “one multiplication per 1” was a choice. The loop took the tensor apart into pieces that are single cells. Nothing says the pieces must be cells.