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
1
x[y;;…;] ≡ x h y
1

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

Rotate

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

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

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

      
ixrt←{
r←≢⍴⍺
i←⍋⍋((≢⍴⍵)⍴1),r⍴0,r⍴1
((⍴⍵),1↓⍴⍺) ⍴ i ⍉ ⍵ ⊖⍤(0,r) ⊢ ((⍴⍵),2,1↓⍴⍺) ⍴ (1+1∊⍵)↑⍺
}

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

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

      
ixn←{
r←≢c←1↓⍴⍺
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
1
x[y;] ≡ x ixn y
1

If the array to be indexed is numeric, it is possible to to use arithmetic to finesse the prohibition against using [;] and ⌷.
Roger|Dyalog
 
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