Vocabulary/Nouns

From J Wiki
Jump to navigation Jump to search

Back to: Vocabulary

Nouns

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 atoms 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 10000000000 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
integer2 6 2-byte integers
integer4 7 4-byte integers
long float 11 1.2fq double-double-precision IEEE floating-point value (two IEEE double-precision values with nonoverlapping mantissas)
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 is 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.


Boxes

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.


Constants

Constants are words that directly represent nouns. They include numbers, byte strings between single quotes, and the primitive nouns a. (the list of all 256 bytes) and a: (a box containing 0$0).

A constant containing a single number or letter, e. g. 5, 1j2, and 'c', represents an atom. A constant containing more than one number or letter, e. g. 1 3 and 'abc', represents a list.

There is no way to write a constant representing a single-element list. Such values must be constructed by reshaping to length 1.


Shape and Rank

The shape of a noun is a list giving the number of atoms along each axis. Since an atom has no axes, its shape is an empty list.

The number of axes, which is the same as the number of atoms in the shape, is called the rank of a noun.

Name Rank
atom 0
list 1
table 2
brick 3
   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

The sentence $ y gives the shape of y. The sentence # $ y gives the rank of y.


Indexes and Arrays Of Arrays

A list is a series of atoms. The atoms are distinguished by a sequential number, called the index, that starts at 0 for the first atom. (Zero-orgin indexing points to the start of an interval, whereas the ordinary, ordinal, terminology may name it as a whole, and points to the cardinal value at its end.)

A table can be thought of as a series of lists (all of the same length), where the lists are distinguished by a index.

Likewise, a brick can be thought of as a series of tables distinguished by an index, and so on for arrays of any rank.

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.


Cells

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, there! 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.

For example, a table can be thought of as a table of atoms, or as a list of lists. A brick of shape 2 3 4 can be thought of as any of the following:

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.

If k is negative, it is taken to refer to cells whose rank is the rank of the noun plus the (negative) k. Thus, _1-cells have rank 1 less than the noun, _2-cells have rank 2 less, and so on.


Items

The items of a noun are the _1-cells. Many J verbs operate on items.

The items of a list are atoms; the items of a table are lists; etc. An atom has one item, itself.

The sentence # y gives the number of items in y, its tally.


Empty Arrays

A noun is empty if it contains no atoms. The simplest examples of empty arrays are the empty string '' and the empty numeric list 0$0.

An empty noun contains a 0 in its shape. An atom is not empty (the shape of an atom is empty, though).

   $ ''   NB. shape of empty string
0
   $ 'a'  NB. shape of atom

An empty array may have cells and items. For example, an array with shape 3 0 4 contains no atoms, but it has 3 2-cells, each with shape 0 4:

   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

There is a great difference between an atom and a 1-element list.

   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.

Elements are considered equal if they are of the same type and represent identical values in the type. 1 and 1.0 are the same number.