<<     >>

APL Exercises 5
Index-Of
 

⎕io←0 throughout.

Dyadicis the most important APL primitive and familiarization with it is recommended.
 

5.0 Drills I

Predict the result of each expression before evaluating it.

3 1 4 1 5 9 26 5 3 ⍳ 3
3 1 4 1 5 9 26 5 3 ⍳ 1
3 1 4 1 5 9 26 5 3 ⍳ 4
3 1 4 1 5 9 26 5 3 ⍳ 3 1 4 1 5
3 1 4 1 5 9 26 5 3 ⍳ 5 1 4 1 3
3 1 4 1 5 9 26 5 3 ⍳ 3 1 4 1 5 9
3 1 4 1 5 9 26 5 3 ⍳ 2 3 ⍴ 3 1 4 1 5 9
3 1 4 1 5 9 26 5 3 ⍳ 33
3 1 4 1 5 9 26 5 3 ⍳ '3'
 
x←3 1 4 1 5 9 26 53
x⍳x
(⍳≢x)=x⍳x
((⍳≢x)=x⍳x)⌿x
 
a←'Mississippi'
a⍳a
(⍳≢a)=a⍳a
((⍳≢a)=a⍳a)⌿a
 
c←'To' 'err' 'is' 'human' 'to' 'forgive' 'divine'
c⍳c
(⍳≢c)=c⍳c
((⍳≢c)=c⍳c)⌿c

If you get the idea that ⍵⍳⍵ (←→ ⍳⍨⍵) is a good and useful computation, you would be right!
 

5.1 Drills II

In general, ⍺⍳⍵ works on left arguments with rank ≥ 1 . The items to be sought are the major cells of , the subarrays with rank (⍴⍴⍺)-1 along the first axis of . For example, in the most common non-vector case, ifis a matrix then ⍺⍳⍵ will look for vectors that match a row of .

   x←↑'Curry' 'Thompson' 'Durant' 'Green' 'Iguodala' 'Curry'
   x
Curry   
Thompson
Durant  
Green   
Iguodala
Curry
   ⍴x
6 8

Predict the result of each expression before evaluating it.

x⍳'Curry'
x⍳'Curry   '
x⍳2 8⍴'Curry   Jordan  '
x⍳x
(⍳≢x)=x⍳x
((⍳≢x)=x⍳x)⌿x

5.2 Drills III

Predict the result of each expression before evaluating it.

x ⍳⍤1 ⊢'aeiou' ⍝ x as defined in Section 1
⌊/ x⍳⍤1 ⊢'aeiou'
(7|∘.+⍨⍳10) ⍳⍤1 ⊢0
(p∘.=⍳≢p) ⍳⍤1 ⊢1 ⊣ p←7?7

5.3 Nub (AKA Unique)

The monadic function ∪⍵ produces the nub (unique items) of , equivalent to {((⍳≢⍵)=⍵⍳⍵)⌿⍵} . Currently is defined only on scalars and vectors.

Predict the result of each expression before evaluating it.

∪ 3 1 4 1 5 9 2 6 5 3
∪ 'Mississippi'
∪ '⎕io delenda est'
∪ x ⍝ x as defined in Section 1
∪ 'Curry' 'Thompson' 'Durant' 'Green' 'Iguodala' 'Curry'
∪ 0
∪ 0,?1000⍴2
 
≢ {∪⍵,⊖¨⍵}⍣≡ ⊂s←?5 5⍴1000
≢ {∪⍵,⌽¨⍵}⍣≡ ⊂s
≢ {∪⍵,⍉¨⍵}⍣≡ ⊂s
≢ {∪⍵,(⊖¨⍵),(⌽¨⍵),(⍉¨⍵)}⍣≡ ⊂s
 
≢ {∪⍵,⌽¨⍵}⍣≡ ⊂p←13?13
≢ {∪⍵,⍋¨⍵}⍣≡ ⊂p
≢ {∪⍵,⍒¨⍵}⍣≡ ⊂p
≢ {∪⍵,(⌽¨⍵),(⍋¨⍵),(⍒¨⍵)}⍣≡ ⊂p

5.4 Symmetries of the Square

The last two sets of drills are different manifestations of the group of symmetries of the square. Indexing by a permutation p is equivalent to multiplication by the boolean matrix B←p∘.=⍳≢p .

p←13?13
p
B←p∘.=⍳≢p
B
x←?13⍴100
x[p] ≡ B+.×x

Conversely, multiplication by a square boolean matrix B with exactly one 1 in each row and column is equivalent to indexing by the permutation p←B⍳⍤1⊢1 .

B←13 13⍴0 ⋄ B[(⍳≢B),¨(≢B)?≢B]←1
B
+⌿B
+/B
p←B⍳⍤1⊢1
x←?13⍴100
(B+.×x) ≡ x[p]

Since ⊖ ⌽ ⍉ each transform a matrix (respectively, flip vertically, flip horizontally, flip diagonally), they are obviously transformations of a square. However, the above presents a case that there is an isomorphism between boolean matrices (of a particular sort) and permutations, so that ⌽ ⍋ ⍒ too are transformations of a square.