Inverse Where

General APL language issues

Inverse Where

Postby jayfoad on Wed Dec 30, 2020 10:06 am

18.0 introduced the inverse of Where, monadic ⍸⍣¯1. Thanks! As pointed out to me on Reddit (https://www.reddit.com/r/adventofcode/c ... ?context=3) it is useful for building grids out of coordinates:
      ⍸⍣¯1⊢(1 2)(2 1)(3 3)
0 1 0
1 0 0
0 0 1

Unfortunately it insists that its argument is sorted:
      ⍸⍣¯1⊢(2 1)(1 2)(3 3)
DOMAIN ERROR

Can you please consider removing that restriction? I see no benefits and several disbenefits:
  • It took me a long time to work out the reason for the DOMAIN ERROR, and ⎕DMX was no help.
  • The workaround is to explicitly sort the argument, which takes programmer and CPU time: ⍸⍣¯1{(⊂⍋⍵)⌷⍵}(2 1)(1 2)(3 3)
  • Checking that the argument is sorted must take up even more CPU time.
jayfoad
 
Posts: 5
Joined: Wed May 29, 2019 1:51 pm

Re: Inverse Where

Postby Roger|Dyalog on Sat Jan 02, 2021 7:27 am

I believe that a reason for requiring that the argument x in ⍸⍣¯1⊢x be sorted, is that that ensures
x ←→ ⍸ ⍸⍣¯1 ⊢x.
Roger|Dyalog
 
Posts: 236
Joined: Thu Jul 28, 2011 10:53 am

Re: Inverse Where

Postby Roger|Dyalog on Mon Jan 04, 2021 5:41 am

I would like to point out that a better way to create the grid from the coordinates, is the @ operator. For example:

For a vector:

      b←(?10⍴2),4⍴0
b
0 1 0 1 0 0 1 0 1 0 0 0 0 0
⍸b
1 3 6 8
⍸⍣¯1 ⍸b
0 1 0 1 0 0 1 0 1
1@(⍸b)⊢14⍴0
0 1 0 1 0 0 1 0 1 0 0 0 0 0
b ≡ 1@(⍸b)⊢14⍴0
1

That is, ⍸⍣¯1 does not necessarily get the original argument but @ does. Put another way, ⍸⍣¯1⊢x produces the correct grid only if x contains the maximum coordinate.

Likewise for a matrix:

      c←5 6↑?3 4⍴2
c
1 1 1 0 0 0
0 1 0 0 0 0
1 0 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0
⍸c
┌───┬───┬───┬───┬───┬───┬───┐
│0 0│0 1│0 2│1 1│2 0│2 2│2 3│
└───┴───┴───┴───┴───┴───┴───┘
⍸⍣¯1 ⍸ c
1 1 1 0
0 1 0 0
1 0 1 1
1@(⍸c)⊢5 6⍴0
1 0 0 0 0 0 0 0 0 0
0 0 1 1 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
c ≡ 1@(⍸c)⊢5 6⍴0
1
Roger|Dyalog
 
Posts: 236
Joined: Thu Jul 28, 2011 10:53 am


Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest