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. If the right argument is an array of indices generated by Iota, Replicate resembles the function Indices. 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. 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 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). Replicate usually allows scalar extension of the left argument, which results in every element being copied a fixed number of times.

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 ). 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. This extension was rapidly and widely adopted, starting with NARS in 1981, and is now a feature of the ISO/IEC 13751:2001 standard.

Two extensions to allow negative numbers in the left argument have been introduced, in each case specifying that the negative of a number indicates that many fill elements should appear in the result. In 1981 NARS specified that these fill elements replace the corresponding right argument element, so that the lengths of the left and right arguments are always equal, and extended Expand similarly. APL2, in 1984, made the opposite choice, so that the length of the right argument along the specified axis is equal to the number of non-negative elements on the left. APL2 also loosened the conformability requirements further than simply allowing singleton extension: it allowed a right argument with length 1 along the replication axis to be extended. Dyalog APL, created before APL2, adopted the NARS definition for negative elements but added APL2 conformability extension in version 13.1. Later APLX took advantage of the fact that the two negative number extensions can be distinguished by the length of the left argument, and implemented every NARS and APL2 extension.

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.

Extension support
Here ">1" refers to the SHARP APL extension to non-negative integers, while "<0" refers to extension to negative integers in either NARS or APL2 style.

In each language without axis specification, there is only one form of Replicate, which always applies to the first axis or major cells—the last-axis form is discarded.

Outside of APL
While Replicate is rarely used in non-array programming languages, Compress is sometimes seen. Usually the same functionality is provided by the higher-order function filter, which an APLer might define as the monadic operator  on a vector argument.

While filter is similar to Compress, some extensions to the x86 instruction set are exactly equivalent to Compress on particular data types. In BMI2, the PEXT and PDEP instructions (parallel bit extract and deposit) are identical to Compress and Expand on the bits of a register argument. Indeed, Dyalog APL uses these instructions to implement those primitives (see Dyalog APL). The AVX-512 instructions VPCOMPRESSQ and VPEXPANDQ (and variations) are not only equivalent to Compress and Expand using a mask register for the Boolean argument and a vector register for the other argument, but are named after the APL functions. These instructions allow compression of 4-byte and 8-byte elements, and with AVX-512_VBMI2 support was added for 1-byte and 2-byte elements as well.

Lessons

 * APL Cultivation

Documentation

 * Dyalog
 * NARS2000
 * APLX
 * J Dictionary, NuVoc

Other

 * Marshall Lochbaum "Expanding Bits in Shrinking Time": On implementing Replicate of a Boolean array by a scalar.