robpike / ivy

ivy, an APL-like calculator
Other
1.33k stars 103 forks source link

support large takes (with zero fill) and large drops for matrices #131

Closed fzipp closed 1 year ago

fzipp commented 1 year ago

Commit https://github.com/robpike/ivy/commit/05a8373fa28de7b4812be8e1d210f6dc3eec65d6 added support for large takes (with zero fill) and large drops (returning an empty vector) for vectors, but it doesn't work with matrices:

4 4 take 3 3 rho iota 9
    take: left operand (4) out of range for 3 in shape (3 3)

4 4 drop 3 3 rho iota 9
    drop: left operand (4) out of range for 3 in shape (3 3)

Expected behavior (consistent with APL):

4 4 take 3 3 rho iota 9
    1 2 3 0
    4 5 6 0
    7 8 9 0
    0 0 0 0

4 4 drop 3 3 rho iota 9
fzipp commented 1 year ago

There is another issue with the zero fill:

x = 3 3 rho iota 9; x
1 2 3
4 5 6
7 8 9

y = 1 take x x; y
(1 2 3|
|4 5 6|
|7 8 9)

2 take y
(1 2 3| 0
|4 5 6|
|7 8 9)

The second element is a scalar, while the first element is a matrix. Comare with APL:

x←3 3 ⍴ ⍳9
1 2 3
4 5 6
7 8 9

y←⊂x
┌─────┐
│1 2 3│
│4 5 6│
│7 8 9│
└─────┘

2↑y
┌─────┬─────┐
│1 2 3│0 0 0│
│4 5 6│0 0 0│
│7 8 9│0 0 0│
└─────┴─────┘

Here, both elements are matrices.