1:1 partitioning a Vec question

For users of dfns, both novice and expert

1:1 partitioning a Vec question

Postby drothman on Fri Feb 03, 2017 2:37 pm

Given I'm more APL than APL2:

x←(1 2)(3 4 5)(6 7 8 9)
y←(2 1) (3 1 2 1) (2 4)

I need an idiom to return essentially each y picking out of each x, so result would be:
2 1 5 3 4 3 7 9

I know that this works: x{⍺[⍵]}¨y but:

1. i don't really understand why y indexes 'each' x. IOW, if that works, i'd think x[y] should work.

Now, i came up with this bastardized solution: x[↓((,↑⍴¨y)/⍳⍴x),[1.1]↑,/y] but i have to believe there's a cleaner way. Can someone shed some light for me as to why the dfn works, and also perhaps offer something more elegant than my hacked up vers? thanks
drothman
 
Posts: 4
Joined: Fri Feb 06, 2015 3:03 pm

Re: 1:1 partitioning a Vec question

Postby Veli-Matti on Mon Feb 06, 2017 7:51 am

Hi!
Your second approach is using the Reach Index, i.e. all the indices reach the equivalent item from the original nested vector. You could rewrite the example e.g. as
      x[↑,/,(⍳⍴y)∘.,¨y]
which might show the original idea in a little bit clearer way.

The first approach could also be written with the Index function:
      (⊂¨y)⌷¨x

I recommend consulting the DyalogAPL manual for Indexing.

-wm
Veli-Matti
 
Posts: 26
Joined: Sat Nov 28, 2009 3:12 pm

Re: 1:1 partitioning a Vec question

Postby Morten|Dyalog on Mon Feb 06, 2017 8:37 am

An alternative approach would be:

Code: Select all
      (∊x)[∊y++\¯1↓0,≢¨x]
User avatar
Morten|Dyalog
 
Posts: 294
Joined: Tue Sep 09, 2008 3:52 pm

Re: 1:1 partitioning a Vec question

Postby drothman on Mon Feb 06, 2017 12:30 pm

thanks guys.

1. in my hacked version, i failed to note that i'm playing with some large data sets and (reflexively) avoided using an outer product.

2. (⊂¨y)⌷¨x is what i was looking for. i figured squish quad or pick would be the key, but i had checked Mastering APL for the exact construct and "For now, we shall not try to extract more than one item from a vector; we need additional knowledge to do that" caught me. My bad.
drothman
 
Posts: 4
Joined: Fri Feb 06, 2015 3:03 pm

Re: 1:1 partitioning a Vec question

Postby Morten|Dyalog on Mon Feb 06, 2017 1:31 pm

drothman wrote:i failed to note that i'm playing with some large data sets

In that case, it may be worth noting that:

Code: Select all
      y←1000⍴(2 1)(3 1 2 1)(2 4)
      x←1000⍴(1 2)(3 4 5)(6 7 8 9)
      cmpx '(∊x)[∊y++\¯1↓0,≢¨x]' '∊(⊂¨y)⌷¨x'
  (∊x)[∊y++\¯1↓0,≢¨x] → 1.4E¯4 |   0% ⎕⎕⎕⎕⎕⎕⎕               
  ∊(⊂¨y)⌷¨x           → 2.3E¯4 | +63% ⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕⎕

That's in the unreleased version 16.0 with Marshall's speed-ups to ∊. In v15 the difference is only about 25-30%.
User avatar
Morten|Dyalog
 
Posts: 294
Joined: Tue Sep 09, 2008 3:52 pm


Return to Functional Programming

Who is online

Users browsing this forum: No registered users and 1 guest