Identity: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
(→‎Documentation: BQN link)
m (Text replacement - "<source" to "<syntaxhighlight")
Line 4: Line 4:
:''This page is about the class of primitive functions. For the result of an empty reduction, see [[Identity element]].''
:''This page is about the class of primitive functions. For the result of an empty reduction, see [[Identity element]].''
An '''Identity function''', or '''tack function''', is one of the three [[primitive function]]s which returns one of its [[argument]]s with no modification:
An '''Identity function''', or '''tack function''', is one of the three [[primitive function]]s which returns one of its [[argument]]s with no modification:
* '''Identity''', '''Same''', or '''Pass''' (<source lang=apl inline>⊣</source> or <source lang=apl inline>⊢</source>) is [[monadic]] and returns its only argument.
* '''Identity''', '''Same''', or '''Pass''' (<syntaxhighlight lang=apl inline>⊣</source> or <syntaxhighlight lang=apl inline>⊢</source>) is [[monadic]] and returns its only argument.
* '''Left Identity''', or '''Left''' (<source lang=apl inline>⊣</source>) is [[dyadic]] and returns its left argument.
* '''Left Identity''', or '''Left''' (<syntaxhighlight lang=apl inline>⊣</source>) is [[dyadic]] and returns its left argument.
* '''Right Identity''', or '''Right''' (<source lang=apl inline>⊢</source>) is [[dyadic]] and returns its right argument.
* '''Right Identity''', or '''Right''' (<syntaxhighlight lang=apl inline>⊢</source>) is [[dyadic]] and returns its right argument.


The ''right tack'' glyph <source lang=apl inline>⊢</source>, when used for Right, is almost paired with Identity for the monadic case. ''Left tack'', <source lang=apl inline>⊣</source> is usually used for Identity as well, but may be given a different meaning, such as [[Stop]] (which returns the constant <source lang=apl inline>0 0⍴0</source>) in [[SHARP APL]] and [[APLX]], or [[Hide]] (which returns the constant <source lang=apl inline>0</source> as a [[shy]] result) in [[GNU APL]].
The ''right tack'' glyph <syntaxhighlight lang=apl inline>⊢</source>, when used for Right, is almost paired with Identity for the monadic case. ''Left tack'', <syntaxhighlight lang=apl inline>⊣</source> is usually used for Identity as well, but may be given a different meaning, such as [[Stop]] (which returns the constant <syntaxhighlight lang=apl inline>0 0⍴0</source>) in [[SHARP APL]] and [[APLX]], or [[Hide]] (which returns the constant <syntaxhighlight lang=apl inline>0</source> as a [[shy]] result) in [[GNU APL]].


Identity functions (Identity in particular) may be used like elements of [[APL syntax|syntax]] to break up [[stranding]], or to force a [[shy]] result to be shown. They can also be combined with an array-oriented [[operator]] to perform structural manipulations on arrays. Identity functions are a central feature of [[tacit programming]], in which functions and operators rather than names are used to direct the flow of arguments. The pairing of both Left and Right with monadic Identity makes it easier to design [[ambivalent]] functions which usefully work with one or two arguments.
Identity functions (Identity in particular) may be used like elements of [[APL syntax|syntax]] to break up [[stranding]], or to force a [[shy]] result to be shown. They can also be combined with an array-oriented [[operator]] to perform structural manipulations on arrays. Identity functions are a central feature of [[tacit programming]], in which functions and operators rather than names are used to direct the flow of arguments. The pairing of both Left and Right with monadic Identity makes it easier to design [[ambivalent]] functions which usefully work with one or two arguments.
Line 15: Line 15:


The [[monadic]] Identity function simply returns its [[argument]].
The [[monadic]] Identity function simply returns its [[argument]].
<source lang=apl>
<syntaxhighlight lang=apl>
       ⊢ 'argument'
       ⊢ 'argument'
argument
argument
Line 21: Line 21:
3.14
3.14
</source>
</source>
The result of an identity function is never [[shy]], even if the argument is. Thus the result of the second expression above is displayed, although the assignment <source lang=apl inline>pi←3.14</source> on its own would not produce any display.
The result of an identity function is never [[shy]], even if the argument is. Thus the result of the second expression above is displayed, although the assignment <syntaxhighlight lang=apl inline>pi←3.14</source> on its own would not produce any display.


