Sharpening your APL knife

Operators -2-

Outer product: Definition 1

{Result}←L ∘.f R

Outer product: Definition 2

{Result}←1 2 ∘.f 20 30 40

  • (1 f 20) (1 f 30) (1 f 40)
  • (2 f 20) (2 f 30) (2 f 40)

The no of elements in the left array defines the no of rows in the result. The no of elements in the right array defines the no of cols in the result

Outer product: Applications (1)

(⍳3) ∘.+ 10×⍳4

  • 11 21 31 41
  • 12 22 32 42
  • 13 23 33 43

Outer product: Applications (2)

(⍳3) ∘., 10×⍳4

  • 1 10  1 20  1 30  1 40 
  • 2 10  2 20  2 30  2 40 
  • 3 10  3 20  3 30  3 40 

Outer product: Applications (3)

(⍳5) ∘.| ⍳5

  • 0 0 0 0 0
  • 1 0 1 0 1
  • 1 2 0 1 2
  • 1 2 3 0 1
  • 1 2 3 4 0

{⍵/⍨2=+⌿0=⍵∘.|⍵}⍳20

2 3 5 7 11 13 17 19

Outer product: Applications (4a)

⎕←q←↑(1 10)(1 4)(1 2)(2 0)(3 1)(3 3)(4 4)

  • 1 10
  • 1 4
  • 1 2
  • 2 0
  • 3 1
  • 3 3
  • 4 4

Outer product: Applications (4b)

q[;1]∘.=∪q[;1]

  • 1 0 0 0
  • 1 0 0 0
  • 1 0 0 0
  • 0 1 0 0
  • 0 0 1 0
  • 0 0 1 0
  • 0 0 0 1

Inner product: Definition -1-

{Result}←L lf.rf R

Inner product: Definition -2-

{Result}←L leftFns.rightFns R

  1. Combines subarrays along the last axis of L with subarrays along the first axis of R by applying a "rightFns" Outer Product
  2. Finally, an "leftFns"-reduction is applied to each item of the intermediary result

Inner product: Matrix Multiplication 1

A vector of (hours,minutes,seconds) needs to be converted to seconds

v←2 15 30

This can be achieved by multiplying with the appropriate no of seconds:

+/3600 60 1 × 2 15 30

This can also be achieved by using an Inner Product

3600 60 1 +.× 2 15 30

Inner product: Matrix Multiplication 2

{Result}←(2 3⍴⍳6) +.× 10×3 2⍴⍳6

  • 220 280
  • 490 640

Inner product: Matrix Multiplication 3

L←2 3⍴⍳6 ⋄ R←10×3 2⍴⍳6 ⋄ L+.×R

  •   ↓L
  • 1 2 3  4 5 6 
  •   ⊂[1]R
  • 10 30 50  20 40 60 
  •   (↓L) ∘.× ⊂[1]R
  • 10 60 150   20 80 180
  • 40 150 300  80 200 360

Inner product: Matrix Multiplication 4

⎕←intRes←(↓L) ∘.× ⊂[1]R

  • 10 60 150   20 80 180
  • 40 150 300  80 200 360
  •   +/¨intRes
  • 220 280
  • 490 640

Inner product: Row Search 1

  • m←↑'Clinton' 'Bush' 'Reagan' 'Bush'
  •   m∧.=(2⊃⍴m)↑'Bush'
  • 0 1 0 1
  •   {⍵/⍳⍴⍵}m∧.=(2⊃⍴m)↑'Bush'
  • 2 4
  •    {⍵/⍳⍴⍵}¨(⊂m)∧.=¨(2⊃⍴m)↑¨'Bush' 'Clinton'
  • 2 4  1
  •   {⍵/⍳⍴⍵}∨/m∧.=⍉(2⊃⍴m)↑[2]↑'Bush' 'Clinton'
  • 2 4 1

Note also: ∨/m∧.=⍉↑(2⊃⍴m)↑¨'Bush' 'Clinton'

Inner product: Row Search 2

  •   m←⍕,[1.5]⍳1000000
  •   {⍵/⍳⍴⍵}∨/¨(↓m)∧.=¨⊂¯7↑⍕500001
  • 500001    ⍝ 3.68 sec
  •   {⍵/⍳⍴⍵}m ≡¨⊂¯7↑⍕500001
  • 500001    ⍝ 0.375 sec
  •   {⍵/⍳⍴⍵}m∧.= ¯7↑⍕500001
  • 500001    ⍝ 0.015 sec

End