At: Difference between revisions

From NARS2000
Jump to navigationJump to search
(Created page with "==At Derived Functions== <p>The dyadic At operator is used to index assign into an array <apll>R</apll>, returning as the result the changed array. There are four separate cases, depending upon whether the two operands are Variables or Functions. The design of this operator is taken directly from the corresponding feature in Dyalog APL.</p> ===Variable v. Variable=== <table border="1" cellpadding="5" cellspacing="0" rules="none" summary=""> <tr> <td> <table bor...")
 
 
(One intermediate revision by the same user not shown)
Line 10: Line 10:
     <table border="0" cellpadding="5" cellspacing="0" summary="">
     <table border="0" cellpadding="5" cellspacing="0" summary="">
     <tr>
     <tr>
       <td valign="top"><apll>Z←<i>a</i>@<i>b</i> R</apll></td>+
       <td valign="top"><apll>Z←<i>a</i>@<i>b</i> R</apll></td>
     </tr>
     </tr>
     <tr>
     <tr>
Line 33: Line 33:
  5  b  7  8
  5  b  7  8
  9 10  c 12
  9 10  c 12
13 14 15  d</pre></apll></td>
13 14 15  d
</pre></apll>
  </td>
</tr>
</tr>
<tr>
<tr>
Line 40: Line 42:
</table>
</table>
&nbsp;
&nbsp;
===Function v. Variable===
===Variable v. Function===


<table border="1" cellpadding="5" cellspacing="0" rules="none" summary="">
<table border="1" cellpadding="5" cellspacing="0" rules="none" summary="">
Line 47: Line 49:
     <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>@<i>b</i> R</apll></td>
       <td valign="top"><apll>Z←<i>a</i>@<i>g</i> R</apll></td>
 
     </tr>
     </tr>
     <tr>
     <tr>
       <td>Replaces the <apll><i>b</i></apll> items of <apll>R</apll> with <apll><i>f</i></apll> applied to each replaced item, where <apll>{L}</apll> is an optional left argument to <apll><i>f</i></apll>, <apll><i>f</i></apll> is a function, and <apll><i>b</i></apll> is a variable.</td>
       <td>When the Boolean-valued result of <apll><i>g R</i></apll> is <apll>1</apll>, replace the corresponding element of <apll>R</apll> with the corresponding element of <apll><i>a</i></apll>, where <apll><i>a</i></apll> is a variable and <apll><i>g</i></apll> is a function.</td>
     </tr>
     </tr>
     </table>
     </table>
Line 58: Line 59:
<tr>
<tr>
   <td>For example:
   <td>For example:
Replace the diagonal elements of a matrix with their reciprocals:
Replace the prime numbers in R with a character:
<apll><pre>
<apll><pre>
       ÷@(2/¨⍳4)4 4⍴⍳16<_x/>
       'π'@{0π⍵}4 4⍴⍳16
  1  2    3    4
1  π  π 4
  5 1<_r/>6   7    8
π 6 π  8
10 1<_r/>11   12
9 10 π 12
13  14   15 1<_r/>16
π 14 15 16</pre></apll></td>
</pre></apll>
</tr>
<tr>
   <td><p>This case is implemented via the anonymous function <apll>{A←⍵ ⋄ A[⍸⍵⍵ A]←⍺⍺ ⋄ A}</apll>.</p></td>
</tr>
</table>
&nbsp;
===Variable v. Jot===
 
<table border="1" cellpadding="5" cellspacing="0" rules="none" summary="">
<tr>
   <td>
    <table border="0" cellpadding="5" cellspacing="0" summary="">
    <tr>
      <td valign="top"><apll>Z←<i>a</i>@∘ R</apll></td>
    </tr>
    <tr>
      <td>Replaces all of the items of <apll>R</apll> with <apll><i>a</i></apll>, where <apll><i>a</i></apll> is a variable.</td>
    </tr>
    </table>
   </td>
   </td>
</tr>
</tr>
<tr>
<tr>
   <td>Replace the diagonal elements of a matrix with their duplicates:
   <td>For example:
Replace all of the items of <apll>R</apll> with <apll>¯1</apll>:
<apll><pre>
<apll><pre>
      2/¨@(2/¨⍳4)4 4⍴⍳16
      ¯1@∘⍳2 3
1 1    2      3       4
¯1 ¯1 ¯1
  5    6 6    7        8
¯1 ¯1 ¯1
  9  10    11 11      12
</pre></apll>
  13  14      15    16 16
  </td>
</pre</apll></td>
</tr>
</tr>
<tr>
<tr>
   <td><p>This case is implemented via the anonymous function <apll>{⍺←⊢ ⋄ A←⍵ ⋄ 1≥≡A:A⊣A[B]←⍺ ⍺⍺ A[B]⊣B←⊃⍵⍵⌷¨⊂⍳⍴A ⋄ A[⍵⍵]←⍺ ⍺⍺ A[⍵⍵] ⋄ A}</apll>.</p></td>
   <td><p>This case is implemented via the anonymous function <apll>{A←⍵ ⋄ A[]←⍺⍺ ⋄ A}</apll>.</p></td>
</tr>
</tr>
</table>
</table>
&nbsp;
&nbsp;
===Variable v. Function===
 
===Function v. Variable===


<table border="1" cellpadding="5" cellspacing="0" rules="none" summary="">
<table border="1" cellpadding="5" cellspacing="0" rules="none" summary="">
Line 90: Line 110:
     <table border="0" cellpadding="5" cellspacing="0" summary="">
     <table border="0" cellpadding="5" cellspacing="0" summary="">
     <tr>
     <tr>
       <td valign="top"><apll>Z←<i>a</i>@<i>g</i> R</apll></td>
       <td valign="top"><apll>Z←{L} <i>f</i>@<i>b</i> R</apll></td>
     </tr>
     </tr>
     <tr>
     <tr>
       <td>When the Boolean-valued result of <apll><i>g R</i></apll> is <apll>1</apll>, replace the corresponding element of <apll>R</apll> with the corresponding element of <apll><i>a</i></apll>, where <apll><i>a</i></apll> is a variable and <apll><i>g</i></apll> is a function.</td>
       <td>Replaces the <apll><i>b</i></apll> items of <apll>R</apll> with <apll><i>f</i></apll> applied to each replaced item, where <apll>{L}</apll> is an optional left argument to <apll><i>f</i></apll>, <apll><i>f</i></apll> is a function, and <apll><i>b</i></apll> is a variable.</td>
     </tr>
     </tr>
     </table>
     </table>
Line 100: Line 120:
<tr>
<tr>
   <td>For example:
   <td>For example:
Replace the prime numbers in R with a character:
Replace the diagonal elements of a matrix with their reciprocals:
<apll><pre>
      ÷@(2/¨⍳4)4 4⍴⍳16<_x/>
1  2    3    4
5 1<_r/>6    7    8
9  10 1<_r/>11  12
13  14  15 1<_r/>16
</pre></apll>
  </td>
</tr>
<tr>
  <td>Replace the diagonal elements of a matrix with their duplicates:
<apll><pre>
<apll><pre>
      'π'@{0π⍵}4 4⍴⍳16
      2/¨@(2/¨⍳4)4 4⍴⍳16
1 π  π  4
1 1   2      3        4  
π  6 π  8
  5    6 6     7        8  
9 10 π 12
  9   10   11 11      12  
π 14 15 16</pre></apll></td>
  13  14     15   16 16
</pre</apll></td>
</tr>
</tr>
<tr>
<tr>
   <td><p>This case is implemented via the anonymous function <apll>{A←⍵ ⋄ A[⍸⍵⍵ A]←⍺⍺ ⋄ A}</apll>.</p></td>
   <td><p>This case is implemented via the anonymous function <apll>{⍺←⊢ ⋄ A←⍵ ⋄ 1≥≡A:A⊣A[B]←⍺ ⍺⍺ A[B]⊣B←⊃⍵⍵⌷¨⊂⍳⍴A ⋄ A[⍵⍵]←⍺ ⍺⍺ A[⍵⍵] ⋄ A}</apll>.</p></td>
