[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Bug-apl] Strange behaviour of ,/
From: |
Kacper Gutowski |
Subject: |
Re: [Bug-apl] Strange behaviour of ,/ |
Date: |
Tue, 4 Feb 2014 15:48:19 +0100 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
On 2014-02-04 12:17:16, Elias Mårtenson wrote:
> When I look at the result from ,/1
> 2 3 it looks like an array that contains a single element: another array with
> the values 1 2 3 in it. But it isn't.
Technically you aren't wrong at all, it IS an array that contains a
single element. But one-element arrays can come in many shapes.
Any array whose shape list consist of only ones is an “array that
contains a single element.” For example if ⍴X ←→ 1 1 1 then X
contains only single element although it's 3-dimensional (its rank
⍴⍴X is 3). In general, array X contains ×/⍴X elements, and, if
⍴X ←→ ,() then it also has one element (×/⍬ ←→ 1) but it is
zero-dimensional (⍴⍴X ←→ 0). As Jay has written, you can't index it
because number of indices must match number of axes.
> But wait, I can take the first element from it:
>
> ↑,/1 2 3
> 1 2 3
First takes the first element of array's ravel list which is always
a vector (rank 1). In other words, this works as you'd expect:
(,,/1 2 3)[1]
1 2 3
Fact that scalars are rank zero arrays may be perplexing at first.
It sure was for me; in Matlab, for example, nothing can have rank
lower than 2, and therefore “vectors” come in two flavors (row and
column) and “scalars” are expressed as 1×1 matrices.
APL's approach is more consistent, though. Vector has rank one and
scalar's rank is zero.
Note that every time you write one element literal, you're
creating a scalar not a single element vector:
1 = ⍳1
1
1 ≡ ⍳1
0
1 ≡ ↑⍳1
1
(,1) ≡ ⍳1
1
⍴⍴1
0
⍴⍴⍳1
1
⍴'ab'
2
⍴⍴'ab'
1
⍴'a' ⍝ empty result
⍴⍴'a'
0
⍴,'a'
1
-k