At

From NARS2000
Revision as of 17:14, 4 December 2023 by WikiSysop (talk | contribs)
Jump to navigationJump to search

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 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

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}.