Nouns hold data. Examples are 1, 3 4 5, and 'abc'.
Nouns have type and precision, shape, and elements.
Types and Precisions
Data in J come in types that indicate the general sort of data; each type comprises precisions that describe the data in more detail. All the data in a noun must be of the same precision. Data within the same type can be joined together into arrays, and J will make reasonable conversions between precisions to choose a suitable precision for the entire noun.
J Types and Precisions Type Precision Result of 3!:0 Example Comments numeric Boolean 1 0 Only values 0 or 1 integer 4 100 machine-word integers extended integer 64 100x extended-precision integers, using as many digits as necessary rational 128 1r2 fraction, represented as extended numerator and extended denominator float 8 1.2 double-precision IEEE floating-point value complex 16 1j2 real and imaginary, both floats character byte 2 'abc' ASCII characters only; other bytes for external interfacing unicode 131072 u: 260 unicode code points up to U+FFFF unicode4 262144 u: 16b10600 unicode code points up to U+10FFFF boxed boxed 32 <anything a box has contents, which are hidden inside. The box itself is a single atomic entity with its own type symbol symbol 65536 s: string a symbol is a hash-table entry representing a string sparse numeric sparse Boolean 1024 A sparse array stores only the atoms that differ from a default value, usually 0. The sparse array may have one of the types/precisions listed. sparse byte 2048 sparse integer 4096 sparse floating-point 8192 sparse complex 16384 sparse boxed sparse boxed 32768
The operations allowed on a noun depend on its type. All nouns can participate in nonnumeric operations, such as copying, joining into an array, and comparison for equality. Only numeric types can participate in numeric operations such as addition and multiplication, and only character nouns with byte precision may be written or read from files.
Atoms and Arrays
A noun is either an atom or an array.
The smallest unit of data is the atom (from Greek a not + temnein to cut, i. e. something that cannot be divided). A numeric atom is a number; a character atom is a letter. The box is a special kind of atom, discussed below. Scalar is another word for atom.
An array is a single noun that contains multiple atoms organized along axes. Any number of axes are allowed. An array with one axis is called a list, one with 2 axes a table, and one with 3 axes a brick.
i. 3 NB. a list 0 1 2 i. 2 3 NB. A table 0 1 2 3 4 5 i. 2 3 4 NB. A brick 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Arrays must be homogeneous:
- All elements of an array must be of the same precision.
- Ragged arrays are not supported. Each row of a table must have the same number of atoms, all the layers of a brick must be identically-shaped tables, etc.
Boxing is a way to bypass the requirement for homogeneous arrays. Boxing a noun produces an atom, called a box, whose contents are the value of the noun. The box can be put into an array with other boxes.
A box is not a numeric type, even if its contents are numeric, so the box cannot participate in numeric operations.
'abc',1 2 3 NB. Heterogeneous array not allowed |domain error | 'abc' ,1 2 3 (<'abc') NB. Put the string into a box +---+ |abc| +---+ (<'abc'),(<1 2 3) NB. OK to join two boxes +---+-----+ |abc|1 2 3| +---+-----+
A box may be opened, which creates a noun containing the contents of the box.
There is no way to write a constant representing a single-element list.
Shape and Rank
The number of axes, which is the same as the number of atoms in the shape, is called the rank of a noun.
i. 4 NB. shape is 4 0 1 2 3 i. 2 3 NB. shape is 2 3, i. e. 2 rows, 3 columns 0 1 2 3 4 5
Indexes and Arrays Of Arrays
The index list of an atom of an array is a list, whose length is the rank of the array, which gives the coordinates of the atom in the array. The first number in the index selects a component along the first axis; within that component, the second number in the index selects a component; and so on, specifying an atom uniquely.
The indexing process described above can be stopped before it specifies an atom. For example, given a table, we could have a partial index list 0 which would specify the first list in the table without going on to select an atom from that list.
Such an incomplete index is called a subindex, and the noun specified by the subindex is called a cell.
i. 2 3 0 1 2 3 4 5 NB. subindex of 0 designates the cell 0 1 2, which is a list NB. subindex of 1 designates the cell 3 4 5, which is a list NB. subindex of 1 1 designates the cell 4, which is an atom
It follows that the rank of a cell of a noun is the difference between the rank of the noun and the length of the subindex. Calling this rank k, we say that the cell is a k-cell. A 0-cell is an atom, a 1-cell is a list, etc.
Not so fast, bro! That last paragraph was really important. Make sure you can say it in your own words!
At a higher level, we say that any array can be thought of as an array of k-cells, where k is anything from 0 to the rank of the array.
k An array with frame of k-cells whose shape is 0 2 3 4 empty 1 2 3 4 2 2 3 4 3 empty 2 3 4
Infinite and Negative Cell Rank
If k is greater than or equal to the rank of a noun, the noun has a single k-cell which is the entire noun. The frame of this cell is empty. The extreme case of this is when k is _ (infinity), which is always greater than the rank of any noun.
The sentence # y gives the number of items in y.
$ '' NB. shape of empty string 0 $ 'a' NB. shape of atom
a =. i. 3 0 4 NB. an empty array $ a NB. its shape 3 0 4 #a NB. number of items 3
Leading Ones In The Shape
a =. 5 NB. An atom b =. ,a NB. A 1-element list $a NB. They have different shapes. $b 1 1 2 3 + a NB. They execute differently... 6 7 8 1 2 3 + b |length error | 1 2 3 +b a NB. ... but the display is identical 5 b 5
In general, leading 1s in the shape of a noun cannot be discerned from the display of the noun itself. You have to look at the shape of the noun.
a =. 0 1 2 3 NB. a list b =. ,: a NB. A table with 1 row a NB. The displays are identical... 0 1 2 3 b 0 1 2 3 $ a NB. ...but the shapes are different 4 $b 1 4
The Value of a Noun
The value of a noun is the combination of its type, shape, and elements.