Task

Multiply two matrices together.

They can be of any dimensions, so long as the number of columns of the first matrix is equal to the number of rows of the second matrix.




 
operator (A matmul B)
  $A -> \[i](
    $B(1) -> \[j](@: 0;
      1..$B::length -> @: $@ + $A($i;$) * $B($;$j);
      $@ !\) !
  \) !
end matmul
 
templates printMatrix&{w:}
  templates formatN
    @: [];
    $ -> #
    '$@ -> $::length~..$w -> ' ';$@(last..1:-1)...;' !
    when <1..> do ..|@: $ mod 10; $ ~/ 10 -> #
    when <=0?($@ <[](0)>)> do ..|@: 0;
  end formatN
  $... -> '|$(1) -> formatN;$(2..last)... -> ', $ -> formatN;';|
' !
end printMatrix
 
def a: [[1, 2, 3], [4, 5, 6]];
'a:
' -> !OUT::write
$a -> printMatrix&{w:2} -> !OUT::write
 
def b: [[0, 1], [2, 3], [4, 5]];
'
b:
' -> !OUT::write
$b -> printMatrix&{w:2} -> !OUT::write
'
axb:
' -> !OUT::write
($a matmul $b) -> printMatrix&{w:2} -> !OUT::write
 