# Mesh

In Iverson notation, Mesh (${\displaystyle \backslash {}a,u,b\backslash }$) 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: L(a∘\)R combines L and R using an integer (not Boolean) control vector. Bob Bernecky has suggested the syntax a(u\)b (requiring Expand to be an operator) for ${\displaystyle \backslash {}a,u,b\backslash }$.[1]

## 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 ${\displaystyle \backslash {}a,u,b\backslash }$  for vectors ${\displaystyle a}$  and ${\displaystyle b}$  and Boolean vector ${\displaystyle u}$ , where ${\displaystyle +/{\overline {u}}=\nu (a)}$ , and ${\displaystyle +/u=\nu (b)}$ , to be the vector ${\displaystyle c}$  whose compressions by ${\displaystyle u}$  and its negation yield the other arguments: ${\displaystyle {\overline {u}}/c=a}$  and ${\displaystyle u/c=b}$ . Conformability requirements give ${\displaystyle \nu (c)=\nu (u)}$ ; because ${\displaystyle \nu (u)=(+/{\overline {u}})+(+/u)}$  we have ${\displaystyle \nu (c)=\nu (a)+\nu (b)}$ .

Mesh ${\displaystyle \backslash {}a,u,b\backslash }$  is a generalization of Catenate ${\displaystyle a\oplus b}$ , since meshing with a suffix vector ${\displaystyle \backslash {}a,\omega ^{\nu (b)},b\backslash }$  produces ${\displaystyle a\oplus b}$ .

Iverson notes the following relationships between Mask, Mesh, Compress, and Expand:

{\displaystyle {\begin{aligned}/a,u,b/&=\backslash {}{\overline {u}}/a,u,u/b\backslash \\\backslash {}a,u,b\backslash &=/{\overline {u}}\backslash {}a,u,u\backslash {}b/\end{aligned}}}

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 (⎕IO←0) Notes
${\displaystyle \backslash {}A,u,B\backslash }$  mesh matrices along rows u⊃¨⍤1⊢((~u)\A),¨(u\B)
${\displaystyle \backslash {}A,U,B\backslash }$  mesh differently for each row U⊃¨⍤1⊢((~U)\⍤1⊢A),¨(U\⍤1⊢B) where ${\displaystyle +/U=\nu (A)+\nu (B)}$  in each component
${\displaystyle \backslash {}a,U,b\backslash }$  mesh vectors in multiple ways U⊃¨⍤1⊢((~U)\⍤1⊢A),¨(U\⍤1⊢B) like the previous with repeated rows in ${\displaystyle A}$  and ${\displaystyle B}$
${\displaystyle \backslash \!\backslash {}A,u,B\backslash \!\backslash }$  mesh matrices along columns u⊃¨⍤0 1⊢((~u)⍀A),¨(u⍀B)
${\displaystyle \backslash \!\backslash {}A,U,B\backslash \!\backslash }$  mesh differently for each column ⍉U⊃¨⍤1⊢((~U)\⍤1⍉A),¨(U\⍤1⍉B) where ${\displaystyle +/\!/U=\mu (A)+\mu (B)}$  in each component
${\displaystyle \backslash \!\backslash {}a,U,b\backslash \!\backslash }$  mesh vectors differently in each column ⍉U⊃¨⍤1⊢((~U)\⍤1⍉a),¨(U\⍤1⍉b) like the previous with repeated columns in ${\displaystyle A}$  and ${\displaystyle B}$

### APL models

In all APLs, ${\displaystyle /a,u,b/}$  for vectors ${\displaystyle a}$ , ${\displaystyle b}$ , and ${\displaystyle u}$  can be implemented using the ordinal idiom consisting of two copies of Grade:

(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.[2][3] They also appear in the FinnAPL idiom library. The idea is that every element of a and 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 ⍋⍋u of ⍋u transforms 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.

t←(~u)\a ⋄ (u/t)←b ⋄ t


The two steps can be reversed, as long as a is paired with ~u and 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

Works in: dzaima/APL

Here any vector of the same length could be used in place of a,b.

If Mask is available, for instance as a monadic operator, then Mesh can be obtained by masking together the expansion of each argument:

((~u)\a) (u mask) u\b


Depending on available language facilities, this idea can be realized in a few different ways. Here the expressions which use indexing must have the index origin set to 0.

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