|Just another Perl shrine|
Kronecker Productby choroba (Archbishop)
|on Jun 21, 2022 at 05:45 UTC||Need Help??|
choroba has asked for the wisdom of the Perl Monks concerning the following question:
Spoiler alert: If you participate in The Weekly Challenge, don't read further if you haven't solved week 170 yet.
Kronecker product is a matrix operation that uses elements of one matrix to multiply the second matrix. Mohammad shows this example:
Wait, loops? The whole point of PDL is to hide loops. And indeed, my solution doesn't involve them. Also, a benchmark shows my solution is more than twice faster than the Rosetta Code one.
I'm far from an expert on PDL or matrices in general. But it seems we can easily multiply each element in a matrix by the same number, but it's not so easy to multiply them by different numbers. But we can multiply each element by a matrix, so we just need to "inflate" the matrix, so instead of
That's what dummy does (two dummies, in fact, one in each dimension). Multiplying this with the second matrix gives us a result that has all the expected numbers, but a bit different dimensions.
Fortunately, PDL has all the needed methods to reshuffle the submatrices into the expected result.
It seems correct (it passes the test shown in the challenge and in the Wikipedia page), but my PDL is not so strong. Maybe it can be further improved?