Scalar extension: Difference between revisions
Jump to navigation
Jump to search
→Examples
Miraheze>Adám Brudzewsky |
Miraheze>Adám Brudzewsky |
||
Line 39: | Line 39: | ||
Dyadic [[scalar functions]] and the [[Each operator]] use scalar extension to pair their arguments: | Dyadic [[scalar functions]] and the [[Each operator]] use scalar extension to pair their arguments: | ||
< | <source lang=apl> | ||
1 2 3 4 * 2 | 1 2 3 4 * 2 | ||
1 4 9 16 | 1 4 9 16 | ||
</ | </source> | ||
[[Replicate]] and [[Expand]] extend a scalar left argument to apply to each element of the right argument: | [[Replicate]] and [[Expand]] extend a scalar left argument to apply to each element of the right argument: | ||
< | <source lang=apl> | ||
2/'abc' | 2/'abc' | ||
aabbcc | aabbcc | ||
</ | </source> | ||
[[APL2]] and [[Dyalog APL]] use a variant of singleton extension when the selected axis of the right argument has length one: each element along that axis is reused for every element of the left argument. | [[APL2]] and [[Dyalog APL]] use a variant of singleton extension when the selected axis of the right argument has length one: each element along that axis is reused for every element of the left argument. | ||
< | <source lang=apl> | ||
⍴ 2 ¯3 /[2] 7 1 8⍴⍳56 | ⍴ 2 ¯3 /[2] 7 1 8⍴⍳56 | ||
7 5 8 | 7 5 8 | ||
</ | </source> | ||
{{Works in|[[Dyalog APL]]}} | {{Works in|[[Dyalog APL]], [APL2], [APLX]}} | ||
In languages which allow a vector left argument to [[Rotate]], the behavior with a scalar left argument follows from scalar extension. In the following example a length-2 vector could be used to rotate each row by a different amount. A scalar rotates both rows by the same amount. | In languages which allow a vector left argument to [[Rotate]], the behavior with a scalar left argument follows from scalar extension. In the following example a length-2 vector could be used to rotate each row by a different amount. A scalar rotates both rows by the same amount. | ||
< | <source lang=apl> | ||
3⌽2 6⍴'extendscalar' | 3⌽2 6⍴'extendscalar' | ||
endext | endext | ||
larsca | larsca | ||
</ | </source> |