Split: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Text replacement - "http://help.dyalog.com" to "https://help.dyalog.com")
m (Text replacement - "<source" to "<syntaxhighlight")
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
{{Built-in|Split|↓}} is a [[monadic]] [[primitive function]] which reduces the [[rank]] of its [[argument]] by converting one of its [[axis|axes]] to one level of [[nested array model|nesting]]. The axis to move defaults to the last axis, but a different axis can be chosen using [[function axis]]. It shares its [[glyph]] <source lang=apl inline>↓</source> with the dyadic function [[Drop]]. Split is the [[inverse]] of [[Mix]] in the sense that the latter undoes the enclosing that Split introduced.
{{Built-in|Split|↓}} is a [[monadic]] [[primitive function]] which reduces the [[rank]] of its [[argument]] by converting one of its [[axis|axes]] to one level of [[nested array model|nesting]]. The axis to move defaults to the last axis, but a different axis can be chosen using [[function axis]]. It shares its [[glyph]] <syntaxhighlight lang=apl inline>↓</syntaxhighlight> with the dyadic function [[Drop]]. Split is the [[inverse]] of [[Mix]] in the sense that the latter undoes the enclosing that Split introduced.


== Examples ==
== Examples ==


The result of Split on a non-[[scalar]] array is always a [[nested array]] whose elements are [[vector|vectors]]. The [[rank]] of <source lang=apl inline>↓[K]Y</source> is <source lang=apl inline>¯1+≢⍴Y</source> (original rank minus 1), its [[shape]] is <source lang=apl inline>(K≠⍳≢⍴Y)/⍴Y</source> (original shape with K-th axis removed), and the shape of each element is <source lang=apl inline>(⍴Y)[K]</source>.
The result of Split on a non-[[scalar]] array is always a [[nested array]] whose elements are [[vector|vectors]]. The [[rank]] of <syntaxhighlight lang=apl inline>↓[K]Y</syntaxhighlight> is <syntaxhighlight lang=apl inline>¯1+≢⍴Y</syntaxhighlight> (original rank minus 1), its [[shape]] is <syntaxhighlight lang=apl inline>(K≠⍳≢⍴Y)/⍴Y</syntaxhighlight> (original shape with K-th axis removed), and the shape of each element is <syntaxhighlight lang=apl inline>(⍴Y)[K]</syntaxhighlight>.


<source lang=apl>
<syntaxhighlight lang=apl>
       ⎕←Y←2 3 4⍴⎕A  ⍝ 3D array
       ⎕←Y←2 3 4⍴⎕A  ⍝ 3D array
ABCD
ABCD
Line 37: Line 37:
       (≡↓Y)(≢⍴↓Y)  ⍝ Split array is depth 1+1, rank 3-1
       (≡↓Y)(≢⍴↓Y)  ⍝ Split array is depth 1+1, rank 3-1
2 2
2 2
</source>{{Works in|[[Dyalog APL]]}}
</syntaxhighlight>{{Works in|[[Dyalog APL]]}}


Split is a no-op to a [[scalar]].
Split is a no-op to a [[scalar]].


<source lang=apl>
<syntaxhighlight lang=apl>
       2≡↓2
       2≡↓2
1
1
</source>
</syntaxhighlight>


== Alternatives ==
== Alternatives ==
Most dialects do not have Split. Instead, they can use [[Enclose]] (<source lang=apl inline>⊂</source>) with [[bracket axis]] or the [[Rank operator]]:
Most dialects do not have Split. Instead, they can use [[Enclose]] (<syntaxhighlight lang=apl inline>⊂</syntaxhighlight>) with [[bracket axis]] or the [[Rank operator]]:
<source lang=apl>
<syntaxhighlight lang=apl>
       ↓Y
       ↓Y
┌────┬────┬────┐
┌────┬────┬────┐
Line 67: Line 67:
│MNOP│QRST│UVWX│
│MNOP│QRST│UVWX│
└────┴────┴────┘
└────┴────┴────┘
</source>
</syntaxhighlight>
It is common to split a higher-[[rank]] array into its constituent [[major cell|major cells]]. The behaviour of Split on matrices might mislead to the belief that this is what the primitive does. However, it isn't so for vectors or arrays of higher rank than 2. Instead, the solution is to use or <source lang=apl inline>⊂[1↓⍳≢⍴Y]Y</source> or <source lang=apl inline>⊂⍤¯1⊢Y</source>:
It is common to split a higher-[[rank]] array into its constituent [[major cell|major cells]]. The behaviour of Split on matrices might mislead to the belief that this is what the primitive does. However, it isn't so for vectors or arrays of higher rank than 2. Instead, the solution is to use or <syntaxhighlight lang=apl inline>⊂[1↓⍳≢⍴Y]Y</syntaxhighlight> or <syntaxhighlight lang=apl inline>⊂⍤¯1⊢Y</syntaxhighlight>:
<source lang=apl>
<syntaxhighlight lang=apl>
       ⊂[1↓⍳≢⍴Y]Y
       ⊂[1↓⍳≢⍴Y]Y
┌────┬────┐
┌────┬────┐
Line 82: Line 82:
│IJKL│UVWX│
│IJKL│UVWX│
└────┴────┘
└────┴────┘
</source>
</syntaxhighlight>


== External links ==
== External links ==

Latest revision as of 10:55, 11 September 2022

Split () is a monadic primitive function which reduces the rank of its argument by converting one of its axes to one level of nesting. The axis to move defaults to the last axis, but a different axis can be chosen using function axis. It shares its glyph with the dyadic function Drop. Split is the inverse of Mix in the sense that the latter undoes the enclosing that Split introduced.

Examples

The result of Split on a non-scalar array is always a nested array whose elements are vectors. The rank of [K]Y is ¯1+≢⍴Y (original rank minus 1), its shape is (K≠⍳≢⍴Y)/Y (original shape with K-th axis removed), and the shape of each element is (Y)[K].

      Y2 3 4⎕A  ⍝ 3D array
ABCD
EFGH
IJKL
    
MNOP
QRST
UVWX
      Y  ⍝ Last axis split; 2×3 array of length-4 vectors
┌────┬────┬────┐
ABCDEFGHIJKL
├────┼────┼────┤
MNOPQRSTUVWX
└────┴────┴────┘
      [2]Y  ⍝ 2nd axis split; 2×4 array of length-3 vectors
┌───┬───┬───┬───┐
AEIBFJCGKDHL
├───┼───┼───┼───┤
MQUNRVOSWPTX
└───┴───┴───┴───┘
      ↓↓Y  ⍝ Split twice
┌────────────────┬────────────────┐
│┌────┬────┬────┐│┌────┬────┬────┐│
││ABCDEFGHIJKL│││MNOPQRSTUVWX││
│└────┴────┴────┘│└────┴────┴────┘│
└────────────────┴────────────────┘

      (Y)(≢⍴Y)  ⍝ Original array is depth 1, rank 3
1 3
      (≡↓Y)(≢⍴↓Y)  ⍝ Split array is depth 1+1, rank 3-1
2 2
Works in: Dyalog APL

Split is a no-op to a scalar.

      2≡↓2
1

Alternatives

Most dialects do not have Split. Instead, they can use Enclose () with bracket axis or the Rank operator:

      Y
┌────┬────┬────┐
ABCDEFGHIJKL
├────┼────┼────┤
MNOPQRSTUVWX
└────┴────┴────┘
      [3]Y
┌────┬────┬────┐
ABCDEFGHIJKL
├────┼────┼────┤
MNOPQRSTUVWX
└────┴────┴────┘
      1Y
┌────┬────┬────┐
ABCDEFGHIJKL
├────┼────┼────┤
MNOPQRSTUVWX
└────┴────┴────┘

It is common to split a higher-rank array into its constituent major cells. The behaviour of Split on matrices might mislead to the belief that this is what the primitive does. However, it isn't so for vectors or arrays of higher rank than 2. Instead, the solution is to use or [1↓⍳≢⍴Y]Y or ¯1Y:

      [1↓⍳≢⍴Y]Y
┌────┬────┐
ABCDMNOP
EFGHQRST
IJKLUVWX
└────┴────┘
      ¯1Y
┌────┬────┐
ABCDMNOP
EFGHQRST
IJKLUVWX
└────┴────┘

External links

Lessons

Documentation


APL built-ins [edit]
Primitive functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentitySelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndex
Selector Index generatorGradeIndex OfInterval IndexIndicesDeal
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Primitive operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductPowerAtUnderRankDepthVariantStencilCut (J)
Quad names
Arrays Index originMigration levelAtomic vector
Functions Name classCase convertUnicode convert
Operators SearchReplace