<<   >>

29. Array Indexing

{(,⍺)[(⊂⍴⍺)⊥¨⊃∘.,/⍵]}

From What is an Array? [91]:

 

An array is a function from a set of indices to numbers, characters, … A rank-n array is one whose function f applies to n-tuples of non-negative integers. A rank-n array is rectangular if there exist non-negative integer maxima s = (s0, s1, …, sn-1) such that  f(i0, i1, …, in-1 is defined (has a value) for all integer ij such that (0≤ij)^(ij<sj).  s is called the shape of the array.

Indexing on rectangular arrays can be modelled as follow:

   ind←{(,⍺)[(⊂⍴⍺)⊥¨⊃∘.,/⍵]}    

   x←?5 7 11⍴100
   i←?2 3⍴5 ⋄ j←?6⍴7 ⋄ k←?4 1 1⍴11

   x[i;j;k] ≡ x ind i j k
1                              

The function {(⍴⍺)⊥⍵} is the inverse of the odometer function on ⍴⍺ . The cartesian product ∘.,/ and the inverse odometer can be combined for a slight gain in efficiency:

   ind1←{(,⍺)[⊃∘.+/⍵×⌽×\⌽1↓(⍴⍺),1]}

Elided indices can be modelled by using '' to indicate elision or, if trailing, being elided altogether, and:

   eix←{0⌷⍤1⊢(~r↑~⍵∊⊂'')⌽((r←≢⍴⍺)↑⍵),⍪⍳¨⍴⍺}

   x[i;;k]  ≡ x ind x eix i '' k
1
   x[i;j;]  ≡ x ind x eix i j
1
   x[;j;]   ≡ x ind x eix '' j
1
   x[i;;]   ≡ x ind x eix ⊂i
1
   x[i;⍬;k] ≡ x ind x eix i ⍬ k    ⍝ ⍬ different from ''
1


Based on [49e].