indexing by booleans

APL-related discussions - a stream of APL consciousness.
Not sure where to start a discussion ? Here's the place to be
Forum rules
This forum is for discussing APL-related issues. If you think that the subject is off-topic, then the Chat forum is probably a better place for your thoughts !

indexing by booleans

Postby Roger|Dyalog on Wed Jun 16, 2021 5:21 am

In the recent sequence of posts (for example Addition on 1-Byte Integers), the game was to write a function g such that

      (x symbol y) ≡ x g y

where g does not use the function denoted by "symbol". Here, the game is to write a function h which does not use [;] or ⌷, so that:

      x ← t[?3 19⍴≢t←(?3⍴0),'⍣⍟⌹ab',(⊂'asdf'),¯400+?7⍴1000]
y ← ?97 4 5⍴2

((⊂y)⌷x) ≡ x h y
x[y;;…;] ≡ x h y

⎕io←0 is assumed. ⎕io delenda est!


      ixr ← {(1↓⍴⍺)⍴⍵⊖⍺}⍤99 0 

((⊂y)⌷x) ≡ x ixr y
x[y;] ≡ x ixr y

Indexing ⍺ by a numeric index can be computed independently on each scalar index i (right rank 0). When i is boolean, the result is either the first (or only) major cell or the second major cell, that is, (1↓⍴⍺)⍴i⊖⍺.

Rotate with Transpose

((⍴⍵),1↓⍴⍺) ⍴ i ⍉ ⍵ ⊖⍤(0,r) ⊢ ((⍴⍵),2,1↓⍴⍺) ⍴ (1+1∊⍵)↑⍺

((⊂y)⌷x) ≡ x ixrt y
x[y;] ≡ x ixrt y

Can we compute result on the index items in toto, all at once? Yes, we can. Dyadic transpose plays a key part in so doing.

As is often the case with dyadic transpose, the left argument is either ⍋something or ⍋⍋something; if one doesn't work, try the other. Seriously, the mnemonic is that an item of the left argument specifies where an argument axis goes rather than where a result axis comes from (thus allowing duplicate items and therefore diagonal sections).

Numeric Arrays

x0←c⍴⍺ ⍝ (⊂0)⌷⍺ ←→ ⍺[0;;…;]
x1←c⍴(1∊⍵)↓⍺ ⍝ (⊂1)⌷⍺ ←→ ⍺[1;;…;], or (⊂0)⌷⍺ if ~1∊⍵
(x0 ×⍤(r,0)⊢ 0=⍵) + (x1 ×⍤(r,0)⊢ 1=⍵)

x←¯1e7+?3 19⍴3e7

((⊂y)⌷x) ≡ x ixn y
x[y;] ≡ x ixn y

If the array to be indexed is numeric, it is possible to to use arithmetic to finesse the prohibition against using [;] and ⌷.
Posts: 236
Joined: Thu Jul 28, 2011 10:53 am

Return to APL Chat

Who is online

Users browsing this forum: No registered users and 1 guest