Matrix multiplication: Difference between revisions

add Tailspin solution
m (Tweak Swift solution)
(add Tailspin solution)
Line 4,339:
<pre>299.0 268.0 178.5 311.0
407.0 364.0 114.5 211.0 </pre>
 
=={{header|Tailspin}}==
<lang tailspin>
templates matmul@{B:}
$ -> [i](def r: $;
[1..$B(1)::length -> (def j: $; @: 0;
1..$r::length -> @: $@ + $r($) * $B($;$j);
$@ !)] !
) !
end matmul
 
templates printMatrix@{w:}
templates formatN
@: [];
$ -> #
'$@ -> $::length~..$w -> ' ';$@(-1..1:-1)...;' !
<1..> ..|@: $ mod 10; $ / 10 -> #
<0?($@ <[](0)>)> ..|@: 0;
end formatN
$... -> '|$(1) -> formatN;$(2..-1)... -> ', $ -> 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: $b} -> printMatrix@{w:2} -> !OUT::write
</lang>
{{out}}
<pre>
a:
| 1, 2, 3|
| 4, 5, 6|
 
b:
| 0, 1|
| 2, 3|
| 4, 5|
 
axb:
|16, 22|
|34, 49|
</pre>
 
=={{header|Tcl}}==
Anonymous user