Mix: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
 
(9 intermediate revisions by 3 users not shown)
Line 3: Line 3:
|}
|}
'''Mix''' (<code>↑</code> or <code>⊃</code> in [[Nested array model|nested]] APLs; '''Disclose''' or '''Open''' <code>></code> in [[Flat array model|flat]] array languages) is a [[primitive function|primitive]] [[monadic function]] which reduces the [[depth]] of its [[argument]] by combining all its [[element]] arrays into a single array. The [[shape]] of its result is then the shape of the argument followed by the common shape of the argument elements. Depending on the definition of Mix, the elements may be required to share the same shape, or they may be extended in shape (with [[fill element]]s) or rank to fit a common shape. In all APLs Mix allows a [[simple array]] argument, and returns it unchanged. Mix is a left inverse to both [[Enclose]] and [[Split]], while the function [[First]], which is identical to Mix on [[scalar]]s, is a left inverse to [[Enclose]] only.
'''Mix''' (<code>↑</code> or <code>⊃</code> in [[Nested array model|nested]] APLs; '''Disclose''' or '''Open''' <code>></code> in [[Flat array model|flat]] array languages) is a [[primitive function|primitive]] [[monadic function]] which reduces the [[depth]] of its [[argument]] by combining all its [[element]] arrays into a single array. The [[shape]] of its result is then the shape of the argument followed by the common shape of the argument elements. Depending on the definition of Mix, the elements may be required to share the same shape, or they may be extended in shape (with [[fill element]]s) or rank to fit a common shape. In all APLs Mix allows a [[simple array]] argument, and returns it unchanged. Mix is a left inverse to both [[Enclose]] and [[Split]], while the function [[First]], which is identical to Mix on [[scalar]]s, is a left inverse to [[Enclose]] only.
== Examples ==
<syntaxhighlight lang="apl">
      x ← (0 1 2) (3 4 5) (9 9)
      ⍴x ⍝ x is a nested array, i.e. array of array
3
      ↑x ⍝ note that on the third row, 0 is padded to the end so the matrix can be orthogonal (non-ragged)
0 1 2
3 4 5
9 9 0
      ⍴↑x ⍝ ↑x is a non-nested 2-d array
3 3
</syntaxhighlight>
Example application, to find the longest common prefix:
<syntaxhighlight lang="apl">
      lcp←{(+/∧\(∧/⊃=⊢)¨↓⍉↑⍵)↑⊃⍵}
      lcp 'flowers' 'flow' 'flip'
fl
      lcp 'choco' 'choky' 'chom'
cho   
</syntaxhighlight>


== History ==
== History ==
Line 8: Line 31:
Early experiments in array nesting usually defined a primitive to extract the contents of a nested [[scalar]], but did not always specify how it should behave on an array. [[Jim Brown]]'s "A Generalization of APL",<ref>[[Jim Brown]]. [http://www.softwarepreservation.org/projects/apl/Books/AGENERALIZATIONOFAPL "A Generalization of APL"] (Ph.D. thesis). 1971.</ref> which would become [[APL2]], defined such a primitive, "reveal", while stating that it "may be undefined" on non-scalar arrays.
Early experiments in array nesting usually defined a primitive to extract the contents of a nested [[scalar]], but did not always specify how it should behave on an array. [[Jim Brown]]'s "A Generalization of APL",<ref>[[Jim Brown]]. [http://www.softwarepreservation.org/projects/apl/Books/AGENERALIZATIONOFAPL "A Generalization of APL"] (Ph.D. thesis). 1971.</ref> which would become [[APL2]], defined such a primitive, "reveal", while stating that it "may be undefined" on non-scalar arrays.


