Sharpening your APL knife
Basics -2-
Scalars -1-
- s←2
- ⍴s
-
- s[1]
- RANK ERROR ⍝ A scalar does not have any axis
- 1⊃s
- RANK ERROR ⍝ Does not work either
Scalars -2-
- ⍬⌷1
- 1 ⍝ !
- ''⌷1
- 1
- ⍬⌷'abc'
- LENGTH ERROR
In such a case, an empty numeric vector and an empty string are exchangeable
Scalars -4-
- ⍴v←1 2 3
- 3
- v≡(⍴v)⍴v
- 1
- s←1
- s≡(⍴s)⍴s
- 1
Scalar extension works recursively!
Scalars -3-
- ''⍴1 2 3
- 1
- ≡''⍴1 2 3
- 0
- ≡⍬⍴1 2 3
- 0
- (''⍴1 2 3) ≡ (⍬⍴1 2 3)
- 1
Axis -1-
In an APL operation, an axis needs to be specified except for...
Scalars have no axis at all while vectors have exactly one: no need to specify this one and only axis then!
Axis -2-
In case no axis is specified, the last axis is the default
- m←2 3⍴⍳6
- m,0 ←→ m,[⍴⍴m]0
- ⍝ no axis specified means: take the last one:
- 6 15 ←→ +/m
- ⍝ ... which disappears therefore
- ⍝ Specify an axis explicitly:
- 5 7 9 ←→ +/[1]m
- ⍝ There are shortcuts for: "Take the first axis"
- +⌿m ⋄ ×⍀m ⋄ m⍪0
Scalar Extension -1-
- m←2 3⍴⍳6
- m,0
- 1 2 3 0
- 4 5 6 0
- m,0 0
- 1 2 3 0
- 4 5 6 0
Scalar Extension -2-
- m←2 3⍴⍳6 ⋄ m,0 0 0
- LENGTH ERROR
- m⍪0 0 0
- 1 2 3
- 4 5 6
- 0 0 0
- (m,[1]0) ≡ m⍪0 0 0
- 1
- (m⍪0) ≡ m⍪0 0 0
- 1
Scalar Extension -3-
- 2 = 2 2 0
- 1 1 0
- 2 2 0 = 2
- 1 1 0
- 2 2 0 = ,2
- 1 1 0
- 2 2 0 = 1 1⍴2
- 1 1 0
Scalar Extension -4-
- 1 = (1(1(1(0 0)))) (1 1)
- 1 1 1 0 0 1 1
- 1 1 = (1(1(1(0 0)))) (1 1)
- 1 1 1 0 0 1 1
- 1 1 1 = (1(1(1(0 0)))) (1 1)
- LENGTH ERROR
Rules about indexing
- ⍴q[index]
- (⍴index) ←→ ⍴q[index]
- ⍴q[index1;index2]
- ((⍴index1),(⍴index2)) ←→ ⍴q[index1;index2]
- q[ind1;ind2]←a
- (⍴a) ←→ (⍴ind1),(⍴ind2)
- ⍝ Otherwise you get a LENGTH ERROR
Scalar Extension (inverse) -1-
- ≡q←1 (,2) 2
- ¯2
- ≡q[2]←,2
- 1
- ≡q
- 1
- ⍬ ⍬ ⍬≡⍴¨q
- 1
Scalar Extension (inverse) -2-
- q←1 (,2) 3
- q[2]←,2
- ⍝ Why is this not a length error?
- ⍝ If a scalar is requested but a vector of length 1 is...
- ⍝ provided, this vector is transformed into a scalar
- ≡q[2]←,2
- 1
If an expression with an assignment prints something to ⎕SE, this "something" is always what was specified to the right of the assignment arrow
Squad indexing -1-
- v←10 20 30
- 2 ⌷ v
- 20
- 2 3 ⌷ v
- LENGTH ERROR
- (⊂2 3) ⌷ v
- 20 30
Note that an axis must be specified for arrays with a rank greater than 1: no default takes place here
Squad indexing -2-
- m←100×2 3⍴⍳6
- 2 3 ⌷ m
- 600
- 1⌷[1]m
- 100 200 300
- 1⌷[2]m
- 100 400
Squad indexing -3-
Squad Indexing provides almost the same result than bracket indexing. For what was it introduced then?
- v←10⍴⊂⍳10
- (⍳5) ⌷¨ v
- 1 2 3 4 5 6 7 8 9 10
- ⍝ ⌷ is an ordinary fns and can be "eached"
Note that squad can also be used in selective assignments (discussed later)
End