Matrix multiplication: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: added/changed whitespace and comments, changed alignments and indentations. |
→{{header|Perl 6}}: modernize |
||
Line 2,545: | Line 2,545: | ||
{{trans|Perl 5}} |
{{trans|Perl 5}} |
||
{{works with|Rakudo| |
{{works with|Rakudo|2015-09-22}} |
||
There are three ways in which this example differs significantly from the original Perl 5 code. These are not esoteric differences; all three of these features typically find heavy use in Perl 6. |
There are three ways in which this example differs significantly from the original Perl 5 code. These are not esoteric differences; all three of these features typically find heavy use in Perl 6. |
||
Line 2,562: | Line 2,562: | ||
<lang perl6>sub mmult(@a,@b) { |
<lang perl6>sub mmult(@a,@b) { |
||
my @p; |
my @p; |
||
for ^@a X ^@b[0] -> $r, $c { |
for ^@a X ^@b[0] -> ($r, $c) { |
||
@p[$r][$c] += @a[$r][$_] * @b[$_][$c] for ^@b; |
@p[$r][$c] += @a[$r][$_] * @b[$_][$c] for ^@b; |
||
} |
} |
||
Line 2,581: | Line 2,581: | ||
{{out}} |
{{out}} |
||
<pre>1 0 0 0 |
<pre>[1 0 0 0] |
||
0 1 0 0 |
[0 1 0 0] |
||
0 0 1 0 |
[0 0 1 0] |
||
0 0 0 1</pre> |
[0 0 0 1]</pre> |
||
Note that these are not rounded values, but exact, since all the math was done in rationals. |
Note that these are not rounded values, but exact, since all the math was done in rationals. |
||
Line 2,592: | Line 2,592: | ||
Some people will find this more readable and elegant, and others will, well, not. |
Some people will find this more readable and elegant, and others will, well, not. |
||
<lang perl6>sub mmult( |
<lang perl6>sub mmult(\a,\b) { |
||
[ |
|||
for ^ |
for ^a -> \r { |
||
[ |
[ |
||
for ^b[0] -> \c { |
|||
[+] a[r;^b] Z* b[^b;c] |
|||
} |
|||
] |
|||
} |
} |
||
] |
] |
||
}</lang> |
}</lang> |
||
Here we use Z with an "op" of <tt>*</tt>, which is a zip with multiply. This, along with the <tt>[+]</tt> reduction operator, replaces the inner loop. We chose to split the outer X loop back into two loops to make it convenient to collect each subarray value in <tt>[...]</tt>. It just collects all the returned values from the inner loop and makes an array of them. The outer loop simply returns the |
Here we use Z with an "op" of <tt>*</tt>, which is a zip with multiply. This, along with the <tt>[+]</tt> reduction operator, replaces the inner loop. We chose to split the outer X loop back into two loops to make it convenient to collect each subarray value in <tt>[...]</tt>. It just collects all the returned values from the inner loop and makes an array of them. The outer loop simply returns the outer array. |
||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |