At: Difference between revisions
(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...") |
No edit summary |
||
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> | ||
| | ||
=== | ===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← | <td valign="top"><apll>Z←<i>a</i>@<i>g</i> R</apll></td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<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 | Replace the prime numbers in R with a character: | ||
<apll><pre> | <apll><pre> | ||
'π'@{0π⍵}4 4⍴⍳16 | |||
1 π π 4 | |||
π 6 π 8 | |||
9 10 π 12 | |||
π 14 15 16</pre></apll></td> | |||
</ | </tr> | ||
<tr> | |||
<td><p>This case is implemented via the anonymous function <apll>{A←⍵ ⋄ A[⍸⍵⍵ A]←⍺⍺ ⋄ A}</apll>.</p></td> | |||
</tr> | |||
</table> | |||
| |||
===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 | <td>For example: | ||
Replace all items of <apll>R</apll>: | |||
<apll><pre> | <apll><pre> | ||
¯1@∘⍳2 3 | |||
¯1 ¯1 ¯1 | |||
¯1 ¯1 ¯1 | |||
</pre></apll> | |||
</td> | |||
</pre</apll></td> | |||
</tr> | </tr> | ||
<tr> | <tr> | ||
<td><p>This case is implemented via the anonymous function <apll>{ | <td><p>This case is implemented via the anonymous function <apll>{A←⍵ ⋄ A[]←⍺⍺ ⋄ A}</apll>.</p></td> | ||
</tr> | </tr> | ||
</table> | </table> | ||
| | ||
=== | ===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 109: | ||
<table border="0" cellpadding="5" cellspacing="0" summary=""> | <table border="0" cellpadding="5" cellspacing="0" summary=""> | ||
<tr> | <tr> | ||
<td valign="top"><apll>Z←<i> | <td valign="top"><apll>Z←{L} <i>f</i>@<i>b</i> R</apll></td> | ||
</tr> | </tr> | ||
<tr> | <tr> | ||
<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 119: | ||
<tr> | <tr> | ||
<td>For example: | <td>For example: | ||
Replace the | 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> | ||
2/¨@(2/¨⍳4)4 4⍴⍳16 | |||
1 | 1 1 2 3 4 | ||
5 6 6 7 8 | |||
9 10 | 9 10 11 11 12 | ||
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[ | <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> |
Revision as of 17:14, 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
|
||
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
|
||
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
|
||
For example:
Replace all items of R: ¯1@∘⍳2 3 ¯1 ¯1 ¯1 ¯1 ¯1 ¯1 |
||
This case is implemented via the anonymous function {A←⍵ ⋄ A[]←⍺⍺ ⋄ A}. |
Function v. 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
|
||
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}. |