Rank/Atop: Difference between revisions

From NARS2000
Jump to navigationJump to search
No edit summary
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>⍤X) R</apll></td>
       <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><apll><apll>R</apll> is an arbitrary array, <apll><i>f</i></apll> is an arbitrary monadic function, and <apll>X</apll> is an integer scalar or vector.</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&lt;⍴⍴X</apll>, signal a <apll>RANK ERROR</apll>.</td>
   <td>If <apll>1&lt;⍴⍴Y</apll>, signal a <apll>RANK ERROR</apll>.</td>
</tr>
</tr>
<tr>
<tr>
   <td>If <apll>1=⍴⍴X</apll> and <apll>3&lt;⍴X</apll>, signal a <apll>LENGTH ERROR</apll>.</td>
   <td>If <apll>1=⍴⍴Y</apll> and <apll>3&lt;⍴Y</apll>, signal a <apll>LENGTH ERROR</apll>.</td>
</tr>
</tr>
<tr>
<tr>
   <td>Normalize <apll>X</apll> by setting it to <apll>X←(-⍴⍴R)⌈(⍴⍴R)⌊1↑⌽3⍴⌽X</apll>.</td>
   <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>|X</apll>.</td>
   <td>The cells from <apll>R</apll> are of rank <apll>|Y</apll>.</td>
