Jump to content

Element-wise operations: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(→‎{{header|Groovy}}: Initial solution)
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 1,265:
 
Note that in some branches of mathematics, it has been traditional to define multiplication such that it is not performed element-wise. This can introduce some complications ([[wp:Einstein notation]] is arguably the best approach for resolving those complexities in latex, when they occur frequently enough that mentioning and using the notation is not more complicated than explicitly describing the multiply-and-sum) and makes expressing element-wise multiplication complicated. J deals with this conflict by making its multiplication primitive be elementwise (consistent with the rest of the language) and by using a different verb (typically +/ .*) to represent the traditional non-element-wise multiply and sum operation.
 
=={{header|Java}}==
<lang Java>
Line 1,600 ⟶ 1,601:
Matrix(3, 3, [[1, 32, 0], [16807, 32, 16807], [7776, 161051, 243]])
</pre>
 
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
Line 1,748:
/ 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8
^ 1 32 243 1024 3125 7776 16807 32768 59049</pre>
 
=={{header|Perl 6}}==
{{Works with|rakudo|2016.05}}
Perl 6 already implements this and other metaoperators as higher-order functions (cross, zip, reduce, triangle, etc.) that are usually accessed through a meta-operator syntactic sugar that is productive over all appropriate operators, including user-defined ones. In this case, a dwimmy element-wise operator (generically known as a "hyper") is indicated by surrounding the operator with double angle quotes. Hypers dwim on the pointy end with cyclic APL semantics as necessary. You can turn the quote the other way to suppress dwimmery on that end. In this case we could have used <tt>»op»</tt> instead of <tt>«op»</tt> since the short side is always on the right.<lang perl6>my @a =
[1,2,3],
[4,5,6],
[7,8,9];
 
sub msay(@x) {
for @x -> @row {
print ' ', $_%1 ?? $_.nude.join('/') !! $_ for @row;
say '';
}
say '';
}
 
msay @a «+» @a;
msay @a «-» @a;
msay @a «*» @a;
msay @a «/» @a;
msay @a «+» [1,2,3];
msay @a «-» [1,2,3];
msay @a «*» [1,2,3];
msay @a «/» [1,2,3];
msay @a «+» 2;
msay @a «-» 2;
msay @a «*» 2;
msay @a «/» 2;
 
# In addition to calling the underlying higher-order functions directly, it's possible to name a function.
 
sub infix:<M+> (\l,\r) { l <<+>> r }
 
msay @a M+ @a;
msay @a M+ [1,2,3];
msay @a M+ 2;
</lang>
{{out}}
<pre> 2 4 6
8 10 12
14 16 18
 
0 0 0
0 0 0
0 0 0
 
1 4 9
16 25 36
49 64 81
 
1 1 1
1 1 1
1 1 1
 
2 3 4
6 7 8
10 11 12
 
0 1 2
2 3 4
4 5 6
 
1 2 3
8 10 12
21 24 27
 
1 2 3
2 5/2 3
7/3 8/3 3
 
3 4 5
6 7 8
9 10 11
 
-1 0 1
2 3 4
5 6 7
 
2 4 6
8 10 12
14 16 18
 
1/2 1 3/2
2 5/2 3
7/2 4 9/2
 
2 4 6
8 10 12
14 16 18
 
2 3 4
6 7 8
10 11 12
 
3 4 5
6 7 8
9 10 11</pre>
 
=={{header|Phix}}==
Line 2,043 ⟶ 1,946:
(array #[#[1 27] #[4 256]])
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{Works with|rakudo|2016.05}}
Perl 6 already implements this and other metaoperators as higher-order functions (cross, zip, reduce, triangle, etc.) that are usually accessed through a meta-operator syntactic sugar that is productive over all appropriate operators, including user-defined ones. In this case, a dwimmy element-wise operator (generically known as a "hyper") is indicated by surrounding the operator with double angle quotes. Hypers dwim on the pointy end with cyclic APL semantics as necessary. You can turn the quote the other way to suppress dwimmery on that end. In this case we could have used <tt>»op»</tt> instead of <tt>«op»</tt> since the short side is always on the right.<lang perl6>my @a =
[1,2,3],
[4,5,6],
[7,8,9];
 
sub msay(@x) {
for @x -> @row {
print ' ', $_%1 ?? $_.nude.join('/') !! $_ for @row;
say '';
}
say '';
}
 
msay @a «+» @a;
msay @a «-» @a;
msay @a «*» @a;
msay @a «/» @a;
msay @a «+» [1,2,3];
msay @a «-» [1,2,3];
msay @a «*» [1,2,3];
msay @a «/» [1,2,3];
msay @a «+» 2;
msay @a «-» 2;
msay @a «*» 2;
msay @a «/» 2;
 
# In addition to calling the underlying higher-order functions directly, it's possible to name a function.
 
sub infix:<M+> (\l,\r) { l <<+>> r }
 
msay @a M+ @a;
msay @a M+ [1,2,3];
msay @a M+ 2;
</lang>
{{out}}
<pre> 2 4 6
8 10 12
14 16 18
 
0 0 0
0 0 0
0 0 0
 
1 4 9
16 25 36
49 64 81
 
1 1 1
1 1 1
1 1 1
 
2 3 4
6 7 8
10 11 12
 
0 1 2
2 3 4
4 5 6
 
1 2 3
8 10 12
21 24 27
 
1 2 3
2 5/2 3
7/3 8/3 3
 
3 4 5
6 7 8
9 10 11
 
-1 0 1
2 3 4
5 6 7
 
2 4 6
8 10 12
14 16 18
 
1/2 1 3/2
2 5/2 3
7/2 4 9/2
 
2 4 6
8 10 12
14 16 18
 
2 3 4
6 7 8
10 11 12
 
3 4 5
6 7 8
9 10 11</pre>
 
=={{header|REXX}}==
10,343

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.