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:
- β$β - this verb returns an array, the dimension of which is specified in the left operand, and the contents in the right operand. Create an array of a given dimension, all elements of which are the same:
3 $ 1 NB. , = 1 1 1 1 2 3 $ 2 NB. 2 3 , = 2 2 2 2 2 2 2
You can also specify an arbitrary vector with the right operand, the elements of which will be sequentially copied into the resulting array:
2 3 $ 1 2 3 1 2 3 1 2 3 2 3 $ 1 2 1 2 1 2 1 2 2 3 $ 1 2 3 4 1 2 3 4 1 2
- "#" - in the dyadic version of the verb copy. Copies the i-th element of the right operand as many times as specified in the i-th element of the left operand. Thus, the length of the resulting array is equal to the sum of the elements x. Example:
1 2 0 3 # 1 2 3 4 1 2 2 4 4 4 4 # 1 1 1 1 1
- "I." Creates listings and tables. In a monadic call, returns an array made up of integers (starting with zero), each of which is greater than the previous one by one. The length of such an array is specified by the right operand. If the value of the operand is negative, then the numbers in the resulting array go in reverse order:
i.4 NB. 0 1 2 3 i._4 3 2 1 0 i.2 3 0 1 2 3 4 5
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:
- floating point random number if operand is zero;
- a random integer in the range from zero to y, if the operand is y.
? 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
- "}." Returns the "tail" of the array, i.e. all elements except the first.
- "{." Returns the "head" of the array, i.e. the first element of the array.
- "{:" Returns the last element of the array.
- "}:" Returns all elements of the array except the last.
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