2,951
edits
m (→In Iverson notation: Tighten up double slashes) |
(APL models) |
||
Line 2: | Line 2: | ||
|<math>\backslash{}a,u,b\backslash</math> | |<math>\backslash{}a,u,b\backslash</math> | ||
|} | |} | ||
In [[Iverson notation]], '''Mesh''' (<math>\backslash{}a,u,b\backslash</math>) 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]]: <source lang=apl inline>L(a∘\)R</source> combines <source lang=apl inline>L</source> and <source lang=apl inline>R</source> using an integer (not Boolean) control vector | In [[Iverson notation]], '''Mesh''' (<math>\backslash{}a,u,b\backslash</math>) 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]]: <source lang=apl inline>L(a∘\)R</source> combines <source lang=apl inline>L</source> and <source lang=apl inline>R</source> using an integer (not Boolean) control vector. | ||
== In Iverson notation == | == In Iverson notation == | ||
Line 36: | Line 36: | ||
| style=text-align:center | <math>\backslash\!\backslash{}a,U,b\backslash\!\backslash</math> || mesh vectors differently in each column || <source lang=apl inline>⍉U⊃¨⍤1⊢((~U)\⍤1⍉a),¨(U\⍤1⍉b)</source> || like the previous with repeated columns in <math>A</math> and <math>B</math> | | style=text-align:center | <math>\backslash\!\backslash{}a,U,b\backslash\!\backslash</math> || mesh vectors differently in each column || <source lang=apl inline>⍉U⊃¨⍤1⊢((~U)\⍤1⍉a),¨(U\⍤1⍉b)</source> || like the previous with repeated columns in <math>A</math> and <math>B</math> | ||
|} | |} | ||
=== APL models === | |||
In all APLs, <math>/a,u,b/</math> for vectors <math>a</math>, <math>b</math>, and <math>u</math> can be implemented using the [[ordinal]] idiom consisting of two copies of [[Grade]]: | |||
<source lang=apl> | |||
(a,b)[⍋⍋u] | |||
(b,a)[⍋⍒u] ⍝ Reversed order by sorting u the other way | |||
</source> | |||
These solutions were published in 1971 by [[Bob Smith]], who attributes them to Luther Woodrum by way of [[Ken Iverson]].<ref>[[Bob Smith|Smith, Bob]]. "Problem section". [[APL Quote-Quad]] Vol. III No. 2&3, p.61.</ref><ref>[[Hui, Roger|Roger Hui]]. [https://www.jsoftware.com/papers/mesh.htm "An Amuse-Bouche from APL History"].</ref> The idea is that every element of <source lang=apl inline>a</source> and <source lang=apl inline>b</source> should be included in the final result, but they are ordered based on <source lang=apl inline>u</source>, so that elements of <source lang=apl inline>a</source> correspond to 0s and those of <source lang=apl inline>b</source> correspond to 1s. Given such a vector, [[Sort by|sorting]] it according to <source lang=apl inline>u</source>, or equivalently, permuting by the [[Grade]] of <source lang=apl inline>u</source>, would return it to the separated vector <source lang=apl inline>a,b</source>. It follows that applying the inverse [[permutation]] <source lang=apl inline>⍋⍋u</source> of <source lang=apl inline>⍋u</source> transforms <source lang=apl inline>a,b</source> into the meshed vector. | |||
Another, more straightforward strategy uses [[selective assignment]] and [[Expand]]. It creates a temporary vector <source lang=apl inline>t</source>, placing elements of <source lang=apl inline>a</source> in the appropriate positions with [[Expand]], and then inserts elements of <source lang=apl inline>b</source> in the appropriate positions by assigning to the [[Compress]] of <source lang=apl inline>t</source>. Of course these steps could also be performed in the opposite order. | |||
<source lang=apl> | |||
t←(~u)\a ⋄ (u/t)←b ⋄ t | |||
</source> | |||
The two steps can be reversed, as long as <source lang=apl inline>a</source> is paired with <source lang=apl inline>~u</source> and <source lang=apl inline>b</source> is paired with <source lang=apl inline>u</source>. [[Structural Under]] allows the assignment to be performed in a functional style, with no temporary variable. For example, in [[dzaima/APL]], which implements Structural Under but not Expand, admits this implementation: | |||
<source lang=apl> | |||
a⍨⍢((~u)∘⌿) b⍨⍢(u∘⌿) a,b | |||
</source> | |||
Here any vector of the same length could be used in place of <source lang=apl inline>a,b</source>. | |||
If [[Mask]] is available, for instance as a [[monadic operator]], then Mesh can be obtained by masking together the [[Expand|expansion]] of each argument: | |||
<source lang=apl> | |||
((~u)\a) (u mask) u\b | |||
</source> | |||
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. | |||
<source lang=apl> | |||
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 | |||
</source> | |||
== External Links == | == External Links == | ||
Line 44: | Line 74: | ||
* [http://wiki.nars2000.org/index.php/Compose#Mesh NARS2000] | * [http://wiki.nars2000.org/index.php/Compose#Mesh NARS2000] | ||
== References == | |||
<references /> |