Essays/Negative Rank

From J Wiki
Jump to navigation Jump to search

A negative rank specifies the ranks of the cells relative to the rank of the array. Thus the rank _1 cells (or _1-cells) of a table are the rows and its _2-cells are the atoms. The _1-cells are also called items. Many J primitives are defined in terms of items.

Some examples of using negative ranks:

0. On the  \. page of the dictionary (also on the green Jsoftware mug) there is a phrase 1&(|:\.)"2^:2 that computes the minors of a matrix. The result is a 4-dimensional array. Boxing the _2-cells gives a much more intelligible display.

   ] m=: 3 3$'abcdefghi'
abc
def
ghi
   $ 1&(|:\.)"2^:2 m
3 3 2 2
   <"(_2) 1&(|:\.)"2^:2 m
┌──┬──┬──┐
│ef│df│de│
│hi│gi│gh│
├──┼──┼──┤
│bc│ac│ab│
│hi│gi│gh│
├──┼──┼──┤
│bc│ac│ab│
│ef│df│de│
└──┴──┴──┘

1. The 2 2 non-singular boolean matrices form a group under boolean matrix multiplication. Again, its group table is made more intelligible by boxing the _2-cells. In this case, boxing the 2-cells would have worked just as well, but boxing the _2-cells works for any group table. See Cayley's Theorem for further illustration of this point.

   det=: -/ .*

   y=: 2 2 $"1 #: i.16      NB. all boolean matrices
   $y
16 2 2

   y1=: y #~ 0~:det y       NB. all non-singular boolean matrices
   <"_1 y1
┌───┬───┬───┬───┬───┬───┐
│0 1│0 1│1 0│1 0│1 1│1 1│
│1 0│1 1│0 1│1 1│0 1│1 0│
└───┴───┴───┴───┴───┴───┘

   y2=: y1 \: 2=i.#y1       NB. re-order so that the identity is first
   <"2 y2
┌───┬───┬───┬───┬───┬───┐
│1 0│0 1│0 1│1 0│1 1│1 1│
│0 1│1 0│1 1│1 1│0 1│1 0│
└───┴───┴───┴───┴───┴───┘

   x=: ~:/ .*. " 2          NB. boolean matrix multiplication

   $ x/~ y2
6 6 2 2
   <"_2 x/~ y2
┌───┬───┬───┬───┬───┬───┐
│1 0│0 1│0 1│1 0│1 1│1 1│
│0 1│1 0│1 1│1 1│0 1│1 0│
├───┼───┼───┼───┼───┼───┤
│0 1│1 0│1 1│1 1│0 1│1 0│
│1 0│0 1│0 1│1 0│1 1│1 1│
├───┼───┼───┼───┼───┼───┤
│0 1│1 0│1 1│1 1│0 1│1 0│
│1 1│1 1│1 0│0 1│1 0│0 1│
├───┼───┼───┼───┼───┼───┤
│1 0│0 1│0 1│1 0│1 1│1 1│
│1 1│1 1│1 0│0 1│1 0│0 1│
├───┼───┼───┼───┼───┼───┤
│1 1│1 1│1 0│0 1│1 0│0 1│
│0 1│1 0│1 1│1 1│0 1│1 0│
├───┼───┼───┼───┼───┼───┤
│1 1│1 1│1 0│0 1│1 0│0 1│
│1 0│0 1│0 1│1 0│1 1│1 1│
└───┴───┴───┴───┴───┴───┘

   y2 i. x/~ y2
0 1 2 3 4 5
1 0 4 5 2 3
2 3 5 4 1 0
3 2 1 0 5 4
4 5 3 2 0 1
5 4 0 1 3 2



See also



Contributed by Roger Hui.