Rank operator
11 posts
• Page 1 of 2 • 1, 2
Rank operator
The rank operator works as I expected with take (↑).
Why do we get different results with plus-reduce (+/) ?
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
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
-
Morten|Dyalog - Posts: 453
- Joined: Tue Sep 09, 2008 3:52 pm
Re: Rank operator
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.
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.
-
Phil Last - Posts: 628
- Joined: Thu Jun 18, 2009 6:29 pm
- Location: Wessex
Re: Rank operator
P.S.
In case the first sentence wasn't clear f/ has rank 1 therefore
In case the first sentence wasn't clear f/ has rank 1 therefore
f/ ←→ f/⍤1 ⍝ for any fthus
f/ ←→ f/⍤r ⍝ for any r≥1
-
Phil Last - Posts: 628
- Joined: Thu Jun 18, 2009 6:29 pm
- Location: Wessex
Re: Rank operator
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
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:
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
Thanks!
It seems I was on the wrong path with Rank.
Can you suggest an expression with A above
That returns the following:
It seems I'm looking for something like the [index] operator used with plus-reduce, but something more general?
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
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"
will produce, when used rank 2 on >1 rank arrays
Put together this is
or, (this is different but functionally equivalent here)
Of course, instead of tally, you can use any function you want.
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
If it's really reduction with axis you're trying to emulate there is an equality between
f⌿⍤(-x)⊢aand
f⌿[x]afor dyad f, origin-zero axis x and array a where x is less than ⍴⍴a and not less than 1.
-
Phil Last - Posts: 628
- Joined: Thu Jun 18, 2009 6:29 pm
- Location: Wessex
Re: Rank operator
As you suggested, I spent some time experimenting with applying the rank operator to transforms. This is very clever. :-)
Thanks!
Thanks!
- mwr0707
- Posts: 17
- Joined: Wed Oct 28, 2015 9:49 am
11 posts
• Page 1 of 2 • 1, 2
Who is online
Users browsing this forum: No registered users and 1 guest
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group