# — Replicate

N#Y repeats each major cell of Y by its count in N. Boolean counts filter.

1 0 1#10 20 30          ⍝ 10 30
2 1#10 20               ⍝ 10 10 20
0 2#[1 2 ⋄ 3 4]         ⍝ [3 4 ⋄ 3 4]

A negative count replaces its cell with that many fills. A single count applies to every cell. A single cell serves every count.

1 ¯2 1#10 20 30         ⍝ 10 0 0 30
2 ¯1 1#"abc"            ⍝ "aa c"
2#3 4                   ⍝ 3 3 4 4
1 0 2#5                 ⍝ 5 5 5

With Axis, #⍠¯1 replicates along the last axis.

1 0 1#⍠¯1 [1 2 3 ⋄ 4 5 6]   ⍝ [1 3 ⋄ 4 6]

Expand

With N fixed, ⁻¹ inverts N#. The inverse gives one cell for each count. A positive count gives the first of its copies, and a zero or negative count gives a fill. With a Boolean mask, the inverse expands Y. The result has the next cell of Y wherever the mask has 1, and a fill wherever it has 0.

1 0 1#⁻¹10 20           ⍝ 10 0 20
1 0 1 #⁻¹⍠¯1 [1 2 ⋄ 3 4]      ⍝ [1 0 2 ⋄ 3 0 4]
2 1#⁻¹10 10 20          ⍝ 10 20
1 ¯2 1#⁻¹10 0 0 30      ⍝ 10 0 30

The counts must use every cell once, unless Y has one cell. A single count undoes a replicate by that count.

1 0 1#⁻¹5               ⍝ 5 0 5
2#⁻¹1 1 2 2             ⍝ 1 2

Under and selective assignment both keep the cells that N drops.

-⌾(1 0 1#)1 2 3         ⍝ ¯1 2 ¯3
x←1 2 3
(1 0 1#x)←0
x                       ⍝ 0 2 0

APL difference: # replaces /, ⌿, \ and ⍀. Dyalog’s expand with counts other than 0 and 1, N\Y, is (1⌈|N)#(N>0)#⁻¹Y.

Errors

  • SYNTAX: no counts
  • RANK: counts of rank 2 or more
  • LENGTH: counts that don’t match the cells, or an inverse that doesn’t use every cell
  • DOMAIN: counts that aren’t integers, or one inverse count that doesn’t divide the number of cells