Mix (<source lang=apl inline>↑</source>) was first introduced by [[NARS]] in 1981.<ref>Carl M. Cheney. [http://www.sudleyplace.com/APL/Nested%20Arrays%20System.pdf ''APL*PLUS Nested Arrays System''] ([[NARS]] reference manual). [[STSC]]. 1981.</ref> It required all elements to have matching shape, and allowed a [[Function axis|specified axis]] to define where in the result array their axes would be placed. In the same year, [[SHARP APL]] introduced the concept of a [[box]]ed array, along with the function Disclose (<source lang=apl inline>></source>).<ref>[[Ken Iverson]]. SATN-41: [https://www.jsoftware.com/papers/satn41.htm Composition and Enclosure]. [[IPSA]]. 1981-06-20.</ref> In SHARP, Disclose was simply defined to be the inverse of Enclose, but given [[function rank]] 0 so that it would disclose each box in an argument and combine the results. Based on SHARP's definition of [[Rank (operator)|Rank]], the arrays to be combined were required to have the same [[shape]], and because of its [[flat array model]], they also had to have the same type (numeric, character, or boxed), as otherwise the result array would not be representable.
Mix (<syntaxhighlight lang=apl inline>↑</syntaxhighlight>) was first introduced by [[NARS]] in 1981.<ref>Carl M. Cheney. [http://www.sudleyplace.com/APL/Nested%20Arrays%20System.pdf ''APL*PLUS Nested Arrays System''] ([[NARS]] reference manual). [[STSC]]. 1981.</ref> It required all elements to have matching shape, and allowed a [[Function axis|specified axis]] to define where in the result array their axes would be placed. In the same year, [[SHARP APL]] introduced the concept of a [[box]]ed array, along with the function Disclose (<syntaxhighlight lang=apl inline>></syntaxhighlight>).<ref>[[Ken Iverson]]. SATN-41: [https://www.jsoftware.com/papers/satn41.htm Composition and Enclosure]. [[IPSA]]. 1981-06-20.</ref> In SHARP, Disclose was simply defined to be the inverse of Enclose, but given [[function rank]] 0 so that it would disclose each box in an argument and combine the results. Based on SHARP's definition of [[Rank (operator)|Rank]], the arrays to be combined were required to have the same [[shape]], and because of its [[flat array model]], they also had to have the same type (numeric, character, or boxed), as otherwise the result array would not be representable.


[[APL2]], released in 1984, used the same name Disclose as SHARP APL but introduced the glyph [[Right Shoe]] (<source lang=apl inline>⊃</source>). It shared NARS's [[function axis]] definition, but extended the function to allow argument elements with different [[shape]]s as long as they had the same [[rank]]. Arrays with different shapes would be padded using [[fill element]]s as with [[Take]] to bring them to a common shape large enough to contain every element.
[[APL2]], released in 1984, used the same name Disclose as SHARP APL but introduced the glyph [[Right Shoe]] (<syntaxhighlight lang=apl inline>⊃</syntaxhighlight>). It shared NARS's [[function axis]] definition, but extended the function to allow argument elements with different [[shape]]s as long as they had the same [[rank]]. Arrays with different shapes would be padded using [[fill element]]s as with [[Take]] to bring them to a common shape large enough to contain every element.


A further extension was introduced as a consequence of [[J]]'s definition of the [[Rank operator]]. In J, result arrays in a function applied with rank can have different [[rank]]s, and results with lower rank are brought to a common rank by adding leading 1s to the shape. This change was taken up by [[Dyalog APL]] in [[Dyalog APL 14.0|version 14.0]], which introduced [[Rank (operator)|Rank]] following J's definition, in order to make Mix and Rank consistent. However, Dyalog differs from J in that it permits [[mixed array]]s, so while in J an array containing a mix of numbers, characters, and boxes cannot be mixed, in Dyalog any array at all can be mixed.
A further extension was added to SHARP APL in 1989,<ref>[[IPSA]]. [https://archive.org/details/sharp-apl-release-20.0-guide-for-apl-programmers "SHARP APL Release 20.0: Guide for APL Programmers"].</ref> and featured in [[J]] as a consequence of its definition of the [[Rank operator]]. In J, result arrays in a function applied with rank can have different [[rank]]s, and results with lower rank are brought to a common rank by adding leading 1s to the shape. This change was taken up by [[Dyalog APL]] in [[Dyalog APL 14.0|version 14.0]], which introduced [[Rank (operator)|Rank]] following J's definition, in order to make Mix and Rank consistent. However, Dyalog differs from J in that it permits [[mixed array]]s, so while in J an array containing a mix of numbers, characters, and boxes cannot be mixed, in Dyalog any array at all can be mixed.


== Language support ==
== Language support ==
Line 23: Line 46:
| [[NARS]]                        || Mix      || <code>↑</code> || [[Shape]]          || {{Yes}} || {{No}}
| [[NARS]]                        || Mix      || <code>↑</code> || [[Shape]]          || {{Yes}} || {{No}}
|-
|-
| [[SHARP APL]], [[A+]]           || Disclose || <code>></code> || [[Shape]] and type || {{No}}  || {{No}}
| [[A+]], SHARP <19.0              || Disclose || <code>></code> || [[Shape]] and type || {{No}}  || {{No}}
|-
| [[SHARP APL]]                    || Disclose || <code>></code> || Type              || {{No}}  || {{No}}
|-
|-
| [[APL2]], [[APLX]]              || Disclose || <code>⊃</code> || [[Rank]]          || {{No}}  || {{Yes}}
| [[APL2]], [[APLX]]              || Disclose || <code>⊃</code> || [[Rank]]          || {{No}}  || {{Yes}}
|-
|-
| [[NARS2000]]                     || Disclose || <code>⊃</code> || None              || {{No}}  || {{No}}
| [[NARS2000]], [[Kap]]            || Disclose || <code>⊃</code> || None              || {{No}}  || {{No}}
|-
|-
| [[Dyalog APL]]                  || Mix      || <code>↑</code> or <code>⊃</code> ([[Migration level|ML]]) || None || {{Yes}} || {{Yes}}
| [[Dyalog APL]]                  || Mix      || <code>↑</code> or <code>⊃</code> ([[Migration level|ML]]) || None || {{Yes}} || {{Yes}}
Line 51: Line 76:
* [http://microapl.com/apl_help/ch_020_020_600.htm APLX]
* [http://microapl.com/apl_help/ch_020_020_600.htm APLX]
* [[J]] [https://www.jsoftware.com/help/dictionary/d020.htm dictionary], [https://code.jsoftware.com/wiki/Vocabulary/gt NuVoc]
* [[J]] [https://www.jsoftware.com/help/dictionary/d020.htm dictionary], [https://code.jsoftware.com/wiki/Vocabulary/gt NuVoc]
* [https://mlochbaum.github.io/BQN/doc/couple.html BQN]


== References ==
== References ==

Latest revision as of 18:27, 29 January 2024

>

Mix ( or in nested APLs; Disclose or Open > in flat array languages) is a primitive monadic function which reduces the depth of its argument by combining all its element arrays into a single array. The shape of its result is then the shape of the argument followed by the common shape of the argument elements. Depending on the definition of Mix, the elements may be required to share the same shape, or they may be extended in shape (with fill elements) or rank to fit a common shape. In all APLs Mix allows a simple array argument, and returns it unchanged. Mix is a left inverse to both Enclose and Split, while the function First, which is identical to Mix on scalars, is a left inverse to Enclose only.

Examples

      x ← (0 1 2) (3 4 5) (9 9)
      ⍴x ⍝ x is a nested array, i.e. array of array
3
      ↑x ⍝ note that on the third row, 0 is padded to the end so the matrix can be orthogonal (non-ragged)
0 1 2
3 4 5
9 9 0
      ⍴↑x ⍝ ↑x is a non-nested 2-d array
3 3

Example application, to find the longest common prefix:

      lcp←{(+/∧\(∧/⊃=⊢)¨↓⍉↑⍵)↑⊃⍵}
      lcp 'flowers' 'flow' 'flip'
fl
      lcp 'choco' 'choky' 'chom'
cho

History

Early experiments in array nesting usually defined a primitive to extract the contents of a nested scalar, but did not always specify how it should behave on an array. Jim Brown's "A Generalization of APL",[1] which would become APL2, defined such a primitive, "reveal", while stating that it "may be undefined" on non-scalar arrays.

Mix () was first introduced by NARS in 1981.[2] It required all elements to have matching shape, and allowed a specified axis to define where in the result array their axes would be placed. In the same year, SHARP APL introduced the concept of a boxed array, along with the function Disclose (>).[3] In SHARP, Disclose was simply defined to be the inverse of Enclose, but given function rank 0 so that it would disclose each box in an argument and combine the results. Based on SHARP's definition of Rank, the arrays to be combined were required to have the same shape, and because of its flat array model, they also had to have the same type (numeric, character, or boxed), as otherwise the result array would not be representable.

APL2, released in 1984, used the same name Disclose as SHARP APL but introduced the glyph Right Shoe (). It shared NARS's function axis definition, but extended the function to allow argument elements with different shapes as long as they had the same rank. Arrays with different shapes would be padded using fill elements as with Take to bring them to a common shape large enough to contain every element.

A further extension was added to SHARP APL in 1989,[4] and featured in J as a consequence of its definition of the Rank operator. In J, result arrays in a function applied with rank can have different ranks, and results with lower rank are brought to a common rank by adding leading 1s to the shape. This change was taken up by Dyalog APL in version 14.0, which introduced Rank following J's definition, in order to make Mix and Rank consistent. However, Dyalog differs from J in that it permits mixed arrays, so while in J an array containing a mix of numbers, characters, and boxes cannot be mixed, in Dyalog any array at all can be mixed.

Language support

Mix is only available in languages which allow nesting, either by boxes or nested arrays, so it is not present in early APLs such as APL\360 and APL.SV.

Languages Name Glyph Requirements NARS-style function axis APL2-style function axis
NARS Mix Shape Yes No
A+, SHARP <19.0 Disclose > Shape and type No No
SHARP APL Disclose > Type No No
APL2, APLX Disclose Rank No Yes
NARS2000, Kap Disclose None No No
Dyalog APL Mix or (ML) None Yes Yes
J Open > Type No No
ngn/apl Mix None No No
dzaima/APL Merge Rank No No
BQN Merge > Shape No No

External links

Lessons

Documentation

References


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector