Indices: Difference between revisions

From NARS2000
Jump to navigationJump to search
No edit summary
No edit summary
 
(4 intermediate revisions by 2 users not shown)
Line 7: Line 7:
       <td></td>
       <td></td>
       <td></td>
       <td></td>
       <td>returns a simple integer vector or nested vector of integer vectors identical to <apll>,/,R/¨⊂¨⍳⍴1/R</apll>.</td>
       <td>returns a simple integer vector or nested vector of integer vectors identical to <apll>(,R)/,⍳⍴R</apll>.</td>
     </tr>
     </tr>
     </table>
     </table>
Line 16: Line 16:
</tr>
</tr>
<tr>
<tr>
   <td><apll>Z</apll> is an integer vector of length <apll>+/,R</apll>.</td>
   <td>For vector <apll>R</apll>, <apll>Z</apll> is an integer vector.  For all other ranks of <apll>R</apll>, <apll>Z</apll> is a nested vector of integer vectors.  In both case the length of <apll>Z</apll> is <apll>+/,R</apll>.</td>
</tr>
</tr>
<tr>
<tr>
   <td>For scalars or vectors, the result is equivalent to <apll>R/⍳⍴1/R</apll> which encapsulates a very common idiom in one symbol.</td>
   <td>For scalars or vectors, the result is equivalent to <apll>R/⍳⍴R</apll> which encapsulates a very common idiom in one symbol.</td>
</tr>
</tr>
<tr>
<tr>
   <td>For higher rank arrays, the result extends to produce a nested vector of vectors of the indices of all the positive integer elements of <apll>R</apll> replicated as per the corresponding value in <apll>R</apll>.</td>
   <td>For higher rank arrays, the result extends to produce a nested vector of vectors of the indices of all the positive integer elements of <apll>R</apll> replicated as per the corresponding value in <apll>R</apll>.</td>
</tr>
<tr>
  <td>This function is sensitive to <apll>⎕IO</apll>.</td>
</tr>
</tr>
</table>
</table>
Line 28: Line 31:
<p>For example, in origin-0</p>
<p>For example, in origin-0</p>


<apll>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⍸1 0 1 1 1 0 1<br />
<apll><pre>
0 2 3 4 6<br />
      ⍸,3
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⍸2 3 4<br />
0 0 0
0 0 1 1 1 2 2 2 2<br />
      ⍬⍬⍬≡⍸3 ⍝ for scalar S, ⍸S ←→ S⍴⊂⍬  as per the definition R/⍳⍴R
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⍸⎕←2 3⍴⍳4<br />
1
&nbsp;0 1 2<br />
      ⍬≡⍸⍬
&nbsp;3 0 1<br />
1
&nbsp;0 1&nbsp; 0 2&nbsp; 0 2&nbsp; 1 0&nbsp; 1 0&nbsp; 1 0&nbsp; 1 2<br />
      ⍸1 0 1 1 1 0 1
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;⍸1 2 3⍴⍳4<br />
0 2 3 4 6
&nbsp;0 0 1&nbsp; 0 0 2&nbsp; 0 0 2&nbsp; 0 1 0&nbsp; 0 1 0&nbsp; 0 1 0&nbsp; 0 1 2<br />
      ⍸'Now is the time'=' '
<br />
3 6 10
&nbsp;&nbsp;&nbsp;&nbsp;∇ Z←(Txt Rep) txtrep Z;a<br />
      ⍸2 3 4
[1]&nbsp;&nbsp;&nbsp;⍝ Replace Txt in Z with Rep.<br />
0 0 1 1 1 2 2 2 2
[2]&nbsp;&nbsp;&nbsp;:Assert 2=⍴⍴Z ⋄ :Assert (⍴Txt)≡⍴Rep<br />
      ⍸⎕←2 3⍴⍳4
[3]&nbsp;&nbsp;&nbsp;a←⍸Txt⍷Z<br />
0 1 2
[4]&nbsp;&nbsp;&nbsp;Z[⊃⊃¨,¨/¨a+⊂0(0..¯1+⍴Txt)]←((⍴a),⍴Rep)⍴Rep<br />
3 0 1
&nbsp;&nbsp;&nbsp;&nbsp;<br />
0 1 0 2 0 2 1 0 1 0 1 0 1 2
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'Now' 'Who' txtrep 4 13⍴'Now is the time...'<br />
      ⍸1 2 3⍴⍳4
Who is the ti<br />
0 0 1 0 0 2 0 0 2 0 1 0 0 1 0 0 1 0 0 1 2
me...Who is t<br />
 
he time...Who<br />
    ∇ Z←(Txt Rep) txtrep Z;a
&nbsp;is the time.</apll>
[1]   ⍝ Replace Txt in Z with Rep.
[2]   :Assert 2=⍴⍴Z ⋄ :Assert (⍴Txt)≡⍴Rep
[3]   a←⍸Txt⍷Z
[4]   Z[⊃⊃¨,¨/¨a+⊂0(0..¯1+⍴Txt)]←((⍴a),⍴Rep)⍴Rep
   
      'Now' 'Who' txtrep 4 13⍴'Now is the time...'
Who is the ti
me...Who is t
he time...Who
is the time.</pre></apll>

Latest revision as of 22:49, 15 April 2018

Z←⍸R returns a simple integer vector or nested vector of integer vectors identical to (,R)/,⍳⍴R.
R is a simple array of non-negative integers.
For vector R, Z is an integer vector. For all other ranks of R, Z is a nested vector of integer vectors. In both case the length of Z is +/,R.
For scalars or vectors, the result is equivalent to R/⍳⍴R which encapsulates a very common idiom in one symbol.
For higher rank arrays, the result extends to produce a nested vector of vectors of the indices of all the positive integer elements of R replicated as per the corresponding value in R.
This function is sensitive to ⎕IO.


For example, in origin-0

      ⍸,3
0 0 0
      ⍬⍬⍬≡⍸3 ⍝ for scalar S, ⍸S ←→ S⍴⊂⍬  as per the definition R/⍳⍴R
1
      ⍬≡⍸⍬
1
      ⍸1 0 1 1 1 0 1
0 2 3 4 6
      ⍸'Now is the time'=' '
3 6 10
      ⍸2 3 4
0 0 1 1 1 2 2 2 2
      ⍸⎕←2 3⍴⍳4
 0 1 2
 3 0 1
 0 1  0 2  0 2  1 0  1 0  1 0  1 2
      ⍸1 2 3⍴⍳4
 0 0 1  0 0 2  0 0 2  0 1 0  0 1 0  0 1 0  0 1 2

    ∇ Z←(Txt Rep) txtrep Z;a
[1]   ⍝ Replace Txt in Z with Rep.
[2]   :Assert 2=⍴⍴Z ⋄ :Assert (⍴Txt)≡⍴Rep
[3]   a←⍸Txt⍷Z
[4]   Z[⊃⊃¨,¨/¨a+⊂0(0..¯1+⍴Txt)]←((⍴a),⍴Rep)⍴Rep
    ∇
      'Now' 'Who' txtrep 4 13⍴'Now is the time...'
Who is the ti
me...Who is t
he time...Who
 is the time.