At: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(No need for "See Also" on things that are linked in the main text)
m (Text replacement - "<source" to "<syntaxhighlight")
 
(One intermediate revision by the same user not shown)
Line 3: Line 3:
== Description ==
== Description ==
At takes two operands, each of which can be an [[array]] or [[function]], with varying effect:  
At takes two operands, each of which can be an [[array]] or [[function]], with varying effect:  
A call to At is of the form <source lang=apl inline>X(f@g)Y</source> where <source lang=apl inline>X</source> is optional.  
A call to At is of the form <syntaxhighlight lang=apl inline>X(f@g)Y</syntaxhighlight> where <syntaxhighlight lang=apl inline>X</syntaxhighlight> is optional.  
* <source lang=apl inline>g</source>  specifies the indices of the right argument that should be modified.
* <syntaxhighlight lang=apl inline>g</syntaxhighlight>  specifies the indices of the right argument that should be modified.
** '''Function''': Must apply monadically to <source lang=apl inline>Y</source>, and return a boolean array of the same shape as <source lang=apl inline>Y</source>, with ones at positions that should be amended.
** '''Function''': Must apply monadically to <syntaxhighlight lang=apl inline>Y</syntaxhighlight>, and return a boolean array of the same shape as <syntaxhighlight lang=apl inline>Y</syntaxhighlight>, with ones at positions that should be amended.
** '''Array''': A [[vector]] (or [[scalar]]) of [[cell]] indices (of equal lengths) to amend. If the same index appears multiple times, the last amendment overrules any previous ones, meaning that amendments are not stacked. This is in contrast to [[modified assignment]] where multiple specifications of the same parts of the array will be processed sequentially.
** '''Array''': A [[vector]] (or [[scalar]]) of [[cell]] indices (of equal lengths) to amend. If the same index appears multiple times, the last amendment overrules any previous ones, meaning that amendments are not stacked. This is in contrast to [[modified assignment]] where multiple specifications of the same parts of the array will be processed sequentially.
* <source lang=apl inline>f</source> specifies what happens to the elements at those positions.
* <syntaxhighlight lang=apl inline>f</syntaxhighlight> specifies what happens to the elements at those positions.
** '''Function''': Is applied once on an array whose [[major cell]]s are the selected cells. If <source lang=apl inline>X</source> is present, then it is bound to  <source lang=apl inline>f</source> so <source lang=apl inline>X f@g Y</source> is equivalent to <source lang=apl inline>X∘f@g Y</source>.
** '''Function''': Is applied once on an array whose [[major cell]]s are the selected cells. If <syntaxhighlight lang=apl inline>X</syntaxhighlight> is present, then it is bound to  <syntaxhighlight lang=apl inline>f</syntaxhighlight> so <syntaxhighlight lang=apl inline>X f@g Y</syntaxhighlight> is equivalent to <syntaxhighlight lang=apl inline>X∘f@g Y</syntaxhighlight>.
** '''Array''': Replaces the selected elements and must either have the same shape as the array whose major cells are the selected cells, or be a scalar, which then replaces all selected cells.
** '''Array''': Replaces the selected elements and must either have the same shape as the array whose major cells are the selected cells, or be a scalar, which then replaces all selected cells.
Each possible [[derived function]] can be applied [[monadic]]ally or [[dyadic]]ally, except if the left operand (<source lang=apl inline>f</source>) is an array, in which case, only the monadic derived function is defined.
Each possible [[derived function]] can be applied [[monadic]]ally or [[dyadic]]ally, except if the left operand (<syntaxhighlight lang=apl inline>f</syntaxhighlight>) is an array, in which case, only the monadic derived function is defined.


== Examples ==
== Examples ==
<source lang=apl>      ⍝ Simple replacement
<syntaxhighlight lang=apl>      ⍝ Simple replacement
       (1 2 3@4 5 6)7 5 4 10 3 6 9 2 1 8
       (1 2 3@4 5 6)7 5 4 10 3 6 9 2 1 8
7 5 4 1 2 3 9 2 1 8
7 5 4 1 2 3 9 2 1 8
Line 30: Line 30:
30 10 4 10 5
30 10 4 10 5
       ⌽@(2∘|) 1 2 3 4 5  ⍝ Reversal of sub-array 1 3 5
       ⌽@(2∘|) 1 2 3 4 5  ⍝ Reversal of sub-array 1 3 5
5 2 3 4 1</source>
5 2 3 4 1</syntaxhighlight>
== External Links ==
== External Links ==
=== Lessons ===
=== Lessons ===

Latest revision as of 22:17, 10 September 2022

@

At (@) is a primitive dyadic operator that is similar to J and K's Amend in functionality. In Dyalog APL, At handles a subset of the cases of the Under operator that pertain to modifying elements at specific positions in an array.

Description

At takes two operands, each of which can be an array or function, with varying effect: A call to At is of the form X(f@g)Y where X is optional.

  • g specifies the indices of the right argument that should be modified.
    • Function: Must apply monadically to Y, and return a boolean array of the same shape as Y, with ones at positions that should be amended.
    • Array: A vector (or scalar) of cell indices (of equal lengths) to amend. If the same index appears multiple times, the last amendment overrules any previous ones, meaning that amendments are not stacked. This is in contrast to modified assignment where multiple specifications of the same parts of the array will be processed sequentially.
  • f specifies what happens to the elements at those positions.
    • Function: Is applied once on an array whose major cells are the selected cells. If X is present, then it is bound to f so X f@g Y is equivalent to X∘f@g Y.
    • Array: Replaces the selected elements and must either have the same shape as the array whose major cells are the selected cells, or be a scalar, which then replaces all selected cells.

Each possible derived function can be applied monadically or dyadically, except if the left operand (f) is an array, in which case, only the monadic derived function is defined.

Examples

      ⍝ Simple replacement
      (1 2 3@4 5 6)7 5 4 10 3 6 9 2 1 8
7 5 4 1 2 3 9 2 1 8
      (0@2 4) 1 2 3 4 5
1 0 3 0 5
      ⍝ f is a function
      10 (×@2 4) 1 2 3 4 5
1 20 3 40 5
      (÷@2 4) 1 2 3 4 5
1 0.5 3 0.25 5
      ⍝ g is a function
      '*'@(2∘|) 1 2 3 4 5  ⍝ Boolean selection 1 0 1 0 1
* 2 * 4 *
      ⍝ f and g are functions
      10 (×@(≤∘3)) 3 1 4 1 5
30 10 4 10 5
      ⌽@(2∘|) 1 2 3 4 5  ⍝ Reversal of sub-array 1 3 5
5 2 3 4 1

External Links

Lessons

APL Cultivation

Documentation

Publications

Prefix friendly @ by Aaron Hsu and Roger Hui


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)
Quad names Index originComparison toleranceMigration levelAtomic vector