</tr>
</tr>
<tr>
<tr>
   <td>If <apll>X</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←(-X)↑⍴R</apll> is the shape of the righthand cells, and <apll>FR←(-X)↓⍴R</apll> is the shape of the righthand frame.</td>
   <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>&nbsp;&nbsp;&nbsp;&nbsp;∇ Z←(LO #MonRank X) R;O</apll><br />
<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇ Z←(LO #MonRank Y) R;O</apll><br />
<apll>[1]&nbsp;&nbsp;&nbsp;X←1⍴X</apll><br />
<apll>[1]&nbsp;&nbsp;&nbsp;Y←1⍴Y</apll><br />
<apll>[2]&nbsp;&nbsp;&nbsp;O←⍴⍴R</apll><br />
<apll>[2]&nbsp;&nbsp;&nbsp;O←⍴⍴R</apll><br />
<apll>[3]&nbsp;&nbsp;&nbsp;X←(-O)⌈O⌊X</apll><br />
<apll>[3]&nbsp;&nbsp;&nbsp;Y←(-O)⌈O⌊Y</apll><br />
<apll>[4]&nbsp;&nbsp;&nbsp;Z←LO¨⊂[⍳-X]R⋄→0</apll><br />
<apll>[4]&nbsp;&nbsp;&nbsp;Z←LO¨⊂[⍳-Y]R⋄→0</apll><br />
<apll>[5]&nbsp;&nbsp;&nbsp;⎕PROTOTYPE:Z←⊃LO¨¨⊂[⍳-X]¨0⍴⊂R</apll><br />
<apll>[5]&nbsp;&nbsp;&nbsp;⎕PROTOTYPE:Z←⊃LO¨¨⊂[⍳-Y]¨0⍴⊂R</apll><br />
<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇</apll>
<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇</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>⍤X) R</apll></td>
       <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>X</apll>.</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> 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>X</apll> is an integer scalar or vector.</td>
   <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&lt;⍴⍴X</apll>, signal a <apll>RANK ERROR</apll>.</td>
   <td>If <apll>1&lt;⍴⍴Y</apll>, signal a <apll>RANK ERROR</apll>.</td>
</tr>
</tr>
<tr>
<tr>
   <td>If <apll>1=⍴⍴X</apll> and <apll>3&lt;⍴X</apll>, signal a <apll>LENGTH ERROR</apll>.</td>
   <td>If <apll>1=⍴⍴Y</apll> and <apll>3&lt;⍴Y</apll>, signal a <apll>LENGTH ERROR</apll>.</td>
</tr>
</tr>
<tr>
<tr>
   <td>Normalize <apll>X</apll> by setting it to <apll>X←(-(⍴⍴L),⍴⍴R)⌈((⍴⍴L),⍴⍴R)⌊1↓⌽3⍴⌽X</apll>.</td>
   <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>|X[⎕IO]</apll>; the cells from <apll>R</apll> are of rank <apll>|X[⎕IO+1]</apll>.</td>
   <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>1↑X</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↑X)↑⍴L</apll> is the shape of the lefthand cells, and <apll>FL←(-1↑X)↓⍴L</apll> is the shape of the lefthand frame.</td>
   <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>1↓X</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↓X)↑⍴R</apll> is the shape of the righthand cells, and <apll>FR←(-1↓X)↓⍴R</apll> is the shape of the righthand frame.</td>
   <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>&nbsp;&nbsp;&nbsp;&nbsp;∇ Z←L (LO #DydRank X) R;O</apll><br />
<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇ Z←L (LO #DydRank Y) R;O</apll><br />
<apll>[1]&nbsp;&nbsp;&nbsp;X←1↓⌽3⍴⌽X</apll><br />
<apll>[1]&nbsp;&nbsp;&nbsp;Y←1↓⌽3⍴⌽Y</apll><br />
<apll>[2]&nbsp;&nbsp;&nbsp;O←(⍴⍴L),⍴⍴R</apll><br />
<apll>[2]&nbsp;&nbsp;&nbsp;O←(⍴⍴L),⍴⍴R</apll><br />
<apll>[3]&nbsp;&nbsp;&nbsp;X←(-O)⌈O⌊X</apll><br />
<apll>[3]&nbsp;&nbsp;&nbsp;Y←(-O)⌈O⌊Y</apll><br />
<apll>[4]&nbsp;&nbsp;&nbsp;Z←(⊂[⍳-1↑X]L)LO¨⊂[⍳-1↓X]R⋄→0</apll><br />
<apll>[4]&nbsp;&nbsp;&nbsp;Z←(⊂[⍳-1↑Y]L)LO¨⊂[⍳-1↓Y]R⋄→0</apll><br />
<apll>[5]&nbsp;&nbsp;&nbsp;⎕PROTOTYPE:Z←⊃(⊂[⍳-1↑X]¨0⍴⊂L)LO¨¨⊂[⍳-1↓X]¨0⍴⊂R</apll><br />
<apll>[5]&nbsp;&nbsp;&nbsp;⎕PROTOTYPE:Z←⊃(⊂[⍳-1↑Y]¨0⍴⊂L)LO¨¨⊂[⍳-1↓Y]¨0⍴⊂R</apll><br />
<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇</apll>
<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇</apll>


Line 114: Line 116:
<apll>[1]&nbsp;&nbsp;&nbsp;Z←⊃(((L-∊⍴∘⍴¨R)⍴¨1),¨⍴¨R)⍴¨R</apll><br />
<apll>[1]&nbsp;&nbsp;&nbsp;Z←⊃(((L-∊⍴∘⍴¨R)⍴¨1),¨⍴¨R)⍴¨R</apll><br />
<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇</apll>
<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇</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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L←'abcdef' {diamond} R←⍳⍴L</apll><br />
<apll>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L (,⍤0) R</apll><br />
<apll>&nbsp;a 1</apll><br />
<apll>&nbsp;b 2</apll><br />
<apll>&nbsp;c 3</apll><br />
<apll>&nbsp;d 4</apll><br />
<apll>&nbsp;e 5</apll><br />
<apll>&nbsp;f 6</apll><br />
<apll>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;L (,⍤[⎕IO] 0) R</apll><br />
<apll>&nbsp;a b c d e f</apll><br />
<apll>&nbsp;1 2 3 4 5 6</apll><br />

Revision as of 03:24, 23 November 2008

Monadic Derived Function

Z←(f⍤[X] Y) R Applies the monadic function f to the rank-r cells of R, where r is defined by Y and restores the cells to the result as per X.
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

Z←L (f⍤[X] Y) R Applies the dyadic function f between the rank-l cells of L and the rank-r cells of R and restores the cells to the result as per X, where l and r are defined by Y.
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