Replicate: Difference between revisions

Jump to navigation Jump to search
1,247 bytes added ,  10:32, 18 March 2020
→‎Examples: Add APL2-style negative extension and reorganize
No edit summary
(→‎Examples: Add APL2-style negative extension and reorganize)
Line 26: Line 26:
8
8
</source>
</source>
A second extension introduced by [[NARS]] allows either positive or negative integers, where a negative number indicates that a [[fill element]] should be used instead of an element from the right argument. In this case the length of the result is the sum of the [[absolute value]] of the control array.
Replicate usually allows [[scalar extension]] of the left argument, which results in every element being copied a fixed number of times.
<source lang=apl>
      3 / 'replicate'
rrreeepppllliiicccaaattteee
</source>
 
=== Negative numbers ===
An extension introduced by [[NARS]] allows either positive or negative integers, where a negative number indicates that a [[fill element]] should be used instead of an element from the right argument. In this case the argument lengths must be equal (unless one side is a [[singleton]]). [[APL2]] defined a different extension: negative numbers do not correspond to any element of the right argument, but still indicate that many fills should be inserted. In the APL2 extension the length of the right argument is the number of non-negative elements in the left argument. In both extensions the length of the result is the sum of the [[absolute value]] of the control array.
<source lang=apl>
<source lang=apl>
       0 2 ¯3 1 / ⍳4
       0 2 ¯3 1 / ⍳4
2 2 0 0 0 4
2 2 0 0 0 4
</source>
</source>{{Works in|[[NARS2000]], [[Dyalog APL]], [[APLX]], [[ngn/APL]]}}
Replicate works along a particular axis, which can be specified in languages with [[function axis]] and otherwise is the first axis for <source lang=apl inline>⌿</source>, and the last axis for <source lang=apl inline>/</source> (except in [[A+]], which uses <source lang=apl inline>/</source> for the [[Leading axis theory|first-axis]] form and has no last-axis form).
<source lang=apl>
      0 2 ¯3 1 / ⍳3
2 2 0 0 0 3
</source>{{Works in|[[APL2]], [[APLX]], [[GNU APL]]}}
The extensions are the same when the right argument is subject to [[singleton extension]]. This extension was usually supported before any extension to negative numbers, but would not typically be useful because <source lang=apl inline>v/s</source> {{←→}} <source lang=apl inline>(+/v)/s</source> where <source lang=apl inline>v</source> is a non-negative integer vector and <source lang=apl inline>s</source> is a singleton.
<source lang=apl>
      1 ¯2 3/'a'
a  aaa
</source>{{Works in|[[NARS2000]], [[APL2]], [[Dyalog APL]], [[APLX]], [[ngn/APL]], [[GNU APL]]}}
 
=== High-rank arrays ===
Replicate works along a particular axis, which can be specified in languages with [[function axis]] and otherwise is the first axis for <source lang=apl inline>⌿</source>, and the last axis for <source lang=apl inline>/</source> (except in [[A+]], which uses <source lang=apl inline>/</source> for the [[Leading axis theory|first-axis]] form and has no last-axis form, and [[dzaima/APL]], which is similar but uses <source lang=apl inline>⌿</source>).
<source lang=apl>
<source lang=apl>
       ⊢A ← 4 6⍴⎕A
       ⊢A ← 4 6⍴⎕A
Line 48: Line 66:
MNOPQR
MNOPQR
STUVWX
STUVWX
</source>
Replicate usually allows [[scalar extension]] of the left argument, which results in every element being copied a fixed number of times.
<source lang=apl>
      3 / 'replicate'
rrreeepppllliiicccaaattteee
</source>
</source>


Navigation menu