Rank/Atop: Difference between revisions
Sudleyplace (talk | contribs) No edit summary |
Sudleyplace (talk | contribs) No edit summary |
||
Line 6: | Line 6: | ||
<table border="0" cellpadding="5" cellspacing="0" summary=""> | <table border="0" cellpadding="5" cellspacing="0" summary=""> | ||
<tr> | <tr> | ||
<td valign="top"><apll>Z←(<i>f</i> | <td valign="top"><apll>Z←(<i>f</i>⍤[X] Y) R</apll></td> | ||
<td></td> | <td></td> | ||
<td></td> | <td></td> | ||
<td>Applies the monadic function <apll><i>f</i></apll> to the rank-<apll><i>r</i></apll> cells of <apll>R</apll>, where <apll><i>r</i></apll> is defined by <apll>X</apll>.</td> | <td>Applies the monadic function <apll><i>f</i></apll> to the rank-<apll><i>r</i></apll> cells of <apll>R</apll>, where <apll><i>r</i></apll> is defined by <apll>Y</apll> and restores the cells to the result as per <apll>X</apll>.</td> | ||
</tr> | </tr> | ||
</table> | </table> | ||
Line 15: | Line 15: | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td | <td><apll>R</apll> is an arbitrary array, <apll><i>f</i></apll> is an arbitrary monadic function, <apll>X</apll> is an origin-sensitive integer scalar or vector, and <apll>Y</apll> is an integer scalar or vector.</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td>If <apll>1< | <td>If <apll>1<⍴⍴Y</apll>, signal a <apll>RANK ERROR</apll>.</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td>If <apll>1= | <td>If <apll>1=⍴⍴Y</apll> and <apll>3<⍴Y</apll>, signal a <apll>LENGTH ERROR</apll>.</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td>Normalize <apll> | <td>Normalize <apll>Y</apll> by setting it to <apll>Y←(-⍴⍴R)⌈(⍴⍴R)⌊1↑⌽3⍴⌽Y</apll>.</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td>The cells from <apll>R</apll> are of rank <apll>| | <td>The cells from <apll>R</apll> are of rank <apll>|Y</apll>.</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td>If <apll> | <td>If <apll>Y</apll> is positive, the cells from <apll>R</apll> are taken from the right end of the shape vector; if negative, the cells are taken from the left end of the shape vector. In particular, <apll>CR←(-Y)↑⍴R</apll> is the shape of the righthand cells, and <apll>FR←(-Y)↓⍴R</apll> is the shape of the righthand frame.</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
Line 42: | Line 42: | ||
<p>This dyadic operator is partially implemented by calling the following internal magic function:</p> | <p>This dyadic operator is partially implemented by calling the following internal magic function:</p> | ||
<apll> ∇ Z←(LO #MonRank | <apll> ∇ Z←(LO #MonRank Y) R;O</apll><br /> | ||
<apll>[1] | <apll>[1] Y←1⍴Y</apll><br /> | ||
<apll>[2] O←⍴⍴R</apll><br /> | <apll>[2] O←⍴⍴R</apll><br /> | ||
<apll>[3] | <apll>[3] Y←(-O)⌈O⌊Y</apll><br /> | ||
<apll>[4] Z←LO¨⊂[⍳- | <apll>[4] Z←LO¨⊂[⍳-Y]R⋄→0</apll><br /> | ||
<apll>[5] ⎕PROTOTYPE:Z←⊃LO¨¨⊂[⍳- | <apll>[5] ⎕PROTOTYPE:Z←⊃LO¨¨⊂[⍳-Y]¨0⍴⊂R</apll><br /> | ||
<apll> ∇</apll> | <apll> ∇</apll> | ||
See the discussion below for details on the final processing of the result of this magic function. | |||
<br /> | <br /> | ||
Line 59: | Line 61: | ||
<table border="0" cellpadding="5" cellspacing="0" summary=""> | <table border="0" cellpadding="5" cellspacing="0" summary=""> | ||
<tr> | <tr> | ||
<td valign="top"><apll>Z←L (<i>f</i> | <td valign="top"><apll>Z←L (<i>f</i>⍤[X] Y) R</apll></td> | ||
<td></td> | <td></td> | ||
<td></td> | <td></td> | ||
<td>Applies the dyadic function <apll><i>f</i></apll> between the rank-<apll><i>l</i></apll> cells of <apll>L</apll> and the rank-<apll><i>r</i></apll> cells of <apll>R</apll>, where <apll><i>l</i></apll> and <apll><i>r</i></apll> are defined by <apll> | <td>Applies the dyadic function <apll><i>f</i></apll> between the rank-<apll><i>l</i></apll> cells of <apll>L</apll> and the rank-<apll><i>r</i></apll> cells of <apll>R</apll> and restores the cells to the result as per <apll>X</apll>, where <apll><i>l</i></apll> and <apll><i>r</i></apll> are defined by <apll>Y</apll>.</td> | ||
</tr> | </tr> | ||
</table> | </table> | ||
Line 68: | Line 70: | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td><apll>L</apll> and <apll>R</apll> are arbitrary arrays, <apll><i>f</i></apll> is an arbitrary dyadic function, and <apll> | <td><apll>L</apll> and <apll>R</apll> are arbitrary arrays, <apll><i>f</i></apll> is an arbitrary dyadic function, <apll>X</apll> is an origin-sensitive integer scalar or vector, and <apll>Y</apll> is an integer scalar or vector.</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td>If <apll>1< | <td>If <apll>1<⍴⍴Y</apll>, signal a <apll>RANK ERROR</apll>.</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td>If <apll>1= | <td>If <apll>1=⍴⍴Y</apll> and <apll>3<⍴Y</apll>, signal a <apll>LENGTH ERROR</apll>.</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td>Normalize <apll> | <td>Normalize <apll>Y</apll> by setting it to <apll>Y←(-(⍴⍴L),⍴⍴R)⌈((⍴⍴L),⍴⍴R)⌊1↓⌽3⍴⌽Y</apll>.</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td>The cells from <apll>L</apll> are of rank <apll>| | <td>The cells from <apll>L</apll> are of rank <apll>|Y[⎕IO]</apll>; the cells from <apll>R</apll> are of rank <apll>|Y[⎕IO+1]</apll>.</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td>If <apll> | <td>If <apll>1↑Y</apll> is positive, the cells from <apll>L</apll> are taken from the right end of the shape vector; if negative, the cells are taken from the left end of the shape vector. In particular, <apll>CL←(-1↑Y)↑⍴L</apll> is the shape of the lefthand cells, and <apll>FL←(-1↑Y)↓⍴L</apll> is the shape of the lefthand frame.</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td>If <apll> | <td>If <apll>1↓Y</apll> is positive, the cells from <apll>R</apll> are taken from the right end of the shape vector; if negative, the cells are taken from the left end of the shape vector. In particular, <apll>CR←(-1↓Y)↑⍴R</apll> is the shape of the righthand cells, and <apll>FR←(-1↓Y)↓⍴R</apll> is the shape of the righthand frame.</td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
Line 101: | Line 103: | ||
<p>This dyadic operator is partially implemented by calling the following internal magic function:</p> | <p>This dyadic operator is partially implemented by calling the following internal magic function:</p> | ||
<apll> ∇ Z←L (LO #DydRank | <apll> ∇ Z←L (LO #DydRank Y) R;O</apll><br /> | ||
<apll>[1] | <apll>[1] Y←1↓⌽3⍴⌽Y</apll><br /> | ||
<apll>[2] O←(⍴⍴L),⍴⍴R</apll><br /> | <apll>[2] O←(⍴⍴L),⍴⍴R</apll><br /> | ||
<apll>[3] | <apll>[3] Y←(-O)⌈O⌊Y</apll><br /> | ||
<apll>[4] Z←(⊂[⍳- | <apll>[4] Z←(⊂[⍳-1↑Y]L)LO¨⊂[⍳-1↓Y]R⋄→0</apll><br /> | ||
<apll>[5] ⎕PROTOTYPE:Z←⊃(⊂[⍳- | <apll>[5] ⎕PROTOTYPE:Z←⊃(⊂[⍳-1↑Y]¨0⍴⊂L)LO¨¨⊂[⍳-1↓Y]¨0⍴⊂R</apll><br /> | ||
<apll> ∇</apll> | <apll> ∇</apll> | ||
Line 114: | Line 116: | ||
<apll>[1] Z←⊃(((L-∊⍴∘⍴¨R)⍴¨1),¨⍴¨R)⍴¨R</apll><br /> | <apll>[1] Z←⊃(((L-∊⍴∘⍴¨R)⍴¨1),¨⍴¨R)⍴¨R</apll><br /> | ||
<apll> ∇</apll> | <apll> ∇</apll> | ||
If the axis operator <apll>[X]</apll> is present, it is used in the final stage to disclose (<apll>⊃[X]</apll>) the result of <apll>#Conform</apll> to produce the final result. If the axis operator is not present, the final result is the <apll>⊃</apll> without axis of the <apll>#Conform</apll> result. | |||
For example, | |||
<apll> L←'abcdef' {diamond} R←⍳⍴L</apll><br /> | |||
<apll> L (,⍤0) R</apll><br /> | |||
<apll> a 1</apll><br /> | |||
<apll> b 2</apll><br /> | |||
<apll> c 3</apll><br /> | |||
<apll> d 4</apll><br /> | |||
<apll> e 5</apll><br /> | |||
<apll> f 6</apll><br /> | |||
<apll> L (,⍤[⎕IO] 0) R</apll><br /> | |||
<apll> a b c d e f</apll><br /> | |||
<apll> 1 2 3 4 5 6</apll><br /> |
Revision as of 22:24, 22 November 2008
Monadic Derived Function
|
||||
R is an arbitrary array, f is an arbitrary monadic function, X is an origin-sensitive integer scalar or vector, and Y is an integer scalar or vector. | ||||
If 1<⍴⍴Y, signal a RANK ERROR. | ||||
If 1=⍴⍴Y and 3<⍴Y, signal a LENGTH ERROR. | ||||
Normalize Y by setting it to Y←(-⍴⍴R)⌈(⍴⍴R)⌊1↑⌽3⍴⌽Y. | ||||
The cells from R are of rank |Y. | ||||
If Y is positive, the cells from R are taken from the right end of the shape vector; if negative, the cells are taken from the left end of the shape vector. In particular, CR←(-Y)↑⍴R is the shape of the righthand cells, and FR←(-Y)↓⍴R is the shape of the righthand frame. | ||||
Loop through the frame applying f to the shape CR cells from R. | ||||
The result is the conforming disclose of the above. |
This dyadic operator is partially implemented by calling the following internal magic function:
∇ Z←(LO #MonRank Y) R;O
[1] Y←1⍴Y
[2] O←⍴⍴R
[3] Y←(-O)⌈O⌊Y
[4] Z←LO¨⊂[⍳-Y]R⋄→0
[5] ⎕PROTOTYPE:Z←⊃LO¨¨⊂[⍳-Y]¨0⍴⊂R
∇
See the discussion below for details on the final processing of the result of this magic function.
Dyadic Derived Function
|
||||
L and R are arbitrary arrays, f is an arbitrary dyadic function, X is an origin-sensitive integer scalar or vector, and Y is an integer scalar or vector. | ||||
If 1<⍴⍴Y, signal a RANK ERROR. | ||||
If 1=⍴⍴Y and 3<⍴Y, signal a LENGTH ERROR. | ||||
Normalize Y by setting it to Y←(-(⍴⍴L),⍴⍴R)⌈((⍴⍴L),⍴⍴R)⌊1↓⌽3⍴⌽Y. | ||||
The cells from L are of rank |Y[⎕IO]; the cells from R are of rank |Y[⎕IO+1]. | ||||
If 1↑Y is positive, the cells from L are taken from the right end of the shape vector; if negative, the cells are taken from the left end of the shape vector. In particular, CL←(-1↑Y)↑⍴L is the shape of the lefthand cells, and FL←(-1↑Y)↓⍴L is the shape of the lefthand frame. | ||||
If 1↓Y is positive, the cells from R are taken from the right end of the shape vector; if negative, the cells are taken from the left end of the shape vector. In particular, CR←(-1↓Y)↑⍴R is the shape of the righthand cells, and FR←(-1↓Y)↓⍴R is the shape of the righthand frame. | ||||
If FL and FR are both non-empty and (⍴FL)≠⍴FR, signal a RANK ERROR; if the shapes of FL and FR are the same, but their values differ, signal a LENGTH ERROR. | ||||
Loop through the frames (scalar extending as necessary) applying f between the shape CL cells from L and the shape CR cells from R. | ||||
The result is the conforming disclose of the above. |
This dyadic operator is partially implemented by calling the following internal magic function:
∇ Z←L (LO #DydRank Y) R;O
[1] Y←1↓⌽3⍴⌽Y
[2] O←(⍴⍴L),⍴⍴R
[3] Y←(-O)⌈O⌊Y
[4] Z←(⊂[⍳-1↑Y]L)LO¨⊂[⍳-1↓Y]R⋄→0
[5] ⎕PROTOTYPE:Z←⊃(⊂[⍳-1↑Y]¨0⍴⊂L)LO¨¨⊂[⍳-1↓Y]¨0⍴⊂R
∇
Both of the above magic functions implement their respective derived function except for some final processing which is essentially a disclose but one which allows for mismatched ranks. That part is implemented by the following function where L has already been calculated as the maximum rank across all items:
∇ Z←L #Conform R
[1] Z←⊃(((L-∊⍴∘⍴¨R)⍴¨1),¨⍴¨R)⍴¨R
∇
If the axis operator [X] is present, it is used in the final stage to disclose (⊃[X]) the result of #Conform to produce the final result. If the axis operator is not present, the final result is the ⊃ without axis of the #Conform result.
For example,
L←'abcdef' ⋄ R←⍳⍴L
L (,⍤0) R
a 1
b 2
c 3
d 4
e 5
f 6
L (,⍤[⎕IO] 0) R
a b c d e f
1 2 3 4 5 6