User:Art Anger/Arrays of Data

From J Wiki
Jump to navigation Jump to search

Arrays of Data

Numeric Literals

We humans mostly think of a number as it is expressed as a sequence of characters representing decimal digits: such as 2021, or 3.14159, or 5/27. The decimal point and the slash are part of those names; they do suggest how to convert between a decimal expansion and a rational version, but are not prescriptions for a computer to do so every time it processes a statement expressed in those forms. As J is currently implemented, as character sequences like those are first encountered, they are promptly converted into their binary forms suited to the host computer. Each of them is treated thereafter as one atom of data, not as a character sequence.

The sequence 5/27 is not a valid expression in J; that value conversion is specified as 5%27, which will be approximated only once, upon read-in. The sequence 5r27, however, specifies that rational value exactly, by storing numerator and denominator separately, for appropriate use in later computations. In like manner, 5j27 will be stored as two components representing the complex value 5+27i. J recognizes other number representations for exact representations (large numbers of digits), base-value representations (up to 36), polar representations of complex numbers using degrees or radians, and multiples of powers of 10, e, or pi. All of these cases are treated as atoms of data, subject to appropriate computational rules.

Arrays of Atoms

Atoms may be listed on one line, spread out in a rectangular array, stacked into brick shape, and even in higher dimensions. In each dimension, the length of that axis is constant across all other dimensions of the rectangular (hyper-)solid. Many tables of collected data have such forms, relating some measured quantity, or quantities, to various relevant affecting factors.

In J, the number of axes of an array is termed its "rank". If there are n axes, they are ordered from 0 through n-1. When displayed, the width (atom count) of the first row is the length of axis 0; the number of rows displayed consecutively is the length of axis 1. The length of axis 2 will be the number of such consecutive displays, separated by one blank line each. Increasing numbers of blank lines in displays of higher-rank values separate the displays of lower-ranking subsections. The shape of an array is a list of those successive axis lengths.

When supplied data cannot fill out a row or layer as fully as do some other portions, those sections can be extended by a fill value, often 0 in a numerical array, or space in a character array. When the use of fill values poses difficulties, varying quantities of values may each be enclosed in boxes, which form a uniform array. Access to the actual values then requires a disclose operation in each relevant step in the computations.

Rank-1 Arrays: Lists

A numerical list may be computed by appending successive atoms: 3, 4.5, six, 7j9 --but only when the name "six" has a numerical value. Such an expression calls for four word recognitions, one evaluation, and three appendings each time it is used. A space-separated list: 3 4.5 6 7j9 can be stored once when first read, and retrieved as a whole when needed, reducing some duplicate work. This compact form cannot be used with named values, such as "six".

A character list also may be computed: 'a', 'c', e, 'g'--but only when the name "e" has a character value. It consumes equally as many steps as the numerical example. It, too, can be specified more compactly: 'aceg', if the name "e" has the value 'e', but which could be many characters in length.

One special class of lists is empty lists. The simplest one is specified in character form as '' (two adjacent apostrophes), or 0 $ 0 in numeric form, but it is actually formless, and will combine with any datatype. Another useful form is a zero-list, to be used for collecting or summing various values separately; n $ 0 gives n zeroes. A popular third form consists of successive integers: i. n gives values from 0 through n-1.

Two lists of the same datatype--numeric, character, or boxed--may be combined into a longer list by appending them, as in the first example.

Rank-2 Arrays: Tables

A rank-2 array is a sequence, or list, of rank-1 lists, usually displayed each below the previous. Its size, or tally, is the count of sub-lists; its shape is the count of sub-lists along with the common length of those sub-lists. One mode of specification is to laminate two sub-lists together: 1 3 5 ,: 11 12 displays as

1  3  5

11 12 0 Lamination forces sub-lists to a minimal common length by filling with a suitable atom (which may also be specified). Beware, however, of single-atom sub-lists: 1 3 5 ,: 11 becomes, by replication,

1  3  5

11 11 11 Since lamination always creates a higher-rank array, attaching a third row to a list of lists has to be a mere appending: (1 3 5 ,: 11 12) , 2 4 6 yields

1  3  5

11 12 0

2  4  6

Note that, since J's computations proceed strictly from right to left, omitting those parentheses gives a different result: 1 3 5 ,: 11 12 , 2 4 6 yields

1  3 5 0 0

11 12 2 4 6 whereas performing the lamination first will maintain the three lists: 1 3 5 , 11 12 ,: 2 4 6 yields

1  3  5

11 12 0

2  4  6

A zero-value table with 9 rows of 13 elements each can be obtained by reshaping: 9 13 $ 0 . A table of the same size, containing consecutive integers, is generated by: i. 9 13 .

Higher-rank arrays: Bricks

Rank-3 arrays are sometimes termed bricks, being 3-dimensional rectangular solids. One example is i. 3 2 5:

0  1  2  3  4
5  6  7  8  9

10 11 12 13 14 15 16 17 18 19

20 21 22 23 24 25 26 27 28 29

Since every array is a list, a brick may be extended with more tables by appending: --A single atom will be replicated to the size of a sub-table item. --An appended list, table, or brick will be extended with fill atoms to match axis sizes of the existing brick --An existing brick will be extended to accommodate larger axis sizes of an appendage.

Finally, a rank-n array may be promoted to a rank-(n+1) array via lamination or reshaping.

- - - - -