Rank operator

General APL language issues

Rank operator

Postby mwr0707 on Sun Dec 27, 2015 2:17 pm

The rank operator works as I expected with take (↑).
Why do we get different results with plus-reduce (+/) ?

Code: Select all
      A←4 5⍴⍳20
      A
 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20
      1↑A
1 2 3 4 5
      1↑⍤1⊣A
 1
 6
11
16
      +/A
15 40 65 90
      +/⍤1⊣A
15 40 65 90
mwr0707
 
Posts: 17
Joined: Wed Oct 28, 2015 9:49 am

Re: Rank operator

Postby Morten|Dyalog on Mon Dec 28, 2015 9:26 am

I presume your question is about the shape of the result, rather than the values returned? In each case, the operand function is called 5 times on a vector. Each invocation of (1↑) returns a 1-element vector, giving a result shape of (5 by 1). However, (+/) on each vector argument returns a scalar, so there is no contribution to the result shape. If you extract the first item of each vector using pick, which will return a scalar, you will also get a vector result:

Code: Select all
      1⊃⍤1⊣A
1 6 11 16
User avatar
Morten|Dyalog
 
Posts: 453
Joined: Tue Sep 09, 2008 3:52 pm

Re: Rank operator

Postby Phil Last on Mon Dec 28, 2015 9:30 am

f⍤1⊢w reassembles the results of the application of f to each 1-cell (row) of w so you will get the identical result to +/w which by default acts on the last dimension - the rows.

Effects of with reduction become apparent only with ranks greater than two and with rather than /. With f⌿⍤r the first dimension of the r-cells is reduced thus f⌿⍤2 on a 3-d array reduces the second dimension. f⌿⍤2 on a 4-d array reduces the third.
User avatar
Phil Last
 
Posts: 628
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: Rank operator

Postby Phil Last on Mon Dec 28, 2015 9:57 am

P.S.
In case the first sentence wasn't clear f/ has rank 1 therefore
      f/ ←→ f/⍤1 ⍝ for any f
thus
      f/ ←→ f/⍤r ⍝ for any r≥1
User avatar
Phil Last
 
Posts: 628
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: Rank operator

Postby mwr0707 on Mon Dec 28, 2015 10:46 pm

I think I'm beginning to see it. Is this correct?

Code: Select all
      +A←2 4 5⍴⍳40
 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20
             
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40
      ⍴⍤1⊣A ⍝ Each row has shape 5
5
5
5
5
 
5
5
5
5
      ⍴⍤2⊣A ⍝ Each matrix has shape 4 5
4 5
4 5
mwr0707
 
Posts: 17
Joined: Wed Oct 28, 2015 9:49 am

Re: Rank operator

Postby Roger|Dyalog on Sat Jan 02, 2016 3:13 pm

That's correct.

Using ⊂⍤r for monadic application and {⍺⍵}⍤r for dyadic application provides good illustrations of what are the cells, what the operand function is applied to. For example:

Code: Select all
]box on
x←2 3⍴⍳6
y←10+2 3 4⍴⍳24

Code: Select all
⊂⍤0⊢y
⊂⍤1⊢y
⊂⍤2⊢y
⊂⍤3⊢y
⊂⍤4⊢y
⊂⍤¯1⊢y

Code: Select all
x{⍺⍵}⍤0 1⊢y
x{⍺⍵}⍤1 3⊢y
x{⍺⍵}⍤2 2⊢y
x{⍺⍵}⍤¯1⊢y
Roger|Dyalog
 
Posts: 238
Joined: Thu Jul 28, 2011 10:53 am

Re: Rank operator

Postby mwr0707 on Sat Jan 02, 2016 6:42 pm

Thanks!

It seems I was on the wrong path with Rank.

Can you suggest an expression with A above
Code: Select all
 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20
             
21 22 23 24 25
26 27 28 29 30
31 32 33 34 35
36 37 38 39 40


That returns the following:
Code: Select all
fA ⍝ Each column contains 4 elements
4 4 4 4 4
4 4 4 4 4


It seems I'm looking for something like the [index] operator used with plus-reduce, but something more general?
mwr0707
 
Posts: 17
Joined: Wed Oct 28, 2015 9:49 am

Re: Rank operator

Postby DanB|Dyalog on Sun Jan 03, 2016 12:54 am

I think you want to apply a function on columns (here tally).
Cells are defined on the trailing axes (e.g. the rows of a matrix, the planes of a n-D array). They are not defined for leading or intermediate dimensions.
You can, however, use transpose to change that by moving dimensions to the end.

For example, using "tally on rows after transposing"
Code: Select all
      f←≢⍤1⍉

will produce, when used rank 2 on >1 rank arrays
Code: Select all
      f⍤2⊢A
4 4 4 4 4
4 4 4 4 4

Put together this is
Code: Select all
      (≢⍤1⍉)⍤2 ⊢A
4 4 4 4 4
4 4 4 4 4

or, (this is different but functionally equivalent here)
Code: Select all
      ≢⍤1⍉⍤2 ⊢A
4 4 4 4 4
4 4 4 4 4

Of course, instead of tally, you can use any function you want.
DanB|Dyalog
 

Re: Rank operator

Postby Phil Last on Sun Jan 03, 2016 2:55 pm

If it's really reduction with axis you're trying to emulate there is an equality between
      f⌿⍤(-x)⊢a
and
      f⌿[x]a
for dyad f, origin-zero axis x and array a where x is less than ⍴⍴a and not less than 1.
User avatar
Phil Last
 
Posts: 628
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: Rank operator

Postby mwr0707 on Tue Jan 05, 2016 1:33 pm

As you suggested, I spent some time experimenting with applying the rank operator to transforms. This is very clever. :-)

Thanks!
mwr0707
 
Posts: 17
Joined: Wed Oct 28, 2015 9:49 am

Next

Return to Language

Who is online

Users browsing this forum: No registered users and 1 guest