</tr>
</tr>
</table>
</table>

Latest revision as of 17:16, 4 December 2023

At Derived Functions

The dyadic At operator is used to index assign into an array R, returning as the result the changed array. There are four separate cases, depending upon whether the two operands are Variables or Functions. The design of this operator is taken directly from the corresponding feature in Dyalog APL.

Variable v. Variable

Z←a@b R
Replaces the b items of R with a, where a and b are variables.
For example:

Replace the 1st and last characters in a string with a string:

      (⊂'abra')@(1 5)'scads!'
 abra cad abra !
Replace the diagonal elements of a matrix:
      'abcd'@(2/¨⍳4)4 4⍴⍳16
 a  2  3  4
 5  b  7  8
 9 10  c 12
13 14 15  d

This case is implemented via the anonymous function {A←⍵ ⋄ 1≥≡A:A⊣A[⊃⍵⍵⌷¨⊂⍳⍴A]←⍺⍺ ⋄ A[⍵⍵]←⍺⍺ ⋄ A}.

 

Variable v. Function

Z←a@g R
When the Boolean-valued result of g R is 1, replace the corresponding element of R with the corresponding element of a, where a is a variable and g is a function.
For example:

Replace the prime numbers in R with a character:

      'π'@{0π⍵}4 4⍴⍳16
1  π  π  4
π  6  π  8
9 10  π 12
π 14 15 16

This case is implemented via the anonymous function {A←⍵ ⋄ A[⍸⍵⍵ A]←⍺⍺ ⋄ A}.

 

Variable v. Jot

Z←a@∘ R
Replaces all of the items of R with a, where a is a variable.
For example:

Replace all of the items of R with ¯1:

      ¯1@∘⍳2 3
¯1 ¯1 ¯1
¯1 ¯1 ¯1

This case is implemented via the anonymous function {A←⍵ ⋄ A[]←⍺⍺ ⋄ A}.

 

Function v. Variable

Z←{L} f@b R
Replaces the b items of R with f applied to each replaced item, where {L} is an optional left argument to f, f is a function, and b is a variable.
For example:

Replace the diagonal elements of a matrix with their reciprocals:

      ÷@(2/¨⍳4)4 4⍴⍳16x
 1   2    3    4
 5 1r6    7    8
 9  10 1r11   12
13  14   15 1r16
Replace the diagonal elements of a matrix with their duplicates:
       2/¨@(2/¨⍳4)4 4⍴⍳16
 1 1    2       3        4 
   5    6 6     7        8 
   9   10    11 11      12 
  13   14      15    16 16

This case is implemented via the anonymous function {⍺←⊢ ⋄ A←⍵ ⋄ 1≥≡A:A⊣A[B]←⍺ ⍺⍺ A[B]⊣B←⊃⍵⍵⌷¨⊂⍳⍴A ⋄ A[⍵⍵]←⍺ ⍺⍺ A[⍵⍵] ⋄ A}.

 

Function v. Function

Z←{L} f@g R
When the Boolean-valued result of g R is 1, replace the corresponding element of R with f applied to the same element, where {L} is an optional left argument to f, f and g are functions.
For example:

Replace composite numbers with their factors:

      π¨@{~0π⍵}4 4⍴⍳16
 1        2      3  2 2     
   5    2 3      7  2 2 2   
 3 3    2 5     11  2 2 3   
  13    2 7    3 5  2 2 2 2

This case is implemented via the anonymous function {⍺←⊢ ⋄ A←⍵ ⋄ B←⍸⍵⍵ A ⋄ A[B]←⍺ ⍺⍺ A[B] ⋄ A}.