Rank/Atop: Difference between revisions

From NARS2000
Jump to navigationJump to search
No edit summary
No edit summary
Line 1: Line 1:
This dyadic operator is implemented via several internal magic functions:
==Monadic Derived Function==


'''Monadic Derived Function'''
<table border="1" cellpadding="5" cellspacing="0" rules="none" summary="">
<tr>
  <td>
    <table border="0" cellpadding="5" cellspacing="0" summary="">
    <tr>
      <td><apll>Z←(<i>f</i>⍤X) R</apll></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>
    </tr>
    </table>
  </td>
</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>
</tr>
<tr>
  <td>If <apll>1&lt;⍴⍴X</apll>, signal a <apll>RANK ERROR</apll>.</td>
</tr>
<tr>
  <td>If <apll>1=⍴⍴X</apll> and <apll>3&lt;⍴X</apll>, signal a <apll>LENGTH ERROR</apll>.</td>
</tr>
<tr>
  <td>Normalize <apll>X</apll> by setting it to <apll>X←(-⍴⍴R)⌈(⍴⍴R)⌊1↑⌽3⍴⌽X</apll>.</td>
</tr>
<tr>
  <td>The cells from <apll>R</apll> are of rank <apll>|X</apll>.</td>
</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>
</tr>
<tr>
  <td>Loop through the frame applying <apll><i>f</i></apll> to the shape <apll>CR</apll> cells from <apll>R</apll>.</td>
</tr>
<tr>
  <td>The result is the <b>conforming</b> disclose of the above.</td>
</tr>
</table>
<br />
<p>This dyadic operator is partially implemented by calling the following internal magic function:</p>


<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇ Z←(LO F X) R;O</apll><br />
<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇ Z←(LO #MonRank X) R;O</apll><br />
<apll>[1]&nbsp;&nbsp;&nbsp;X←1⍴X</apll><br />
<apll>[1]&nbsp;&nbsp;&nbsp;X←1⍴X</apll><br />
<apll>[2]&nbsp;&nbsp;&nbsp;O←⍴⍴R</apll><br />
<apll>[2]&nbsp;&nbsp;&nbsp;O←⍴⍴R</apll><br />
Line 11: Line 50:
<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇</apll>
<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇</apll>


'''Dyadic Derived Function'''
<br />
<br />
==Dyadic Derived Function==


<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇ Z←L (LO F X) R;O</apll><br />
<table border="1" cellpadding="5" cellspacing="0" rules="none" summary="">
<tr>
  <td>
    <table border="0" cellpadding="5" cellspacing="0" summary="">
    <tr>
      <td><apll>Z←L (<i>f</i>⍤X) R</apll></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>
    </tr>
    </table>
  </td>
</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>
</tr>
<tr>
  <td>If <apll>1&lt;⍴⍴X</apll>, signal a <apll>RANK ERROR</apll>.</td>
</tr>
<tr>
  <td>If <apll>1=⍴⍴X</apll> and <apll>3&lt;⍴X</apll>, signal a <apll>LENGTH ERROR</apll>.</td>
</tr>
<tr>
  <td>Normalize <apll>X</apll> by setting it to <apll>X←(-(⍴⍴L),⍴⍴R)⌈((⍴⍴L),⍴⍴R)⌊1↓⌽3⍴⌽X</apll>.</td>
</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>
</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>
</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>
</tr>
<tr>
  <td>If <apll>FL</apll> and <apll>FR</apll> are both non-empty and <apll>(⍴FL)≠⍴FR</apll>, signal a <apll>RANK ERROR</apll>; if the shapes of <apll>FL</apll> and <apll>FR</apll> are the same, but their values differ, signal a <apll>LENGTH ERROR</apll>.</td>
</tr>
<tr>
  <td>Loop through the frames (scalar extending as necessary) applying <apll><i>f</i></apll> between the shape <apll>CL</apll> cells from <apll>L</apll> and the shape <apll>CR</apll> cells from <apll>R</apll>.</td>
</tr>
<tr>
  <td>The result is the <b>conforming</b> disclose of the above.</td>
</tr>
</table>
<br />
<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>[1]&nbsp;&nbsp;&nbsp;X←1↓⌽3⍴⌽X</apll><br />
<apll>[1]&nbsp;&nbsp;&nbsp;X←1↓⌽3⍴⌽X</apll><br />
<apll>[2]&nbsp;&nbsp;&nbsp;O←(⍴⍴L),⍴⍴R</apll><br />
<apll>[2]&nbsp;&nbsp;&nbsp;O←(⍴⍴L),⍴⍴R</apll><br />
Line 21: Line 109:
<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇</apll>
<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇</apll>


Both of the above 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 <apll>L</apll> has already been calculated as the maximum rank across all items:
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 <apll>L</apll> has already been calculated as the maximum rank across all items:
 
'''Internal Conform Function'''


<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇ Z←L F R</apll><br />
<apll>&nbsp;&nbsp;&nbsp;&nbsp;∇ Z←L #Conform R</apll><br />
<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>

Revision as of 21:33, 10 April 2008

Monadic Derived Function

Z←(f⍤X) R Applies the monadic function f to the rank-r cells of R, where r is defined by X.
R is an arbitrary array, f is an arbitrary monadic function, and X is an integer scalar or vector.
If 1<⍴⍴X, signal a RANK ERROR.
If 1=⍴⍴X and 3<⍴X, signal a LENGTH ERROR.
Normalize X by setting it to X←(-⍴⍴R)⌈(⍴⍴R)⌊1↑⌽3⍴⌽X.
The cells from R are of rank |X.
If X 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←(-X)↑⍴R is the shape of the righthand cells, and FR←(-X)↓⍴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 X) R;O
[1]   X←1⍴X
[2]   O←⍴⍴R
[3]   X←(-O)⌈O⌊X
[4]   Z←LO¨⊂[⍳-X]R⋄→0
[5]   ⎕PROTOTYPE:Z←⊃LO¨¨⊂[⍳-X]¨0⍴⊂R
    ∇



Dyadic Derived Function

Z←L (f⍤X) R Applies the dyadic function f between the rank-l cells of L and the rank-r cells of R, where l and r are defined by X.
L and R are arbitrary arrays, f is an arbitrary dyadic function, and X is an integer scalar or vector.
If 1<⍴⍴X, signal a RANK ERROR.
If 1=⍴⍴X and 3<⍴X, signal a LENGTH ERROR.
Normalize X by setting it to X←(-(⍴⍴L),⍴⍴R)⌈((⍴⍴L),⍴⍴R)⌊1↓⌽3⍴⌽X.
The cells from L are of rank |X[⎕IO]; the cells from R are of rank |X[⎕IO+1].
If 1↑X 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↑X)↑⍴L is the shape of the lefthand cells, and FL←(-1↑X)↓⍴L is the shape of the lefthand frame.
If 1↓X 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↓X)↑⍴R is the shape of the righthand cells, and FR←(-1↓X)↓⍴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 X) R;O
[1]   X←1↓⌽3⍴⌽X
[2]   O←(⍴⍴L),⍴⍴R
[3]   X←(-O)⌈O⌊X
[4]   Z←(⊂[⍳-1↑X]L)LO¨⊂[⍳-1↓X]R⋄→0
[5]   ⎕PROTOTYPE:Z←⊃(⊂[⍳-1↑X]¨0⍴⊂L)LO¨¨⊂[⍳-1↓X]¨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
    ∇