Agreement

From J Wiki
< Help(Redirected from Help/Primer/Agreement)
Jump to navigation Jump to search


<=   =>

For a dyad the left rank of the verb and the rank of the left argument determine the frame of the left argument. Similarly the right rank of the verb and the rank of the right argument determine the frame of the right argument. If the left and right frames are the same, then there are the same number of cells in each argument, and it is simply a matter of taking each cell in turn from the left and right arguments, applying the verb, and putting the result into the frame of the result.

   a =. i. 2 3
   b =. 2 3 $ 7
   a + b
 7  8  9
10 11 12


Visually you can see how each atom from the left is used with the corresponding atom from the right.

0 1 2   +   7 7 7   gives   7  8  9
3 4 5       7 7 7          10 11 12


You have also seen that the following works.

   a + 7
 7  8  9
10 11 12


Visually you can see how each atom from the left is used with the corresponding atom from the right.

0 1 2   +   7 ...      gives   7  8  9
3 4 5       ...               10 11 12


The ... indicates that the cell is repeated to provide the required arguments. The ... to the right and below the 7 indicates it is repeated in 2 axes.

But what about the following?

   a + 3 4
3 4 5
7 8 9


Again you can see how the cells of the right argument repeat to provide the required verb arguments.

0 1 2   +   3 ...      gives    3 4 5
3 4 5       4 ...               7 8 9


But there must be some agreement between the cells in the arguments.

   a + 3 4 5
|length error
|   a    +3 4 5


Visually what is happening:

0 1 2   +   3 ...      gives    3 4 5
3 4 5       4 ...               7 8 9
            5 ...	error - ran out of lefts


The above cases are simple enough, but consider the following with a rank 3 noun.

   b =. i. 2 3 4
   b + a
 0  1  2  3
 5  6  7  8
10 11 12 13

15 16 17 18
20 21 22 23
25 26 27 28


This is more complicated to visualize.

 0  1  2  3   +   0 ...   gives   0  1  2  3
 4  5  6  7       1 ...           5  6  7  8
 8  9 10 11       2 ...          10 11 12 13

12 13 14 15       3 ...          15 16 17 18
16 17 18 19       4 ...          20 21 22 23
20 21 22 23       5 ...          25 26 27 28


Similarly:

   b + 2 3
 2  3  4  5
 6  7  8  9
10 11 12 13

15 16 17 18
19 20 21 22
23 24 25 26


Visually:

 0  1  2  3   +   2 ...   gives  2  3  4  5
 4  5  6  7       ...            6  7  8  9
 8  9 10 11                     10 11 12 13

12 13 14 15       3 ...         15 16 17 18
16 17 18 19       ...           19 20 21 22
20 21 22 23                     23 24 25 26


The agreement rule is quite simple. If the left and right frames are the same then there is no problem. Otherwise, one frame must be a prefix of the other, and its cells are repeated into its trailing axes to provide the required arguments.

<=   =>

Primer Index               Hover to reveal titles   -   Click to access   -   Current page is highlighted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
45 46 47 48
50 51 52 53 54 55 56 57
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
97 98 99 100 101 102 103 104 105 106