Replicate: Difference between revisions

Jump to navigation Jump to search
2,627 bytes added ,  10:09, 4 May 2020
m
(→‎Examples: Add APL2-style negative extension and reorganize)
(8 intermediate revisions by 2 users not shown)
Line 37: Line 37:
       0 2 ¯3 1 / ⍳4
       0 2 ¯3 1 / ⍳4
2 2 0 0 0 4
2 2 0 0 0 4
</source>{{Works in|[[NARS2000]], [[Dyalog APL]], [[APLX]], [[ngn/APL]]}}
</source>{{Works in|[[NARS2000]], [[Dyalog APL]], [[APLX]], [[ngn/apl]]}}
<source lang=apl>
<source lang=apl>
       0 2 ¯3 1 / ⍳3
       0 2 ¯3 1 / ⍳3
Line 44: Line 44:
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.
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>
<source lang=apl>
       1 ¯2 3/'a'
       1 ¯2 3 / 'a'
a  aaa
a  aaa
</source>{{Works in|[[NARS2000]], [[APL2]], [[Dyalog APL]], [[APLX]], [[ngn/APL]], [[GNU APL]]}}
</source>{{Works in|[[NARS2000]], [[APL2]], [[Dyalog APL]], [[APLX]], [[ngn/apl]], [[GNU APL]]}}


=== High-rank arrays ===
=== 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>).
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>
<source lang=apl>
       ⊢A ← 4 6⍴⎕A
       ⊢A ← 4 6⍴⎕A
Line 67: Line 67:
STUVWX
STUVWX
</source>
</source>
[[APL2]] further extends the [[singleton extension]] of the right argument, allowing it to have length 1 along the replication axis even if other axes have lengths not equal to 1.
<source lang=apl>
      1 ¯2 3 / ⍪'abc'
a  aaa
b  bbb
c  ccc
</source>{{Works in|[[APL2]], [[Dyalog APL]], [[APLX]], [[ngn/apl]], [[GNU APL]]}}
[[dzaima/APL]] expects arguments of <source lang=apl inline>⌿</source> to have matching shape, and replicates the [[ravel]] of both.
=== Operator or function? ===
The syntax <source lang=apl inline>a / b</source> is ambiguous: it may be an invocation of a [[dyadic function]] <source lang=apl inline>/</source> with left [[argument]] <source lang=apl inline>a</source> and right argument <source lang=apl inline>b</source>, or of a [[monadic operator]] with [[operand]] <source lang=apl inline>a</source> and right argument <source lang=apl inline>b</source>. In early APLs there was no way to resolve this ambiguity, but with the extension of [[operator]]s to allow arbitrary function operands instead of a specified set of [[primitive function]]s, the distinction becomes apparent: a function Replicate can be used as an [[operand]] while an operator Replicate cannot.
One test of Replicate's nature is to try Replicate [[Each]]<ref>Benkard, J. Philip. [https://dl.acm.org/doi/10.1145/384282.28345 "Replicate each, anyone?"]. [[APL87]].</ref> with an expression such as <source lang=apl inline>1 3 /¨ 'ab' 'cd'</source>. If Replicate is implemented as an operator, it will be applied to the operand <source lang=apl inline>1 3</source>, and Each will be applied to the resulting [[derived function]] <source lang=apl inline>1 3/</source>.
<source lang=apl>
      1 3 /¨ 'ab' 'cd' 
abbb  cddd
      (1 3/)¨ 'ab' 'cd'
abbb  cddd
</source>{{Works in|[[SHARP APL]] (with <source lang=apl inline>¨></source> in place of <source lang=apl inline>¨</source>), [[APL2]], [[APLX]]}}
If Replicate is a function, then Each will apply to Replicate only, and the resulting derived function will be invoked monadically.
<source lang=apl>
      1 3 /¨ 'ab' 'cd' 
ab  cccddd
      1 3 (/¨) 'ab' 'cd'
ab  cccddd
</source>{{Works in|[[NARS2000]], [[Dyalog APL]], [[GNU APL]]}}
In early APLs such as [[APL\360]], applying an operator to Compress will always result in a [[SYNTAX ERROR]], because Compress is not an allowed operand of any operator. This is also the case in [[ngn/apl]]: although operators can apply to any function, Replicate cannot be used unless both arguments are immediately available. In both cases there is no way to determine whether Replicate "acts like a function" or "acts like an operator".


== History ==
== History ==
Line 114: Line 143:
| [[GNU APL]]                                        || Function  || {{Yes}} || {{No}}  || {{Yes}} || {{Yes|APL2}}        || {{Yes}} ||
| [[GNU APL]]                                        || Function  || {{Yes}} || {{No}}  || {{Yes}} || {{Yes|APL2}}        || {{Yes}} ||
|-
|-
| [[dzaima/APL]] (<source lang=apl inline>⌿</source>) || Function  || {{Yes}} || {{Yes}} || {{No}}  || {{None}}            || {{No}}  || Vector right argument only
| [[dzaima/APL]] (<source lang=apl inline>⌿</source>) || Function  || {{Yes}} || {{Yes}} || {{No}}  || {{None}}            || {{No}}  ||
|}
|}


Line 146: Line 175:
<references />
<references />


{{APL built-ins}}
{{APL built-ins}}[[Category:Primitive functions]][[Category:Functions with first- and last-axis forms]]

Navigation menu