JPhrases/Grids

From J Wiki
Jump to navigation Jump to search

8B. Grids

m0=: i. First y non-negative integers
m1=: Ai=: >:@i. Augmented integers (begin at 1)
m2=: Ei=: i.@>: Extended integers (0 to y inclusive)
m3=: Si=: -Ei@+: Symmetric integers (-y to y inclusive)
m4=: grid=: +`(*i.)/ General grid (2{y steps from 0{y by 1{y)
m5=: cb=: ] {. 1 _1"_ $~ >: - 2&| Checkerboard. Try box cb 2 3 4 5
m6=: box=: <"2 Box rank 2 cells (for readable display)
m7=: CB=: *cb@$ Alternate signs of atoms of y.
m8=: bcb=: _1&=@cb Boolean checkerboard
m9=: {. 0 1"_ $~ ] + 2&|@>: "
m10=: _1: ^ m9 Checkerboard
a11=: syft=: (/~)(@Si) Symmetric function tables
v12=: WRAP=: wrap : (wrap@]^:[) Wrap (monad) and repeated wrap (dyad)
m13=: wrap=: RL@(,.(next+i.@#))^:4 Wrap argument with successive integers
m14=: RL=: |.@|: Roll table to left
m15=: next=: >:@(>./)@, Next integer after largest in table

The first four phrases give commonly useful integer lists, and the fifth allows specification of the beginning value, the step size, and the number of steps. For example:

   (m0 ; Ai ; Ei ; Si) 3
+-----+-----+-------+----------------+
|0 1 2|1 2 3|0 1 2 3|3 2 1 0 _1 _2 _3|
+-----+-----+-------+----------------+

   (*/~ ; !/~) Si 3
+-------------------+-----------------+
| 9  6  3 0 _3 _6 _9|1 0 0 0 _1 _4 _10|
| 6  4  2 0 _2 _4 _6|3 1 0 0  1  3   6|
| 3  2  1 0 _1 _2 _3|3 2 1 0 _1 _2  _3|
| 0  0  0 0  0  0  0|1 1 1 1  1  1   1|
|_3 _2 _1 0  1  2  3|0 0 0 0  1  0   0|
|_6 _4 _2 0  2  4  6|0 0 0 0 _1  1   0|
|_9 _6 _3 0  3  6  9|0 0 0 0  1 _2   1|
+-------------------+-----------------+

   grid 3 4 5
3 7 11 15 19

Phrases 5-10 give various checkerboard patterns. Note the alternation along each axis:

   box cb 2 3 4 5
+--------------+--------------+--------------+
| 1 _1  1 _1  1|_1  1 _1  1 _1| 1 _1  1 _1  1|
|_1  1 _1  1 _1| 1 _1  1 _1  1|_1  1 _1  1 _1|
| 1 _1  1 _1  1|_1  1 _1  1 _1| 1 _1  1 _1  1|
|_1  1 _1  1 _1| 1 _1  1 _1  1|_1  1 _1  1 _1|
+--------------+--------------+--------------+
|_1  1 _1  1 _1| 1 _1  1 _1  1|_1  1 _1  1 _1|
| 1 _1  1 _1  1|_1  1 _1  1 _1| 1 _1  1 _1  1|
|_1  1 _1  1 _1| 1 _1  1 _1  1|_1  1 _1  1 _1|
| 1 _1  1 _1  1|_1  1 _1  1 _1| 1 _1  1 _1  1|
+--------------+--------------+--------------+
   CB i. 3 4
 0 _1  2  _3
_4  5 _6   7
 8 _9 10 _11

Phrase 11 is an adverb that produces function tables on symmetric arguments:

   (+syft ; *syft ; !syft) 3
+-------------------+-------------------+-----------------+
|6  5  4  3  2  1  0| 9  6  3 0 _3 _6 _9|1 0 0 0 _1 _4 _10|
|5  4  3  2  1  0 _1| 6  4  2 0 _2 _4 _6|3 1 0 0  1  3   6|
|4  3  2  1  0 _1 _2| 3  2  1 0 _1 _2 _3|3 2 1 0 _1 _2  _3|
|3  2  1  0 _1 _2 _3| 0  0  0 0  0  0  0|1 1 1 1  1  1   1|
|2  1  0 _1 _2 _3 _4|_3 _2 _1 0  1  2  3|0 0 0 0  1  0   0|
|1  0 _1 _2 _3 _4 _5|_6 _4 _2 0  2  4  6|0 0 0 0 _1  1   0|
|0 _1 _2 _3 _4 _5 _6|_9 _6 _3 0  3  6  9|0 0 0 0  1 _2   1|
+-------------------+-------------------+-----------------+

The final phrases produce spirals by wrapping a table (or by default a list or scalar) in a blanket of successive integers:

   ]t=: i. 2 3
0 1 2
3 4 5

   (RL t) ; (WRAP 0) ; (3 WRAP 0)
+---+-----+--------------------+
|2 5|6 7 8|42 43 44 45 46 47 48|
|1 4|5 0 1|41 20 21 22 23 24 25|
|0 3|4 3 2|40 19  6  7  8  9 26|
|   |     |39 18  5  0  1 10 27|
|   |     |38 17  4  3  2 11 28|
|   |     |37 16 15 14 13 12 29|
|   |     |36 35 34 33 32 31 30|
+---+-----+--------------------+