Left and Right return the left and right arguments, respectively, when called [[dyad]]ically.
Left and Right return the left and right arguments, respectively, when called [[dyad]]ically.
<source lang=apl>
<syntaxhighlight lang=apl>
       'left' ⊣ 'right'
       'left' ⊣ 'right'
left
left
Line 34: Line 34:


As the left [[operand]] to [[Beside]], Right makes the resulting [[derived function]] ignore its left argument (so the result is produced by a [[monadic]] invocation of the right operand, on the right argument). The same pattern can be produced by using Right as the right operand to [[Atop]].
As the left [[operand]] to [[Beside]], Right makes the resulting [[derived function]] ignore its left argument (so the result is produced by a [[monadic]] invocation of the right operand, on the right argument). The same pattern can be produced by using Right as the right operand to [[Atop]].
<source lang=apl>
<syntaxhighlight lang=apl>
       2 ⊢∘- 7
       2 ⊢∘- 7
¯7
¯7
Line 41: Line 41:
</source>
</source>
In both cases the derived function, when called monadically, simply acts on the right argument, as there is no left argument to ignore.
In both cases the derived function, when called monadically, simply acts on the right argument, as there is no left argument to ignore.
<source lang=apl>
<syntaxhighlight lang=apl>
       ⊢∘- 7
       ⊢∘- 7
¯7
¯7
Line 48: Line 48:
</source>
</source>
The mirror image—using only the left argument while ignoring the right—is attained by using [[Atop]] (either the operator, or a 2-train) with Left as the right operand.
The mirror image—using only the left argument while ignoring the right—is attained by using [[Atop]] (either the operator, or a 2-train) with Left as the right operand.
<source lang=apl>
<syntaxhighlight lang=apl>
       2 -⍤⊣ 7
       2 -⍤⊣ 7
¯2
¯2
Line 55: Line 55:
</source>
</source>


Within a [[function train]] (as an "argument" function, that is, the rightmost function, or one an even number of steps away), Right indicates the right argument to the train, and Left indicates the left argument. The 3-train <source lang=apl inline>≠⊆⊢</source> thus applies [[Partition]] to the result of [[Not Equal to]] on both arguments and the right argument.
Within a [[function train]] (as an "argument" function, that is, the rightmost function, or one an even number of steps away), Right indicates the right argument to the train, and Left indicates the left argument. The 3-train <syntaxhighlight lang=apl inline>≠⊆⊢</source> thus applies [[Partition]] to the result of [[Not Equal to]] on both arguments and the right argument.
<source lang=apl>
<syntaxhighlight lang=apl>
       ' ' (≠⊆⊢) 'split on spaces'
       ' ' (≠⊆⊢) 'split on spaces'
┌─────┬──┬──────┐
┌─────┬──┬──────┐
Line 65: Line 65:
=== Structural manipulation ===
=== Structural manipulation ===


With [[Reduce]], Left selects the first [[element]]s along the reduction axis, and Right selects the last. For example, <source lang=apl inline>⊣⌿</source> gives the first [[major cell]] of an array while <source lang=apl inline>⊣/</source> gives the first element along each row, for example the first column of a [[matrix]].
With [[Reduce]], Left selects the first [[element]]s along the reduction axis, and Right selects the last. For example, <syntaxhighlight lang=apl inline>⊣⌿</source> gives the first [[major cell]] of an array while <syntaxhighlight lang=apl inline>⊣/</source> gives the first element along each row, for example the first column of a [[matrix]].
<source lang=apl>
<syntaxhighlight lang=apl>
       ⊢A ← 3 4⍴⍳12
       ⊢A ← 3 4⍴⍳12
1  2  3  4
1  2  3  4
Line 80: Line 80:
9 10 11 12
9 10 11 12
</source>
</source>
The combinations <source lang=apl inline>⊣/</source> <source lang=apl inline>⊣⌿</source> <source lang=apl inline>⊢/</source> <source lang=apl inline>⊢⌿</source> are [[Idiom recognition|recognized idioms]] in [[Dyalog APL]].
The combinations <syntaxhighlight lang=apl inline>⊣/</source> <syntaxhighlight lang=apl inline>⊣⌿</source> <syntaxhighlight lang=apl inline>⊢/</source> <syntaxhighlight lang=apl inline>⊢⌿</source> are [[Idiom recognition|recognized idioms]] in [[Dyalog APL]].


