Skip to main content

•index — Fresh indices

•index n gives ιn along an axis with a fresh name. stem •index n starts the name with the text stem. The name is _, the stem, · and a random part of 64 bits written as 13 digits, as in _b·k3f9q2m7x1c0p. Two calls give different names, except with a chance of about one in 10¹⁹. •index maps over an array of numbers, with the same stem for each. It also maps over a nested array: one call can make every index that a formula needs. Given a keyed shape, •index returns a record of indices with the shape’s keys. Plain •indexρx, with no stem, is the usual form:

x←2 3ρι6 ◇ [[n i] h]←•index[ρx 4]
ρ:x.[n i]×h                ⍝ [2 3 4]ₓ
k←["n":2 "i":3]ρι6
•indexρk                   ⍝ ["n":[0 1]ₓ "i":[0 1 2]ₓ]

A name that starts with _ is hidden. Plain display leaves hidden axis names out. Any key or name that starts with _ and contains · is displayed up to its last ·, then …. Box mode, ρ and error messages show a fresh name as _b·…. To keep a long key or name of your own short on screen, start it with _ and put a · before the part to leave out: ["_note·long description":3] is displayed as ["_note·…":3]. •literal⁻ writes every name in full. When a hidden axis name is matched with a visible one, the hidden axis is treated as unnamed, and the result keeps the visible name:

i←•index 3 ◇ v←ι["v":3]
i          ⍝ 0 1 2
ρi         ⍝ ["_·…":3]ₓ
ρi+v       ⍝ ["v":3]ₓ

In arithmetic and in selection, indices with different names form a grid of every combination of their positions. An index used twice pairs with itself. Here x.[i]-x.[j] holds the difference between every pair of items of x, and m.[i i] is the diagonal of m:

x←10 20 40 ◇ [i j]←•index 3 3
⌈/,|x.[i]-x.[j]      ⍝ 30
m←[1 2 3◇4 5 6◇7 8 9]
+/m.[i i]            ⍝ 15

Indices name the axes of a formula, as einsum’s letters do. In this product, +.×⍠k sums along k, the index that both operands share. b and o form the result’s grid. ⍉⁻ reorders the result’s axes by name:

xu←[2 3]ρι6ₓ ◇ wu←[3 4]ρι12ₓ
[b o k]←•index 2 4 3
y←xu.[b k]+.×⍠k⁝wu.[k o]
∧/,y=xu+.×wu                 ⍝ $t
∧/, [o b]⍉⁻y = ⍉xu+.×wu     ⍝ $t

Models in index notation builds a neural network and a transformer’s attention with indices.

Names from •index can’t collide with the names on a caller’s data. ι["i":n] gives a name that the caller chooses.

∂ of a dfn that uses •index runs from a program, as ∂ of any other dfn does. Each call’s result still has new names. A map calls such a dfn once on all its cells, as Evaluation describes for any dfn. The cells share each fresh name that the dfn creates. When the dfn’s result holds one of those names, the map calls the dfn once for each cell instead. Each cell’s result then has names of its own.

Errors: DOMAIN for a stem that isn’t text.