The Cut Operator The text and examples assume ⎕io←1 .
The cut operator works in either index-origin.
0. Definition Cut was defined by Iverson in [0, 1] and refined in J [2]; it is a dyadic operator whose left operand is a function and whose right operand is an integer scalar encoding the kind of cut. It has not been decided what glyph would be used to denote cut. For the nonce we use Cut . The following definitions are adapted from the J Dictionary [2]. 0.1 Monadic Cases f Cut 0 ⊢y applies f to y after reversing y along each axis; it is equivalent to (⍉⎕io,⍪-⍴y) f Cut 0 ⊢y . The fret 1⌷y (the leading major cell of y) marks the start of an interval of major cells of y ; the phrase f Cut 1 ⊢y applies f to each such interval. The phrase f Cut ¯1 ⊢y differs only in that frets are excluded from the result. In f Cut 2 and f Cut ¯2 the fret is the last major cell, and marks the ends of intervals. The monads f Cut 3 and f Cut ¯3 apply f to tessellation by “maximal cubes”, that is, they are defined by their dyadic cases using the left argument (⍴⍴y)⍴⌊/⍴y . 0.2 Dyadic Cases x f Cut 0 ⊢y applies f to a rectangle or cuboid of y with one vertex at the point in y indexed by v←1⌷x , and with the opposite vertex determined as follows: the dimension is |2⌷x , but the rectangle extends back from v along any axis j for which the index j⌷v is negative. Finally, the order of the selected cells is reversed along each axis k for which k⌷2⌷x is negative. If x is a vector or scalar, it is treated as the matrix ⍉⎕io,⍪x . The frets in the dyadic cases 1 , ¯1 , 2 , and ¯2 are determined by the 1s in boolean vector x ; an empty vector x and non-zero ≢y indicate the entire of y . If x is the scalar 0 or 1 it is treated as (≢y)⍴x . In general, boolean vector j⊃x specifies how axis j is to be cut, with a scalar treated as (j⌷⍴y)⍴j⊃x . f Cut 3 and f Cut ¯3 yield (possibly overlapping)
tessellations. x f Cut ¯3 ⊢y
applies f to each complete rectangle
of size |2⌷x
beginning at integer multiples of (each scalar of)
the movement vector 1⌷x .
As in f Cut 0 ,
reversal occurs along each axis for which the size 2⌷x is negative.
The case of a vector or scalar x is equivalent to ⍉1,⍪x ,
and therefore provides a complete tessellation of size x .
The case f Cut 3 differs in that shards
of length less than |2⌷x are included.
1. Examples It would be instructive to figure out what each example does before reading its annotation. 1.1 0-Cut The 0-cut selects rectangular subarrays, reversing each axis for which the specified length is negative. ⎕←x←4 10⍴⍳40 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 (⍉2 3,⍪3 4) ⊂Cut 0 ⊢x ⍝ a1.0 ┌───────────┐ │13 14 15 16│ │23 24 25 26│ │33 34 35 36│ └───────────┘ (⍉2 3,⍪3 ¯4) ⊂Cut 0 ⊢x ⍝ a1.1 ┌───────────┐ │16 15 14 13│ │26 25 24 23│ │36 35 34 33│ └───────────┘ 3 ¯4 ⊂Cut 0 ⊢x ⍝ a1.2 ┌───────────┐ │ 4 3 2 1│ │14 13 12 11│ │24 23 22 21│ └───────────┘ ⊢Cut 0 ⊢x ⍝ a1.3 40 39 38 37 36 35 34 33 32 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 (⍉2 ¯2,⍪3 6) ⊢Cut 0 ⊢x ⍝ a1.4 14 15 16 17 18 19 24 25 26 27 28 29 34 35 36 37 38 39 1.2 ±1-Cut and ±2-Cut The 1- and 2-cut partition according to a fret, 1s in a boolean left argument in the dyadic case or the leading major cell of the right argument in the monadic case. Frets specify the starts of partitions in the 1-cut and the ends in the 2-cut. The ¯1- and ¯2-cuts differ in excluding the frets. ⊂Cut 1 ⊢' Cogito, ergo sum.' ⍝ a2.0 ┌────────┬─────┬─────┐ │ Cogito,│ ergo│ sum.│ └────────┴─────┴─────┘ {⊂⌽⍵}Cut 1 ⊢' Cogito, ergo sum.' ⍝ a2.1 ┌────────┬─────┬─────┐ │,otigoC │ogre │.mus │ └────────┴─────┴─────┘ ⊂Cut ¯1 ⊢' Cogito, ergo sum.' ⍝ a2.2 ┌───────┬────┬────┐ │Cogito,│ergo│sum.│ └───────┴────┴────┘ b←1 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 b ⊂Cut 1 ⊢' Cogito, ergo sum.' ⍝ a2.3 ┌────────┬─────┬─────┐ │ Cogito,│ ergo│ sum.│ └────────┴─────┴─────┘ ⊢Cut 1 ⊢' Cogito, ergo sum.' ⍝ a2.4 Cogito, ergo sum. ⊢Cut ¯1 ⊢' Cogito, ergo sum.' ⍝ a2.5 Cogito, ergo sum. ≢Cut 1⍨ 1 0 1 0 0 ⍝ a2.6 2 3 ≢Cut ¯1⍨ 1 0 1 0 0 ⍝ a2.7 1 2 ⊂Cut 2 ⊢'Cogito,/ergo/sum./' ⍝ a2.8 ┌────────┬─────┬─────┐ │Cogito,/│ergo/│sum./│ └────────┴─────┴─────┘ 1 0 1 0 0 1 0 0 ⊂Cut 2 ⊢'chthonic' ⍝ a2.9 ┌─┬──┬───┐ │c│ht│hon│ └─┴──┴───┘ 1 0 1 0 0 1 0 0 +⌿Cut 2 ⊢8 3⍴⍳24 ⍝ a2.10 1 2 3 11 13 15 39 42 45 x←5 3⍴⍳15 1 0 1 0 0 ⊂Cut 1 ⊢x ⍝ a2.11 ┌─────┬────────┐ │1 2 3│ 7 8 9│ │4 5 6│10 11 12│ │ │13 14 15│ └─────┴────────┘ (1 0 1 0 0) (1 1 0) ⊂Cut 1 ⊢x ⍝ a2.12 ┌──┬─────┐ │1 │2 3 │ │4 │5 6 │ ├──┼─────┤ │ 7│ 8 9│ │10│11 12│ │13│14 15│ └──┴─────┘ ⍬ (1 1 0) ⊂Cut 1 ⊢x ⍝ a2.13 ┌──┬─────┐ │ 1│ 2 3│ │ 4│ 5 6│ │ 7│ 8 9│ │10│11 12│ │13│14 15│ └──┴─────┘ ⍬ ⊂Cut 1 ⊢x ⍝ a2.14 ┌────────┐ │ 1 2 3│ │ 4 5 6│ │ 7 8 9│ │10 11 12│ │13 14 15│ └────────┘ 1.3 ±3-Cut The 3-cut provides tessellation. The ¯3-cut differs in excluding shards, cells less than the full size. ⎕←x←5 7⍴⍳35 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 (2 2⍴2 1 3 2) ⊂Cut 3 ⊢x ⍝ a3.0 ┌─────┬─────┬─────┬─────┬─────┬─────┬──┐ │ 1 2│ 2 3│ 3 4│ 4 5│ 5 6│ 6 7│ 7│ │ 8 9│ 9 10│10 11│11 12│12 13│13 14│14│ │15 16│16 17│17 18│18 19│19 20│20 21│21│ ├─────┼─────┼─────┼─────┼─────┼─────┼──┤ │15 16│16 17│17 18│18 19│19 20│20 21│21│ │22 23│23 24│24 25│25 26│26 27│27 28│28│ │29 30│30 31│31 32│32 33│33 34│34 35│35│ ├─────┼─────┼─────┼─────┼─────┼─────┼──┤ │29 30│30 31│31 32│32 33│33 34│34 35│35│ └─────┴─────┴─────┴─────┴─────┴─────┴──┘ (2 2⍴2 1 ¯3 2) ⊂Cut 3 ⊢x ⍝ a3.1 ┌─────┬─────┬─────┬─────┬─────┬─────┬──┐ │15 16│16 17│17 18│18 19│19 20│20 21│21│ │ 8 9│ 9 10│10 11│11 12│12 13│13 14│14│ │ 1 2│ 2 3│ 3 4│ 4 5│ 5 6│ 6 7│ 7│ ├─────┼─────┼─────┼─────┼─────┼─────┼──┤ │29 30│30 31│31 32│32 33│33 34│34 35│35│ │22 23│23 24│24 25│25 26│26 27│27 28│28│ │15 16│16 17│17 18│18 19│19 20│20 21│21│ ├─────┼─────┼─────┼─────┼─────┼─────┼──┤ │29 30│30 31│31 32│32 33│33 34│34 35│35│ └─────┴─────┴─────┴─────┴─────┴─────┴──┘ (2 2⍴2 1 3 ¯2) ⊂Cut 3 ⊢x ⍝ a3.2 ┌─────┬─────┬─────┬─────┬─────┬─────┬──┐ │ 2 1│ 3 2│ 4 3│ 5 4│ 6 5│ 7 6│ 7│ │ 9 8│10 9│11 10│12 11│13 12│14 13│14│ │16 15│17 16│18 17│19 18│20 19│21 20│21│ ├─────┼─────┼─────┼─────┼─────┼─────┼──┤ │16 15│17 16│18 17│19 18│20 19│21 20│21│ │23 22│24 23│25 24│26 25│27 26│28 27│28│ │30 29│31 30│32 31│33 32│34 33│35 34│35│ ├─────┼─────┼─────┼─────┼─────┼─────┼──┤ │30 29│31 30│32 31│33 32│34 33│35 34│35│ └─────┴─────┴─────┴─────┴─────┴─────┴──┘ ¯3 2 ⊂Cut 3 ⊢x ⍝ a3.3 ┌─────┬─────┬─────┬─────┬─────┬─────┬──┐ │15 16│16 17│17 18│18 19│19 20│20 21│21│ │ 8 9│ 9 10│10 11│11 12│12 13│13 14│14│ │ 1 2│ 2 3│ 3 4│ 4 5│ 5 6│ 6 7│ 7│ ├─────┼─────┼─────┼─────┼─────┼─────┼──┤ │22 23│23 24│24 25│25 26│26 27│27 28│28│ │15 16│16 17│17 18│18 19│19 20│20 21│21│ │ 8 9│ 9 10│10 11│11 12│12 13│13 14│14│ ├─────┼─────┼─────┼─────┼─────┼─────┼──┤ │29 30│30 31│31 32│32 33│33 34│34 35│35│ │22 23│23 24│24 25│25 26│26 27│27 28│28│ │15 16│16 17│17 18│18 19│19 20│20 21│21│ ├─────┼─────┼─────┼─────┼─────┼─────┼──┤ │29 30│30 31│31 32│32 33│33 34│34 35│35│ │22 23│23 24│24 25│25 26│26 27│27 28│28│ ├─────┼─────┼─────┼─────┼─────┼─────┼──┤ │29 30│30 31│31 32│32 33│33 34│34 35│35│ └─────┴─────┴─────┴─────┴─────┴─────┴──┘ ¯3 2 ⊂Cut ¯3 ⊢x ⍝ a3.4 ┌─────┬─────┬─────┬─────┬─────┬─────┐ │15 16│16 17│17 18│18 19│19 20│20 21│ │ 8 9│ 9 10│10 11│11 12│12 13│13 14│ │ 1 2│ 2 3│ 3 4│ 4 5│ 5 6│ 6 7│ ├─────┼─────┼─────┼─────┼─────┼─────┤ │22 23│23 24│24 25│25 26│26 27│27 28│ │15 16│16 17│17 18│18 19│19 20│20 21│ │ 8 9│ 9 10│10 11│11 12│12 13│13 14│ ├─────┼─────┼─────┼─────┼─────┼─────┤ │29 30│30 31│31 32│32 33│33 34│34 35│ │22 23│23 24│24 25│25 26│26 27│27 28│ │15 16│16 17│17 18│18 19│19 20│20 21│ └─────┴─────┴─────┴─────┴─────┴─────┘ life←{1⊖1⌽(-⍴⍵)↑5 6 7∊⍨(3 3 ,Cut ¯3⊢⍵)+.×9⍴1,⍨4⍴2} ⍝ a3.5 b←10 10⍴0 b[2;3]←b[3;4]←b[4;2 3 4]←1 ⍝ a3.6 {'.x'[1+life⍣⍵⊢b]}¨¯1+⍳10 ⍝ a3.7 ┌──────────┬──────────┬──────────┬──────────┬──────────┬ │..........│..........│..........│..........│..........│ │..x.......│..........│..........│..........│..........│ │...x......│.x.x......│...x......│..x.......│...x......│ │.xxx......│..xx......│.x.x......│...xx.....│....x.....│ │..........│..x.......│..xx......│..xx......│..xxx.....│ │..........│..........│..........│..........│..........│ │..........│..........│..........│..........│..........│ │..........│..........│..........│..........│..........│ │..........│..........│..........│..........│..........│ │..........│..........│..........│..........│..........│ └──────────┴──────────┴──────────┴──────────┴──────────┴ ┬──────────┬──────────┬──────────┬──────────┬──────────┐ │..........│..........│..........│..........│..........│ │..........│..........│..........│..........│..........│ │..........│..........│..........│..........│..........│ │..x.x.....│....x.....│...x......│....x.....│..........│ │...xx.....│..x.x.....│....xx....│.....x....│...x.x....│ │...x......│...xx.....│...xx.....│...xxx....│....xx....│ │..........│..........│..........│..........│....x.....│ │..........│..........│..........│..........│..........│ │..........│..........│..........│..........│..........│ │..........│..........│..........│..........│..........│ ┴──────────┴──────────┴──────────┴──────────┴──────────┘ See [3] for more on the meaning of life. 1.4 Annotations
2. Model Both the model and the tests work in either index-origin. See also [4]. |
Cut←{ ⍝ Model of cut. Works in either index-origin. ⍝ Iverson, K.E., Rationalized APL, 1983; Section K. ⍝ Hui, R.K.W., Some Uses of { and }, 1987-05; Section 3.2. ⍝ Iverson, K.E., A Dictionary of APL, 1987-09; m⍤v. ⍝ Hui, R.K.W., and K.E. Iverson, J Introduction and Dictionary, 2011, Cut (;.). ⍺←(|⍵⍵){ ⍝ default left argument 0=⍺:⍉⎕io,⍪-⍴⍵ 1=⍺:⎕io=( 1↑⍵)⍳⍵ 2=⍺:⎕io=(¯1↑⍵)⍳⍵ 3=⍺:(⍴⍴⍵)⍴⌊/⍴⍵ }⍵ ⍺ ⍺⍺{⍺⍺ ⍵}{ ⎕ml←1 ⍝ needed by ⊂ Under←{⍵⍵⍣¯1⊢(⍵⍵ ⍺)⍺⍺(⍵⍵ ⍵)} ⍝ dyadic case of "under" operator p2 ←{(¯1⌽k↓⍺)⊂[⎕io+⍺⍺]((⍺⍺⍴0),k←⎕io-(⌽⍺)⍳1)↓⍵} Blk1 ←{0=≢⍺:⍵ ⋄ 0=≢⊃⍺:(1↓⍺)((1+⍺⍺)∇∇)⍵ ⋄ (1↓⍺)((1+⍺⍺)∇∇)↑(⊂⊃⍺)⊂[⎕io+⍺⍺]¨⍵} Blk2 ←{0=≢⍺:⍵ ⋄ 0=≢⊃⍺:(1↓⍺)((1+⍺⍺)∇∇)⍵ ⋄ (1↓⍺)((1+⍺⍺)∇∇)↑(⊂⊃⍺)(⍺⍺ p2) ¨⍵} ifv ←{(⍉⍺,⍪)⍣(2>≢⍴⍵)⊢⍵} ⍝ laminate ⍺ if ⍵ is vector or scalar ci ←{(0>s){⌽⍣⍺⊢⍵}¨(⍳¨|s)+(((⍴i)↑⍴⍵)|i-⎕io×0≤i)-(0>i)ׯ1+|s⊣i s←↓⎕io ifv ⍺} ti ←{{↑(⍵+⎕io)((×s)×(|s)⌊r-⍵)}¨m∘ר⎕io-⍨⍳⌈r ÷m+(0=m)×r←(⍴m)⍴⍴⍵⊣m s←↓1 ifv ⍺} tj ←{{↑(⍵+⎕io) s }¨m∘ר⎕io-⍨⍳⌈(1+r-|s)÷m+(0=m)×r←(⍴m)⍴⍴⍵⊣m s←↓1 ifv ⍺} (1=|⍵⍵)∧(⍬≡⍺)∨1≠≡⍺:↑⍺⍺¨(⊂(0>⍵⍵)× ×≢¨⍺)↓¨⍺(0 Blk1)⊂⍵ (2=|⍵⍵)∧(⍬≡⍺)∨1≠≡⍺:↑⍺⍺¨(⊂(0>⍵⍵)×-×≢¨⍺)↓¨⍺(0 Blk2)⊂⍵ 0=⍵⍵:⍺(⍺⍺ ci⌷⊢)⍵ 1=⍵⍵:↑⍺(⍺⍺ ¨⊂[⎕io])⍵ ¯1=⍵⍵:↑⍺(⍺⍺∘(1∘↓)¨⊂[⎕io])⍵ 2=⍵⍵:⍺ ⍺⍺∘⊖∇∇ 1 Under⊖ ⍵ ¯2=⍵⍵:⍺ ⍺⍺∘(¯1∘↓)∇∇ 2⊢⍵ 3=⍵⍵:↑(⍺ ti ⍵)⍺⍺ ∇∇ 0¨⊂⍵ ¯3=⍵⍵:↑(⍺ tj ⍵)⍺⍺ ∇∇ 0¨⊂⍵ *'domain error' }⍵⍵⊢⍵ } |
(The above text for Cut illustrates why having the system preserve whitespace as entered is much to be desired and why ⎕io delenda est.) 2.1 Tests assert ← {⍺←'assertion failure' ⋄ 0∊⍵:⍺ ⎕signal 8 ⋄ shy←0} z←test_cut io;⎕io;f;p;q;x ⍝ testing the cut operator ⎕io←io assert(⊂⌽3 4↑1 2↓x)≡(⍉(1 2+⎕io), ⍪3 ¯4)⊂Cut 0⊢x←4 10⍴⍳40 assert(⊂⌽3 ¯4↑1 ¯1↓x)≡(⍉((1+⎕io),¯2),⍪3 ¯4)⊂Cut 0⊢x assert(⊂⌽3 4↑x)≡3 ¯4⊂Cut 0⊢x assert'TUVWXY'≡(⍪¯2 6)⊢Cut 0⊢⎕a assert'YXWVUT'≡(⍪¯2 ¯6)⊢Cut 0⊢⎕a assert'BCDEFG'≡(⍪(1+⎕io), 6)⊢Cut 0⊢⎕a assert'GFEDCB'≡(⍪(1+⎕io),¯6)⊢Cut 0⊢⎕a assert(⊂Cut 0⊢x)≡(⍉,∘⍪-⍴x)⊂Cut 0⊢x←5 7 11⍴⍳385 assert(' foo' ' upon' ' thee')≡⊂Cut 1⊢' foo upon thee' assert('foo' 'upon' 'thee' )≡⊂Cut ¯1⊢' foo upon thee' assert 0 1 1 0 1 0 0(+⌿Cut 1≡(↑∘(+⌿¨)⊂Cut 1))7 3⍴⍳21 assert 0 1 1 0 1 0 0(+⌿Cut ¯1≡(↑∘(+⌿¨)⊂Cut ¯1))7 3⍴⍳21 assert('foo,' 'upon,' 'thee,')≡⊂Cut 2⊢'foo,upon,thee,' assert('foo' 'upon' 'thee' )≡⊂Cut ¯2⊢'foo,upon,thee,' assert{(⌈(1+r-s)÷m)≡≢(⍪m,s)⊂Cut ¯3⍳r←47⊣m s←⍵}¨∘.,⍨+\10⍴1 assert(+/∘,¨2 3⊂Cut ¯3⊢x)≡2 3(+/,)Cut ¯3⊢x←7 5⍴⍳35 assert(+/∘,¨2 3⊂Cut 3⊢x)≡2 3(+/,)Cut 3⊢x assert(⊂Cut 3⊢x)≡(⍉1,⍪(⍴⍴x)⍴⌊/⍴x)⊂Cut 3⊢x←5 7 11⍴⍳385 assert(⊂Cut ¯3⊢x)≡(⍉1,⍪(⍴⍴x)⍴⌊/⍴x)⊂Cut ¯3⊢x p←(1 0 1 0 0)(1 0 1 1 0 0 1) q←(0 1 0 0 1)(0 1 1 0 0 1 1) x←5 7⍴⍳35 assert(p ⊂Cut 1⊢x)≡⍉¨↑(⊂(1+⎕io)⊃p)⊂Cut 1¨ ⍉¨ (⎕io⊃p)⊂Cut 1⊢x assert(q ⊂Cut 2⊢x)≡p ⊂Cut 1⊢x assert(p ⊂Cut ¯1⊢x)≡ 1 1∘↓¨p ⊂Cut 1⊢x assert(q ⊂Cut ¯2⊢x)≡¯1 ¯1∘↓¨q ⊂Cut 2⊢x x←7 8⍴⍳56 q←⍬(0 0 1 1 0 1 0 0) assert(q⊂Cut 1⊢x)≡1 1 0 1 0 0 ⊂[1+⎕io] 0 2↓x q←⍬(1 0 1 0 0 1 0 0) assert(q⊂Cut 2⊢x)≡1 1 0 1 0 0 ⊂[1+⎕io] 0 ¯2↓x ⎕fx 'z←f y' 'z←⎕ml' assert(2⍴¨0 1 2 3)≡{⎕ml←⍵ ⋄ 1 0 0 1 0 f Cut 1⊢'abcde'}¨0 1 2 3 z←1 3. Special Code It is contemplated that special code would be implemented for the following cases:
References
Presented at Dyalog ’15, Naxos Beach, Sicily, 2015-09-10.
|