A [[Scan]] using Left extends the first element along each axis to the whole axis, while retaining the argument's shape. This is because a scan reduces on [[prefix]]es, and the first element of a prefix is the first element of the entire array. On the other hand, Right Scan doesn't change the argument, since the last element from each prefix gives the entire array.
A [[Scan]] using Left extends the first element along each axis to the whole axis, while retaining the argument's shape. This is because a scan reduces on [[prefix]]es, and the first element of a prefix is the first element of the entire array. On the other hand, Right Scan doesn't change the argument, since the last element from each prefix gives the entire array.
<source lang=apl>
<syntaxhighlight lang=apl>
       ⊣\ 'vector'
       ⊣\ 'vector'
vvvvvv
vvvvvv
Line 91: Line 91:


Right [[Each]] checks the arguments for [[conformability]] and returns the right argument, possibly applying [[scalar extension]] or [[singleton extension]]; Left Each does the same for the left argument.
Right [[Each]] checks the arguments for [[conformability]] and returns the right argument, possibly applying [[scalar extension]] or [[singleton extension]]; Left Each does the same for the left argument.
<source lang=apl>
<syntaxhighlight lang=apl>
       A ⊢¨ 0  ⍝ Extends the right argument
       A ⊢¨ 0  ⍝ Extends the right argument
0 0 0 0
0 0 0 0
Line 107: Line 107:


The [[outer product]] with Left adds the axes from the right argument to the left argument, while the outer product with Right adds the axes from the left argument to the right argument. In each case the resulting array is constant along any of the added axes. In the case of Right outer product, the result is composed of [[cell]]s matching the right argument, and can also be obtained by [[Reshape|reshaping]] the right argument.
The [[outer product]] with Left adds the axes from the right argument to the left argument, while the outer product with Right adds the axes from the left argument to the right argument. In each case the resulting array is constant along any of the added axes. In the case of Right outer product, the result is composed of [[cell]]s matching the right argument, and can also be obtained by [[Reshape|reshaping]] the right argument.
<source lang=apl>
<syntaxhighlight lang=apl>
       'left' ∘.⊣ ⍳6
       'left' ∘.⊣ ⍳6
llllll
llllll

Revision as of 21:28, 10 September 2022

This page is about the class of primitive functions. For the result of an empty reduction, see Identity element.

An Identity function, or tack function, is one of the three primitive functions which returns one of its arguments with no modification:

  • Identity, Same, or Pass (<syntaxhighlight lang=apl inline>⊣</source> or <syntaxhighlight lang=apl inline>⊢</source>) is monadic and returns its only argument.
  • Left Identity, or Left (<syntaxhighlight lang=apl inline>⊣</source>) is dyadic and returns its left argument.
  • Right Identity, or Right (<syntaxhighlight lang=apl inline>⊢</source>) is dyadic and returns its right argument.

The right tack glyph <syntaxhighlight lang=apl inline>⊢</source>, when used for Right, is almost paired with Identity for the monadic case. Left tack, <syntaxhighlight lang=apl inline>⊣</source> is usually used for Identity as well, but may be given a different meaning, such as Stop (which returns the constant <syntaxhighlight lang=apl inline>0 0⍴0</source>) in SHARP APL and APLX, or Hide (which returns the constant <syntaxhighlight lang=apl inline>0</source> as a shy result) in GNU APL.

Identity functions (Identity in particular) may be used like elements of syntax to break up stranding, or to force a shy result to be shown. They can also be combined with an array-oriented operator to perform structural manipulations on arrays. Identity functions are a central feature of tacit programming, in which functions and operators rather than names are used to direct the flow of arguments. The pairing of both Left and Right with monadic Identity makes it easier to design ambivalent functions which usefully work with one or two arguments.

Examples

The monadic Identity function simply returns its argument. <syntaxhighlight lang=apl>

     ⊢ 'argument'

argument

     ⊢pi←3.14

