J Verb Rank and Supplement

I have a 2x3 array

a =. 2 3 $ 2 3 a 2 3 2 3 2 3 

And I want to add all the elements together, using + / to get 15.

So

 +/a 5 5 5 

Hmmm. This explicitly adds columns. I know that + / rank is _ _ _ (i.e., Infinity), and a is rank 2. I cannot translate this to imagine why it adds columns, unfortunately. (I read "J for C programmers")

So just for fun I did:

 +/"1 a 7 8 

So now it adds the lines. Obviously, I changed rank + / to 1, which is less than 2 (rank a), which means ... I don't know. Why am I adding lines now, switching the infinity of the form to 1?

What about

 +/"0 a 2 3 2 3 2 3 

So, now we just add individual cells without anything, so we get an array equal to the original a. Again, I don’t know why, although I can just get confused with the argument to get here: the rank of the verb is less than the rank (noun), so we use this value, which is zero, so we add 0-cells, that is. we add each cell individually individually.

And again for good luck:

 +/"2 a 5 5 5 

And again I add columns. I do not know the mechanism that selects which rows / columns / cells are added when changing the rank of the verb. We add columns, but form my point of view, we could just add rows.

I would like this to be explained, if possible. As I said, I read some kind of literature, but it’s still difficult for me.

+4
source share
2 answers

When you have an n-array, you can access each nesting level using the appropriate rank.

Ranks / Forms NOUN

First you have atoms (rank 0):

 a =: 1 

their form ( $a ) is empty.

Then you have lists (rank 1), which are just a few atoms:

 b =: a,a,a b =: 3 # a b =: 3 $ a 

their form ( $b ) is the length of the list.

 $b 3 

Then the tables (rank 2): lists are put together ( file or otherwise):

 c =: b,.b,.b 

their form is a list of 2 elements: rows, columns.

 $c 3 3 

Then to rank-n array (rank n).

VERB rank

The rank of the verb is a slightly different story. This is the rank on which the verb will be applied. So, when you use the box-0 ( <"0 ) noun, you always put the atoms of that noun, when you use box-1 ( <"1 ), you always list the noun, etc. For instance:

  ]n =: 2 3 4 $ i.24 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 

0-rank:

 <"0 n β”Œβ”€β”€β”¬β”€β”€β”¬β”€β”€β”¬β”€β”€β” β”‚0 β”‚1 β”‚2 β”‚3 β”‚ β”œβ”€β”€β”Όβ”€β”€β”Όβ”€β”€β”Όβ”€β”€β”€ β”‚4 β”‚5 β”‚6 β”‚7 β”‚ β”œβ”€β”€β”Όβ”€β”€β”Όβ”€β”€β”Όβ”€β”€β”€ β”‚8 β”‚9 β”‚10β”‚11β”‚ β””β”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”˜ β”Œβ”€β”€β”¬β”€β”€β”¬β”€β”€β”¬β”€β”€β” β”‚12β”‚13β”‚14β”‚15β”‚ β”œβ”€β”€β”Όβ”€β”€β”Όβ”€β”€β”Όβ”€β”€β”€ β”‚16β”‚17β”‚18β”‚19β”‚ β”œβ”€β”€β”Όβ”€β”€β”Όβ”€β”€β”Όβ”€β”€β”€ β”‚20β”‚21β”‚22β”‚23β”‚ β””β”€β”€β”΄β”€β”€β”΄β”€β”€β”΄β”€β”€β”˜ 

1st rank:

  <"1 n β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚0 1 2 3 β”‚4 5 6 7 β”‚8 9 10 11 β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚12 13 14 15β”‚16 17 18 19β”‚20 21 22 23β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 

2nd rank:

  <"2 n β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚0 1 2 3β”‚12 13 14 15β”‚ β”‚4 5 6 7β”‚16 17 18 19β”‚ β”‚8 9 10 11β”‚20 21 22 23β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 

3 ranks:

  <"3 n β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ 0 1 2 3β”‚ β”‚ 4 5 6 7β”‚ β”‚ 8 9 10 11β”‚ β”‚ β”‚ β”‚12 13 14 15β”‚ β”‚16 17 18 19β”‚ β”‚20 21 22 23β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ 

In this example, ranks above 3 are 3.

You can also use negative ranks, btw, which count back from the highest rank.

You can also mix rows.

Summation

Now you can see how to change the rank +/ , changes the result of the summation. For example, +/"1 summarizes each rank-1 list:

  <"1 n β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚0 1 2 3 β”‚4 5 6 7 β”‚8 9 10 11 β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚12 13 14 15β”‚16 17 18 19β”‚20 21 22 23β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ +/"1 n 6 22 38 54 70 86 

To summarize the rank-n array, you must do n +/ s:

 (+/^:3) n 276 +/+/+/ n 276 

or you can ravel ( , ) an array before adding up:

 +/,n 276 
+3
source

The phrases +/"1 and +/ seem to act on the rows and columns of the matrix, but this is not an exact description of what they do, and does not really help to get an idea of ​​the simplicity and power of the concept of rank.

Let's start by understanding the phrase +/ (for now, we confine ourselves to the monadic form, that is, only the right argument). This phrase consists of the adverb / , which takes the verb on the left, in this case + , and inserts it between the elements of the correct argument. List / vector elements are numbers / atoms in the list, so +/ 3 4 5 6 is actually just abbreviated for 3 + 4 + 5 + 6 . The elements of a table / matrix (rank 2) are its rows (rank 1), and the elements of an array of rank 3 are its tables (rank 2)

  ]a=: i. 2 3 0 1 2 NB. 1st item of a 3 4 5 NB. 2nd item of a ]b=: i. 2 3 4 0 1 2 3 4 5 6 7 NB. 1st item of b 8 9 10 11 12 13 14 15 16 17 18 19 NB. 2nd item of b 20 21 22 23 

Remember +/ inserts + between the elements of its right argument like this:

  +/ i. 2 3 3 5 7 

Essentially simple:

  0 1 2 + 3 4 5 3 5 7 

and

  +/ i. 2 3 4 12 14 16 18 20 22 24 26 28 30 32 34 

just adds two matrices together

  (i. 3 4) + (12 + i. 3 4) 12 14 16 18 20 22 24 26 28 30 32 34 

So, how does rank converge? "I believe that the easiest way to think about it is that you can use it to control how large a piece of the correct argument is to the verb at a time. So, +/"1 matches +/ , but any valid argument will only be sent to +/ list / vector / line at a time. So:

  +/"1 i. 2 3 

submits matrix i. 2 3 i. 2 3 in +/ line in time. +/ then inserts + between the elements of the line, evaluates the expression, and then moves on to the next fragment, adding the results for each fragment together as it goes. In other words, the actual calculation looks something like this.

  +/"1 i. 2 3 --> (+/ 0 1 2) , (+/ 3 4 5) --> (0 + 1 + 2) , (3 + 4 + 5) 3 12 +/"1 i. 2 3 4 NB. Feed right arg to +/ a row-at-a-time 6 22 38 NB. + inserted between items of each row 54 70 86 NB. result has row for each item (matrix) in original argument +/"2 i. 2 3 4 NB. feed right arg to +/ a table/matrix-at-a-time 12 15 18 21 NB. + inserted between rows of each matrix 48 51 54 57 +/"0 i. 2 3 4 NB. feed right arg to +/ an atom at a time 0 1 2 3 NB. this is essentially an identity function because 4 5 6 7 NB. the sum of a number is itself: (+/ 3) is just 3. 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 
+2
source

All Articles