In Iverson notation, Mesh () is a three-argument operation which merges two equal-rank arguments according to a Boolean vector. The relationship between Mask and Mesh is similar to that between Compress and Expand. A related function also named Mesh is implemented in NARS and NARS2000 as a case of Compose:
R using an integer (not Boolean) control vector. Bob Bernecky has suggested the syntax
a(u\)b (requiring Expand to be an operator) for .
In Iverson notation
Mesh in Iverson notation combines or interleaves two arguments according to a Boolean control. The result has the same shape as the control array, and contains entries taken from the left argument when the control is 0 and from the right when it is 1.
A Programming Language defines the Mesh for vectors and and Boolean vector , where , and , to be the vector whose compressions by and its negation yield the other arguments: and . Conformability requirements give ; because we have .
The following forms of Mesh with matrices are defined. Below, matrices are denoted with capital letters while vectors use lowercase letters.
|Iverson notation||Description||Modern APL (
|mesh matrices along rows|
|mesh differently for each row||
||where in each component|
|mesh vectors in multiple ways||
||like the previous with repeated rows in and|
|mesh matrices along columns||
|mesh differently for each column||
||where in each component|
|mesh vectors differently in each column||
||like the previous with repeated columns in and|
(a,b)[⍋⍋u] (b,a)[⍋⍒u] ⍝ Reversed order by sorting u the other way
These solutions were published in 1971 by Bob Smith, who attributes them to Luther Woodrum by way of Ken Iverson. They also appear in the FinnAPL idiom library. The idea is that every element of
b should be included in the final result, but they are ordered based on
u, so that elements of
a correspond to 0s and those of
b correspond to 1s. Given such a vector, sorting it according to
u, or equivalently, permuting by the Grade of
u, would return it to the separated vector
a,b. It follows that applying the inverse permutation
a,b into the meshed vector.
Another, more straightforward strategy uses selective assignment and Expand. It creates a temporary vector
t, placing elements of
a in the appropriate positions with Expand, and then inserts elements of
b in the appropriate positions by assigning to the Compress of
t←(~u)\a ⋄ (u/t)←b ⋄ t
The two steps can be reversed, as long as
a is paired with
b is paired with
u. Structural Under allows the assignment to be performed in a functional style, with no temporary variable. For example, dzaima/APL admits these implementations:
b⍨⍢(u∘⌿) (~u)⍀a a⍨⍢((~u)∘⌿) b⍨⍢(u∘⌿) a,b
Here any vector of the same length could be used in place of
((~u)\a) (u mask) u\b
u⊃¨((~u)\a),¨(u\b) ⍝ Nested APL u(⌷⍤0 1)((~u)\a)(,⍤0)(u\b) ⍝ With the Rank operator ((~u)\a) ⊣⍢(u∘/) (u\b) ⍝ Structural Under