Help / Learning / Ch 5: Building Arrays
>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Learning J
|
Chapter 5: Building ArraysThis chapter is about building arrays. First we look at building arrays from lists, and then at joining arrays together in various ways to make larger arrays.
5.1 Building Arrays by Shaping Lists5.1.1 ReviewRecall from Chapter 02 what we mean by the word "items". The items of a list of numbers are the numbers. The items of a table are its rows. The items of a 3-dimensional array are its planes. Recall also that x $ y produces an array of the items of the list y, with shape x, that is, with dimensions given by the list x. For example:
If the list y contains fewer than the number of items needed, then y is re-used in cyclical fashion to make up the number of items needed. This means that an array can be built to show some simple patterning, such as all elements being the same, for example.
The "Shape" verb, dyadic $, has a companion verb, "ShapeOf" (monadic $), which yields the list-of-dimensions, that is, shape, of its argument. To illustrate:
For any array A, its list-of-dimensions $ A is a 1-dimensional list (the shape). Hence $ $ A is a list of 1 item (the rank). Hence $ $ $ A is always a list containing just the number 1.
5.1.2 Empty ArraysAn array can be of length zero in any of its dimensions. A zero length, or empty, list can be built by writing 0 for its list of dimensions, and any value (doesn't matter what) for the value of the item(s).
If E is empty, then it has no items, and so, after appending an item to it, the result will have one item.
Similarly, if ET is an empty table with no rows, and say, 3 columns, then after adding a row, the result will have one row.
5.1.3 Building a ScalarSuppose we need to build a scalar. A scalar has no dimensions, that is, its dimension-list is empty. We can give an empty list as the left argument of $ to make a scalar:
5.1.4 Shape More GenerallyWe said that (x $ y) produces an x-shaped array of the items of y. That is, in general the shape of (x$y) will be not just x, but rather x followed by the shape of an item of y. If y is a table, then an item of y is a row, that is, a list. In the following example, the shape of an item of Y is the length of a row of Y, which is 4 .
The next sections look at building new arrays by joining together arrays we already have. 5.2 Appending, or Joining End-to-EndRecall that any array can be regarded as a list of items, so that for example the items of a table are its rows. The verb , (comma) is called "Append". The expression (x,y) is a list of the items of x followed by the items of y. B =: 2 3 $ 'UVWXYZ' b =: 3 $ 'uvw'
In the example of (A,B) above. the items of A are lists of length 3, and so are the items of B. Hence items of A are compatible with, that is, have the same rank and length as items of B. What if they do not? In this case the "Append" verb will helpfully try to stretch one argument to fit the other, by bringing them to the same rank, padding to length, and replicating scalars as necessary. This is shown the following examples. 5.2.1 Bringing To Same RankSuppose we want to append a row to a table. For example, consider appending the 3-character list b (above) to the 2 by 3 table A (above) to form a new row.
Notice that we want the two items of A to be followed by the single item of b, but b is not a 1-item affair. We could do it by reshaping b into a 1 by 3 table, that is, by raising the rank of b. However, this is not necessary, because, as we see, the "Append" verb has automatically stretched the low-rank argument into a 1-item array, by supplying leading dimension(s) of 1 as necessary.
5.2.2 Padding To LengthWhen the items of one argument are shorter than the items of the other, they will be padded out to length. Characters arrays are padded with the blank character, numerical arrays with zero.
5.2.3 Replicating ScalarsA scalar argument of "Append" is replicated as necessary to match the other argument. In the following example, notice how the scalar '*' is replicated, but the vector (1 $ '*') is padded.
5.3 Stitching, or Joining Side-to-SideThe dyadic verb ,. (comma dot) is called "Stitch". In the expression (x ,. y) each item of x has the corresponding item of y appended to produce an item of the result.
5.4 Laminating, or Joining Face-to-FaceThe verb ,: (comma colon) is called "Laminate". The result of (x ,: y) is always an array with two items, of which the first is x and the second is y
If x and y are tables, then we can imagine the result as one table laid on top of the other to form a 3-dimensional array, of length 2 along its first dimension.
5.5 LinkingThe verb ; (semicolon) is called "Link". It is convenient for building lists of boxes.
Notice how the example of 5;12;1995 shows that (x;y) is not invariably just (< x),(< y) . Since "Link" is intended for building lists of boxes, it recognises when its right argument is already a list of boxes. If we define a verb which does produce (< x),(< y) foo =: 4 : '(< x) , (< y)' we can compare these two:
5.6 Unbuilding ArraysWe have looked at four dyadic verbs: "Append" (,), "Stitch" (,.), "Laminate" (,:) and "Link" (;). Each of these has a monadic case, which we now look at. 5.6.1 RazingMonadic ; is called "Raze". It unboxes elements of the argument and assembles them into a list.
5.6.2 RavellingMonadic , is called "Ravel". It assembles elements of the argument into a list.
5.6.3 Ravelling ItemsMonadic ,. is called "Ravel Items". It separately ravels each item of the argument to form a table.
"Ravel Items" is useful for making a 1-column table out of a list.
5.6.4 ItemizingMonadic ,: is called "Itemize". It makes a 1-item array out of any array, by adding a leading dimension of 1.
5.7 Arrays Large and SmallAs we have seen, an array can be built with the $ verb. 3 2 $ 1 2 3 4 5 6 1 2 3 4 5 6 For small arrays, where the contents can be listed on a single line, there are alternatives to using $, which avoid the need to give the dimensions explicitly.
To build large tables, a convenient method is as follows. First, here is a "utility" verb (that is, a verb which is useful for present purposes, but we don't need to study its definition now.) ArrayMaker =: ". ;. _2 The purpose of ArrayMaker is to build a numeric table row by row from the lines of a script. table =: ArrayMaker 0 : 0 1 2 3 4 5 6 7 8 9 )
(See Chapter 17 for an explanation of how ArrayMaker works). Arrays of boxes can also be entered from a script in the same way: X =: ArrayMaker 0 : 0 'hello' ; 1 2 3 ; 8 'Waldo' ; 4 5 6 ; 9 )
We have reached the end of Chapter 5. |
The examples in this chapter
were executed using J version 701.
This chapter last updated 29 Jul 2012
Copyright © Roger Stokes 2012.
This material may be freely reproduced,
provided that this copyright notice is also reproduced.
>> << Pri JfC LJ Phr Dic Voc !: Rel NuVoc wd Help Learning J