#MATL, 17 bytes
XJiq:"!J21ドル!*s1$e
This avoids the matrix multiplication doing it manually, by means of element-wise multiplication with broadcast. Specifically, to multiply matrices A and B, both of size n×ばつn:
- Permute the dimensions of
Bsuch thatBis "turned" with a rotation axis along the first dimension, giving an n×ばつ1_n_ 3D array, sayC. - Multiply each element of
Atimes each element ofC, with implicit broadcast. This means thatAis automatically replicated n times along the third dimension, andCis replicated n times along the second, to make them both n×ばつn×ばつn before the actual element-wise multiplication - Sum along the first dimension to yield a ×ばつn×ばつn array.
- Squeeze the leading singleton dimension out, to produce an n×ばつn result.
Luis Mendo
- 106.7k
- 10
- 139
- 382