3.14 </source> The result of an identity function is never shy, even if the argument is. Thus the result of the second expression above is displayed, although the assignment <syntaxhighlight lang=apl inline>pi←3.14</source> on its own would not produce any display.

Left and Right return the left and right arguments, respectively, when called dyadically. <syntaxhighlight lang=apl>

     'left' ⊣ 'right'

left

     'left' ⊢ 'right'

right </source>

Uses

As the left operand to Beside, Right makes the resulting derived function ignore its left argument (so the result is produced by a monadic invocation of the right operand, on the right argument). The same pattern can be produced by using Right as the right operand to Atop. <syntaxhighlight lang=apl>

     2 ⊢∘- 7

¯7

     2 -⍤⊢ 7

¯7 </source> In both cases the derived function, when called monadically, simply acts on the right argument, as there is no left argument to ignore. <syntaxhighlight lang=apl>

     ⊢∘- 7

¯7

     -⍤⊢ 7

¯7 </source> The mirror image—using only the left argument while ignoring the right—is attained by using Atop (either the operator, or a 2-train) with Left as the right operand. <syntaxhighlight lang=apl>

     2 -⍤⊣ 7

¯2

     -⍤⊣ 2

¯2 </source>

Within a function train (as an "argument" function, that is, the rightmost function, or one an even number of steps away), Right indicates the right argument to the train, and Left indicates the left argument. The 3-train <syntaxhighlight lang=apl inline>≠⊆⊢</source> thus applies Partition to the result of Not Equal to on both arguments and the right argument. <syntaxhighlight lang=apl>

     ' ' (≠⊆⊢) 'split on spaces'

┌─────┬──┬──────┐ │split│on│spaces│ └─────┴──┴──────┘ </source>

Structural manipulation

With Reduce, Left selects the first elements along the reduction axis, and Right selects the last. For example, <syntaxhighlight lang=apl inline>⊣⌿</source> gives the first major cell of an array while <syntaxhighlight lang=apl inline>⊣/</source> gives the first element along each row, for example the first column of a matrix. <syntaxhighlight lang=apl>

     ⊢A ← 3 4⍴⍳12

1 2 3 4 5 6 7 8 9 10 11 12

     ⊣⌿ A  ⍝ First row

1 2 3 4

     ⊣/ A  ⍝ First column

1 5 9

     ⊢/ A  ⍝ Last row

4 8 12

     ⊢⌿ A  ⍝ Last column

9 10 11 12 </source> The combinations <syntaxhighlight lang=apl inline>⊣/</source> <syntaxhighlight lang=apl inline>⊣⌿</source> <syntaxhighlight lang=apl inline>⊢/</source> <syntaxhighlight lang=apl inline>⊢⌿</source> are recognized idioms in Dyalog APL.

A Scan using Left extends the first element along each axis to the whole axis, while retaining the argument's shape. This is because a scan reduces on prefixes, and the first element of a prefix is the first element of the entire array. On the other hand, Right Scan doesn't change the argument, since the last element from each prefix gives the entire array. <syntaxhighlight lang=apl>

     ⊣\ 'vector'

vvvvvv

     ⊢\ 'vector'

vector </source>

Right Each checks the arguments for conformability and returns the right argument, possibly applying scalar extension or singleton extension; Left Each does the same for the left argument. <syntaxhighlight lang=apl>

     A ⊢¨ 0  ⍝ Extends the right argument

0 0 0 0 0 0 0 0 0 0 0 0

     A ⊣¨ 0  ⍝ No need to extend the left argument

1 2 3 4 5 6 7 8 9 10 11 12

     A ⊢¨ 1 2 3  ⍝ Non-conforming arguments

RANK ERROR

     A⊢¨1 2 3
      ∧

</source>

The outer product with Left adds the axes from the right argument to the left argument, while the outer product with Right adds the axes from the left argument to the right argument. In each case the resulting array is constant along any of the added axes. In the case of Right outer product, the result is composed of cells matching the right argument, and can also be obtained by reshaping the right argument. <syntaxhighlight lang=apl>

     'left' ∘.⊣ ⍳6

llllll eeeeee ffffff tttttt

     'left' ∘.⊢ ⍳6  ⍝ Identical to 4 6⍴⍳6

1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 </source>

External links

Tutorials

Documentation


APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
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 axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector