Split: Difference between revisions
m (Text replacement - "http://help.dyalog.com" to "https://help.dyalog.com") |
m (Text replacement - "</source>" to "</syntaxhighlight>") |
||
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>↓</ | {{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>↓</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</ | 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</syntaxhighlight> is <source lang=apl inline>¯1+≢⍴Y</syntaxhighlight> (original rank minus 1), its [[shape]] is <source lang=apl inline>(K≠⍳≢⍴Y)/⍴Y</syntaxhighlight> (original shape with K-th axis removed), and the shape of each element is <source lang=apl inline>(⍴Y)[K]</syntaxhighlight>. | ||
<source lang=apl> | <source lang=apl> | ||
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 | ||
</ | </syntaxhighlight>{{Works in|[[Dyalog APL]]}} | ||
Split is a no-op to a [[scalar]]. | Split is a no-op to a [[scalar]]. | ||
Line 44: | Line 44: | ||
2≡↓2 | 2≡↓2 | ||
1 | 1 | ||
</ | </syntaxhighlight> | ||
== Alternatives == | == Alternatives == | ||
Most dialects do not have Split. Instead, they can use [[Enclose]] (<source lang=apl inline>⊂</ | Most dialects do not have Split. Instead, they can use [[Enclose]] (<source lang=apl inline>⊂</syntaxhighlight>) with [[bracket axis]] or the [[Rank operator]]: | ||
<source lang=apl> | <source lang=apl> | ||
↓Y | ↓Y | ||
Line 67: | Line 67: | ||
│MNOP│QRST│UVWX│ | │MNOP│QRST│UVWX│ | ||
└────┴────┴────┘ | └────┴────┴────┘ | ||
</ | </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</ | 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</syntaxhighlight> or <source lang=apl inline>⊂⍤¯1⊢Y</syntaxhighlight>: | ||
<source lang=apl> | <source lang=apl> | ||
⊂[1↓⍳≢⍴Y]Y | ⊂[1↓⍳≢⍴Y]Y | ||
Line 82: | Line 82: | ||
│IJKL│UVWX│ | │IJKL│UVWX│ | ||
└────┴────┘ | └────┴────┘ | ||
</ | </syntaxhighlight> | ||
== External links == | == External links == |
Revision as of 10:24, 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 <source 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
The result of Split on a non-scalar array is always a nested array whose elements are vectors. The rank of <source lang=apl inline>↓[K]Y</syntaxhighlight> is <source lang=apl inline>¯1+≢⍴Y</syntaxhighlight> (original rank minus 1), its shape is <source lang=apl inline>(K≠⍳≢⍴Y)/⍴Y</syntaxhighlight> (original shape with K-th axis removed), and the shape of each element is <source lang=apl inline>(⍴Y)[K]</syntaxhighlight>.
<source lang=apl>
⎕←Y←2 3 4⍴⎕A ⍝ 3D array
ABCD EFGH IJKL
MNOP QRST UVWX
↓Y ⍝ Last axis split; 2×3 array of length-4 vectors
┌────┬────┬────┐ │ABCD│EFGH│IJKL│ ├────┼────┼────┤ │MNOP│QRST│UVWX│ └────┴────┴────┘
↓[2]Y ⍝ 2nd axis split; 2×4 array of length-3 vectors
┌───┬───┬───┬───┐ │AEI│BFJ│CGK│DHL│ ├───┼───┼───┼───┤ │MQU│NRV│OSW│PTX│ └───┴───┴───┴───┘
↓↓Y ⍝ Split twice
┌────────────────┬────────────────┐ │┌────┬────┬────┐│┌────┬────┬────┐│ ││ABCD│EFGH│IJKL│││MNOP│QRST│UVWX││ │└────┴────┴────┘│└────┴────┴────┘│ └────────────────┴────────────────┘
(≡Y)(≢⍴Y) ⍝ Original array is depth 1, rank 3
1 3
(≡↓Y)(≢⍴↓Y) ⍝ Split array is depth 1+1, rank 3-1
2 2
</syntaxhighlight>
Split is a no-op to a scalar.
<source lang=apl>
2≡↓2
1 </syntaxhighlight>
Alternatives
Most dialects do not have Split. Instead, they can use Enclose (<source lang=apl inline>⊂</syntaxhighlight>) with bracket axis or the Rank operator: <source lang=apl>
↓Y
┌────┬────┬────┐ │ABCD│EFGH│IJKL│ ├────┼────┼────┤ │MNOP│QRST│UVWX│ └────┴────┴────┘
⊂[3]Y
┌────┬────┬────┐ │ABCD│EFGH│IJKL│ ├────┼────┼────┤ │MNOP│QRST│UVWX│ └────┴────┴────┘
⊂⍤1⊢Y
┌────┬────┬────┐ │ABCD│EFGH│IJKL│ ├────┼────┼────┤ │MNOP│QRST│UVWX│ └────┴────┴────┘ </syntaxhighlight> 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 <source lang=apl inline>⊂[1↓⍳≢⍴Y]Y</syntaxhighlight> or <source lang=apl inline>⊂⍤¯1⊢Y</syntaxhighlight>: <source lang=apl>
⊂[1↓⍳≢⍴Y]Y
┌────┬────┐ │ABCD│MNOP│ │EFGH│QRST│ │IJKL│UVWX│ └────┴────┘
⊂⍤¯1⊢Y
┌────┬────┐ │ABCD│MNOP│ │EFGH│QRST│ │IJKL│UVWX│ └────┴────┘ </syntaxhighlight>
External links
Lessons
Documentation