I have to question the use of 'elegant' here.
Matrix multiplication has a similarly short representation in J. But the
actual code: it breaks the operation into several layers of loops: one
sized to the registers, one sized to D1$, one sized to D2$.
Simply following the definition of the operation is very slow. J is a
description of what you want done, not necessarily how to do it.
Henry Rich
On 7/12/2022 11:09 PM, Thomas McGuire wrote:
Devon had shared a kx example of performing matrix multiplication backing out
the loops from a wikipedia pseudocode. Providing an elegant solution in K.
You can see it in the NYCJUG notes here:
https://code.jsoftware.com/wiki/NYCJUG/2022-07-12
<https://code.jsoftware.com/wiki/NYCJUG/2022-07-12>
The Kx solution is as follows:
matmul: {x{+/x*y}/:\:+y}
The each right (/:) and each left operators (\:) in sequence appear to perform
in a similar way to J’s table operator ‘/‘.
After some messing around with rank I came up with the following J equivalent:
matmul =: {{x {{+/x*y}}”1 1/ |:y}}
(1+i. 4 3) matmul 1+i. 3 4
38 44 50 56
83 98 113 128
128 152 176 200
173 206 239 272
(1+i. 4 3) +/ . * 1+i. 3 4
38 44 50 56
83 98 113 128
128 152 176 200
173 206 239 272
By transposing the y operand I deliver columns of y to rows of x which gets
operated on by the inner anonymous dfns. Wondering if there is a more elegant
way that does not play around with rank?
Tom McGuire
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
--
This email has been checked for viruses by AVG.
https://www.avg.com
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm