Vocabulary/FramingFill

From J Wiki
Jump to: navigation, search

Back to: Vocabulary

Result Assembly and Framing Fill

Argument Frame And Result Frame

All J verbs, whether monad or dyad, have a verb rank r for each argument, which gives the rank of the largest cell that the verb can operate on. Each argument is construed as an array of cells of rank r. The frame of the argument is the shape of this array of cells. If you take the shape of the argument and drop the trailing r atoms, you are left with the frame of the argument.

If the rank of the argument is less than r, it is construed as a single cell, with empty frame. A Verb with infinite rank always treats its argument as a single cell.

Sentence Argument Rank Frame Number Of Cells
<"0 'abc' abc 0 3 3
2 # 'abc' abc 1 empty 1
+: i. 2 3 0 1 2

3 4 5

0 2 3 6
2 #. i. 2 3 0 1 2

3 4 5

1 2 2
#"2 i. 2 3 0 1 2

3 4 5

2 empty 1
# i. 2 3 0 1 2

3 4 5

_ empty 1

For monads, the result frame is the frame of the argument. For dyads, each argument has a frame. The frames must agree, that is they must be identical or one must be a prefix of the other. The longer of the two argument frames is the result frame, and is also called the frame of the verb.

The verb operates on the cells individually. This produces a number of individual results.

If the frame of the verb contains a 0, the argument is said to be empty, and the verb is executed on a cell of fills.

Empty frame simply means that the verb is executed on a single argument cell. Just as a noun with empty shape is a single atom (no axes), an argument with empty frame is a single cell.

The Type And Precision Of The Overall Result

The type and precision of a noun are given by 3!:0, meaning as follows:

Result from 3!:0 1 2 4 8 16 32 64 128 1024-32768 65536 131072 262144
Type numeric character numeric numeric numeric boxed numeric numeric various symbol character character
Precision Boolean byte integer floating-point complex boxed extended integer rational sparse arrays symbol unicode unicode4

These type/precision values have the following priority when combined:

Priority Table For Result Precisions
Low Priority-> Boolean byte unicode unicode4 integer boxed extended integer rational symbol floating-point complex <-High Priority

The type/precision of the overall result depends on the types/precisions of the individual results, and on whether any individual result is non-empty.


   3!:0 (5)  NB. An integer
4
   3!:0 (1r2)   NB. A rational number
128
   > 5;1r2   NB. integer and rational joined...
5 1r2
   3!:0 > 5;1r2   NB. ... produces rational
128
   3!:0 (0.3)  NB. A floating-point value
8
   > 0.3;1r2    NB. float and rational joined...
0.3 0.5
   3!:0 > 0.3;1r2   NB. ... produces float
8
   ]a =. 'a';5    NB. character and numeric can coexist when protected by boxes...
+-+-+
|a|5|
+-+-+
   > a   NB. ...but when the contents are collected, they are incompatible
|domain error
|       >a

Rank Extension Of Individual Results

The individual results are brought up to a common rank by adding as many leading axes of length 1 as necessary to each argument.

Individual

Result 0 shape

Individual

Result 1 shape

Individual

Result 1 shape
after rank extension

2 3 2 1 2
0 0 2 1 2

Padding Of Results With Framing Fill

After the individual results have been brought up to a common rank, framing fill is added as needed to produce a common shape. This common shape is simply the largest value of the shapes of the individual results, computed for each axis individually.

It is possible for all the individual results to be empty, but the collected result to be non-empty! See examples below.

Any individual result that is smaller than the common shape has fill atoms added to the end of each short axis. The fill atom depends on the class of the overall result: space if character, a: if boxed, 0 if numeric.

Note that some verbs, such as x {. y and x , y, insert fill. They even allow you to specify the fill atom with !.f. This is not framing fill!. It is called verb fill, and is added by the operation of the verb, before results are collected. There is no way for you to specify the fill atom used for framing fill - it depends only on the overall result class.

   $ > (0$0);(0$0)   NB. No extension or fill: shapes 0 and 0
2 0
   $ > (1$0);(0$0)   NB. fill: shapes 1 and 1
2 1
   $ > (0 0$0);(0$0) NB. Extension to 0 0 and 1 0; fill to 1 0 and 1 0
2 1 0
   $ > (0 1$0);(0$0) NB. Extension to 0 1 and 1 0; fill to 1 1 and 1 1
2 1 1
   $ > (0 0$0);(1$0) NB. Extension to 0 0 and 1 1; fill to 1 1 and 1 1
2 1 1
   $ > (1 0$0);(0$0) NB. Extension to 1 0 and 1 0; no fill
2 1 0


Assembly Into The Final Result

The extended-and-filled individual results are collected into a single result. The shape of this result is the concatenation of the result frame with the shape of a single entended-and-filled result cell.

The result frame tells how many times the verb was executed on a cell. There will be a result for each cell. These results have been extended and filled to bring them to a common shape. Now these results are organized into an array of results, where the frame of the array with respect to result cells is the result frame.

This can best be understood with examples.

Suppose we are operating on a 2x2 array a:

   maketable =: ((;:'Shape Value') ,: (,&<~ $))  NB. Add header and shape to value
   maketable a =: i. 2 2
+-----+-----+
|Shape|Value|
+-----+-----+
|2 2  |0 1  |
|     |2 3  |
+-----+-----+

If we apply a verb with rank 0 to a, the frame of the verb is 2 2. The cells of a are the atoms. The verb will be applied 4 times, once for each cell, and the results will be collected into a 2x2 array of result cells.

The shape of the extended-and-filled result cell depends on the verb. The simplest case is a verb returning an atom:

   maketable +: a
+-----+-----+
|Shape|Value|
+-----+-----+
|2 2  |0 2  |
|     |4 6  |
+-----+-----+

Applying +: to an atom produced an atom with shape empty; so the shape of the result is 2 2: the frame (2 2) followed by the shape of an extended-and-filled result (empty).

   maketable <@i."0 a
+-----+-----------+
|Shape|Value      |
+-----+-----------+
|2 2  |+---+-----+|
|     ||   |0    ||
|     |+---+-----+|
|     ||0 1|0 1 2||
|     |+---+-----+|
+-----+-----------+

The verb <@i. creates an array of numbers and then puts them into a box, which is an atom. "0 applies this operation to each atom of a. The shape of each individual result is again empty, so the overall shape of the result is again 2 2.

   maketable i."0 a
+-----+-----+
|Shape|Value|
+-----+-----+
|2 2 3|0 0 0|
|     |0 0 0|
|     |     |
|     |0 1 0|
|     |0 1 2|
+-----+-----+

This time the individual results were not boxed, so they were lists, in fact the lists empty, 0, 0 1, and 0 1 2. The longest of these has length 3, so all the lists were extended and filled to shape 3. The overall result has shape 2 2 3: the frame (2 2) followed by the shape of an extended-and-filled result (3). You may think of this as a brick of shape 2 2 3, or a 2x2 array of lists of shape 3, or as an array of tables or atoms.