πŸ“œ ⬆️ ⬇️

Programming language J. Amateur look. Part 3. Arrays

Previous article in the Cycle Programming Language J. Amateur Look. Part 2. Tacit Programming

β€œI don't think it suits us. I told him what we were doing, and he did not argue. He just listened. ”
Ken Iverson after one of the interviews


1. Arrays


')
J - language for processing arrays. There are many ways to create arrays in J. For example:

Pay attention to the last example - the verb β€œi.” Returned a two-dimensional array to us, since the operand passed to him was a vector. The first element of the operand indicates the number of rows, the second - the columns. However, with the help of this verb one can get an n-dimensional array. For example, three-dimensional:


  i.2 _2 3 3 4 5 0 1 2 9 10 11 6 7 8 


The rows in the resulting array go in reverse order, because the second dimension is given by a negative number. We will talk more about multidimensional arrays in the next section.

Due to the fact that many standard J verbs are capable of processing data arrays, you can extend the previously described verbs to the case of arrays.

  - 1 2 3 _1 _2 _3 


Dyadic case:

  1 2 3 - 3 2 1 _2 0 2 


In addition, operations with different rank values ​​are allowed:

  1 2 3 - 1 0 1 2 


The same result can be obtained using the verb <:

  <: 1 2 3 0 1 2 


Valid and reverse operation:

  1 - 1 2 3 0 _1 _2 


Recall that the last expression can be written as "1 2 3 - ~ 1".

In addition to the standard arithmetic verbs in our work is useful verb-generator of pseudo-random numbers "?". Being called with one operand β€œ?” Returns:

  ? 0 NB. ,      0.622471 ? 3 NB.      [0;2] 2 ? 3 0 


You can set the pseudo-random number sensor by calling

  9!:1 y 


The initial value of the seed = 7 ^ 5.

As in all the examples earlier in this section, the verb β€œ?” Can be called with an array operand - the result will be an array of the same dimension, each i-th element of which will be a random number on the interval specified by the i-th element of the operand:

  ? 0 10 100 0.429769 7 95 


The verb "?", Of course, works not only with vectors, but also, for example, with matrices:

  ? (2 2 $ 0 10) 0.084712 4 0.840877 1 


2. Parts of speech for working with arrays



As we already know, J is a language for handling arrays. This is expressed, not least in the fact that when working with arrays you practically do not have to use explicit iterative procedures.

Suppose you need to find the sum of all the elements of a sequence in a subset of the Python language. Let us generalize this problem to the standard convolution function:

 def reduce(xs, f, acc = 0): """ : >>> reduce([1,2,3], lambda acc,x: acc + x) 6""" for x in xs: acc = f(acc,x) return acc 


To solve such problems in J there is a special adverb "/", called "between". Indeed, our function on python is equivalent to the following expression "x0 f x1 f ... f xN". In terms of J, this is written as β€œf / xs”, where xs is a noun (vector), f is a verb that is inserted β€œbetween” elements of the noun xs, β€œ/” is an adverb, which actually performs such a transformation. Let's give an example:

  +/ 1 2 3 NB.  Β«1 + 2 + 3Β» 6 -/ i.3 NB.  0 - (1 - 2) 1 


But what if we need to return, as a result of convolution, not only the final result of the calculations, but also all intermediate results (in the context of the Python source code, all intermediate values ​​of the variable β€œacc”)? Ie, for example, for the vector β€œ1 2 3 4” after applying the verb β€œ+” β€œbetween” it is expected to get β€œ1 3 6 10”.

In J for this purpose there is a special adverb "\":

  +/\ 1 2 3 4 NB.  : (1) , (1+2), (1+2+3), (1+2+3+4) 1 3 6 10 -/\ 0 1 2 NB.  : (0), (0-1), (0-(1-2)) 0 _1 1 


Other necessary verbs are β€œ/:” and β€œ\:”, which sort the transmitted vector in ascending and descending order, respectively. And the result of sorting is a vector of indices of sorted elements:

  /: 1 3 2 0 2 1 \: 1 3 2 1 2 0 


In order to get the elements of the array at the specified indices, we use the verb β€œ{”, which extracts the elements from the array (the right operand) at the specified indices (the left operand). For example:

  1 0 1 2 { 11 22 33 44 22 11 22 33 


Other verbs for β€œmanual” indexing of array elements are


Let us recall the adverb β€œ~”, which changes the right and left operand in places in a call, and give a somewhat more complicated example:

  ({~ /:) (? (5 $ 0)) 0.221507 0.293786 0.691701 0.72826 0.839186 


In this example, a sequence of 5 random real numbers is generated, then a hook from the verbs "{~" and "/:" is applied to the result.

3. Multidimensional arrays and ranks


About multidimensional arrays, we have already mentioned. It would be logical to assume that since standard verbs work with both numbers and vectors of numbers, they can also process multidimensional data arrays in the same way.

  ]x =: i.2 3 NB.  ]    , ..     x 0 1 2 3 4 5 x + 10 20 30 |length error x + 10 20 10 11 12 23 24 25 


As we can see, if you execute a standard verb on a matrix and a vector, then the default action will be β€œcolumn by column”. In the example, β€œ10” is added to the element on the first row of each column, and β€œ20” is added to the second row. The dimension of the array in Example 2 is 3. We will say then that the first rank of this array is 2, and the second rank is 3. Since the verb is applied by default to the columns of the matrix, we can say that it is applied by the second rank.

This is a general rule for J - default verbs apply to the extreme rank of an array. In order to explicitly indicate the rank of the verb, the special conjunction "" "(double quotes) is used, which the verb accepts with the left operand, the right (the integer) takes the right operand. For example:

  (i.2 3) + 10 20 10 11 12 23 24 25 (i.2 3) +"2 (10 20) 10 11 12 23 24 25 


As you can see, these two expressions are equivalent. Notice the brackets around the vector (10 20). If they are not set, then translator J will assume that the verb's rank is equal to β€œ2 10 20”, and the right operand of the verb is not specified. In order not to clearly indicate the rank of a verb, it is recommended to use the infinity sign "_":

  (i.2 3) +"_ (10 20) 10 11 12 23 24 25 


The result will not change. If you change the rank of the verb by 1:

  (i.2 3) +"1 (10 20) |length error 


The error of this expression is that we are trying to apply a summing verb to a vector of length 2 (right operand) and to a row vector of the left operand of length 3. Let's change our example a little:

  (i.2 3) +"1 (10 20 30) 10 21 32 13 24 35 


The result corresponds to the sequential summation of the i-th element of the right-hand operand and each i-th element of each row of the left-hand operand.

The final article of the cycle Programming language J. Amateur look. Part 4. Boxes and cycles. Conclusion

Source: https://habr.com/ru/post/198228/


All Articles