Replicate

From APL Wiki
Revision as of 11:05, 13 March 2020 by Marshall (talk | contribs) (→‎History: A+, J, K)
Jump to navigation Jump to search

Replicate (/, ), or Copy (#) in J, is a dyadic function or monadic operator that copies each element of the right argument a given number of times, ordering the copies along a specified axis. Typically / is called Replicate while is called "Replicate First" or an equivalent. Replicate is a widely-accepted extension of the function Compress, which requires the number of copies to be Boolean: each element is either retained (1 copy) or discarded (0 copies). Replicate with a Boolean left argument or operand may still be called "Compress".

Replicate is usually associated with Expand (\), and the two functions are related to Mask and Mesh. It is also closely related to the Indices function. It shares a glyph with Reduce even though Replicate is naturally a function and Reduce must be an operator. This incongruity is occasionally resolved by making Replicate an operator itself, and more often by function-operator overloading allowing both syntactic elements to coexist.

Outside of APL, filter typically provides the functionality of Compress, while Replicate has no common equivalent.

Examples

When used with a Boolean array (often called a "mask") on the left, Replicate is called Compress. It filters the right argument, returning only those elements which correspond to 1s in the provided mask.

      1 1 0 1 0 1 0 0 / 'compress'
cope

If the right argument is an array of indices generated by Iota, Replicate resembles the function Indices.

      1 1 0 0 1 / ⍳5
1 2 5

With an array of non-negative integers, Replicate copies each element of the right argument the corresponding number of times. As with Compress, these copies retain their original ordering, and the length of the result is the sum of the control array.

      0 3 0 0 2 0 1 0 2 / 'replicate'
eeeiiaee
      +/ 0 3 0 0 2 0 1 0 2
8
      ⍴ 0 3 0 0 2 0 1 0 2 / 'replicate'
8

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.

      0 2 ¯3 1 / ⍳4
2 2 0 0 0 4

Replicate works along a particular axis, which can be specified in languages with function axis and otherwise is the first axis for , and the last axis for / (except in A+, which uses / for the first-axis form and has no last-axis form).

      ⊢A ← 4 6⍴⎕A
ABCDEF
GHIJKL
MNOPQR
STUVWX
      1 0 0 4 0 2 / A
ADDDDFF
GJJJJLL
MPPPPRR
SVVVVXX
      0 2 1 1 ⌿ A
GHIJKL
GHIJKL
MNOPQR
STUVWX

Replicate usually allows scalar extension of the left argument, which results in every element being copied a fixed number of times.

      3 / 'replicate'
rrreeepppllliiicccaaattteee

History

Compress was described in A Programming Language, where it was written with the symbols   and  . In Iverson notation compression was particularly important because Take and Drop could be performed only by compression with a prefix or suffix vector. It was included in APL\360, which changed the doubled slash to a barred slash , and allowed a specified axis and singleton extension on both sides (very briefly, singleton extension was allowed only for the right argument[1]). The APL\360 definition continued to be included in APLs unchanged until 1980.

In 1980, Bob Bernecky introduced the extension Replicate to SHARP APL: he allowed an operand (since SHARP's Replicate is an operator) consisting of non-negative integers rather than just Booleans to indicate the number of times to copy.[2] This extension was implemented in NARS in 1981, along with further extensions: a similar change to Expand, and the extension allowing negative integers to indicate fill elements. Both extensions were quickly adopted by the APL community, and were included in new nested APLs such as Dyalog APL and APL2, and later the ISO/IEC 13751:2001 standard.

A+ and J modified Replicate to fit leading axis theory. Rather than allow Replicate to operate on any axis they have only one Replicate function (in A+, /; in J, #) which works on the first axis—it copies major cells rather than elements. Both languages rejected the NARS extension to negative left arguments, but J introduced its own system to add fill elements by allowing complex numbers in the left argument, and removed the Expand function entirely. Arthur Whitney went on to make a more radical change in K, removing Replicate entirely in favor of Where.

External Links

Lessons

Documentation

References

  1. Falkoff, A.D., and K.E. Iverson, "APL\360 User's Manual". IBM, August 1968.
  2. Bernecky, Bob. SATN-34: Replication. IPSA. 1980-08-15.


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector