2 ms·
The PEP claims that: > Matrix multiplication is more of a special case. It's only defined on 2d arrays (also known as "matrices") but this is not true. Matri
by JadeNB 13y ago
The PEP claims that:
> Matrix multiplication is more of a special case. It's only defined on 2d arrays (also known as "matrices")
but this is not true. Matrix multiplication is just a special case of contraction of indices in a tensor (https://en.wikipedia.org/wiki/Tensor_contraction)—probably https://en.wikipedia.org/wiki/Tensor_contraction)—probably the most frequently used case, but not the only one. I'm certainly not arguing for the inclusion of general tensor-manipulating operators in Python, but it does seem to suggest a sensible alternative to:
> For inputs with more than 2 dimensions, we treat the last two dimensions as being the dimensions of the matrices to multiply, and 'broadcast' across the other dimensions.
namely, just contract on the inner indices. That is, arr(n1, ..., nk, m) @ arr(m, p1, ... pl) = arr(n1, ..., nk, p1, ..., pl).
EDIT: scythe (https://news.ycombinator.com/item?id=7554013 https://news.ycombinator.com/item?id=7554013) already pointed this out in passing.
- onedognight 13y ago> namely, just contract on the inner indices. That is, arr(n1, ..., nk, m) @ arr(m, p1, ... pl) = arr(n1, ..., nk, p1, ..., pl). No. This is what a mathematician might assume the PEP proposes without actually reading it. It instead proposes an entirely non-obvious definition which not equivalent to what you wrote. In particular consider this example from the PEP. arr(10, 2, 3) @ arr(10, 3, 4) = arr(10, 2, 4)
- JadeNB 13y agoI know it is not equivalent—that is why I proposed it as a sensible alternative to what the PEP proposes (which I quoted). The punctuation may have made it unclear, but what I was trying to say was: > … a sensible alternative to [PEP proposal]; namely, just contract on the inner indices. and not > … a sensible alternative to contracting on the inner indices. My argument for why it's sensible is precisely what you mentioned, namely, that it is what a mathematician would expect.