Identity element: Difference between revisions
m (Text replacement - "http://help.dyalog.com" to "https://help.dyalog.com") |
m (Text replacement - "<source" to "<syntaxhighlight") |
||
Line 3: | Line 3: | ||
== Left and right identities == | == Left and right identities == | ||
Since the identity element preserves the ''other'' argument, it can be a left and/or a right identity. For example, [[Add]] (< | Since the identity element preserves the ''other'' argument, it can be a left and/or a right identity. For example, [[Add]] (<syntaxhighlight lang=apl inline>+</source>) has the left and right identity element <syntaxhighlight lang=apl inline>0</source> because <syntaxhighlight lang=apl inline>N≡N+0</source> and <syntaxhighlight lang=apl inline>N≡0+N</source> for all arrays <syntaxhighlight lang=apl inline>N</source> in the domain of <syntaxhighlight lang=apl inline>+</source>. However, the identity of [[Divide]] (<syntaxhighlight lang=apl inline>÷</source>), <syntaxhighlight lang=apl inline>1</source>, is only a right identity because while <syntaxhighlight lang=apl inline>N≡N÷1</source> is true for all <syntaxhighlight lang=apl inline>N</source> in the domain of <syntaxhighlight lang=apl inline>÷</source>, this isn't so for <syntaxhighlight lang=apl inline>N≡1÷N</source>, and no alternative identity element value exists which would fulfil the condition. | ||
If a function < | If a function <syntaxhighlight lang=apl inline>f</source> has both a left identity element and a right identity element (call them <syntaxhighlight lang=apl inline>l</source> and <syntaxhighlight lang=apl inline>r</source>), then they must be the same. This is because <syntaxhighlight lang=apl inline>l f r</source> {{←→}} <syntaxhighlight lang=apl inline>r</source>, since <syntaxhighlight lang=apl inline>l</source> is a left identity, and <syntaxhighlight lang=apl inline>l f r</source> {{←→}} <syntaxhighlight lang=apl inline>l</source>, since <syntaxhighlight lang=apl inline>r</source> is a right identity, so <syntaxhighlight lang=apl inline>l</source> {{←→}} <syntaxhighlight lang=apl inline>r</source>. | ||
== Reduction over a length-0 axis == | == Reduction over a length-0 axis == | ||
If a [[reduce|reduction]] (using one of < | If a [[reduce|reduction]] (using one of <syntaxhighlight lang=apl inline>/</source>, <syntaxhighlight lang=apl inline>⌿</source>, <syntaxhighlight lang=apl inline>\</source>, or <syntaxhighlight lang=apl inline>⍀</source>) is performed over an axis of length 0, the resulting array is filled with identity elements. For example, the sum of an empty list is <syntaxhighlight lang=apl inline>0</source>, while the columnar sum of a two-column [[matrix]] with no rows is <syntaxhighlight lang=apl inline>0 0</source>: | ||
< | <syntaxhighlight lang=apl> | ||
+/0⍴0 | +/0⍴0 | ||
0 | 0 | ||
Line 21: | Line 21: | ||
Dialects differ in their support for such reductions. All define identity elements for most scalar primitives, and some stop there (e.g. SAX), while others (e.g. Dyalog APL and APL2) define identity elements for various mixed functions. | Dialects differ in their support for such reductions. All define identity elements for most scalar primitives, and some stop there (e.g. SAX), while others (e.g. Dyalog APL and APL2) define identity elements for various mixed functions. | ||
The identity element value for each function is defined in terms of the [[prototype]] < | The identity element value for each function is defined in terms of the [[prototype]] <syntaxhighlight lang=apl inline>P</source> of the array <syntaxhighlight lang=apl inline>Y</source>: | ||
{| class=wikitable | {| class=wikitable | ||
! Function name || [[Glyph]] || Value || Left || Right || Notes | ! Function name || [[Glyph]] || Value || Left || Right || Notes | ||
|- | |- | ||
| [[Add]] || < | | [[Add]] || <syntaxhighlight lang=apl inline>+</source> || <syntaxhighlight lang=apl inline>0</source> || {{Yes}} || {{Yes}} || | ||
|- | |- | ||
| [[Subtract]] || < | | [[Subtract]] || <syntaxhighlight lang=apl inline>-</source> || <syntaxhighlight lang=apl inline>0</source> || {{No}} || {{Yes}} || | ||
|- | |- | ||
| [[Multiply]] || < | | [[Multiply]] || <syntaxhighlight lang=apl inline>×</source> || <syntaxhighlight lang=apl inline>1</source> || {{Yes}} || {{Yes}} || | ||
|- | |- | ||
| [[Divide]] || < | | [[Divide]] || <syntaxhighlight lang=apl inline>÷</source> || <syntaxhighlight lang=apl inline>1</source> || {{No}} || {{Yes}} || | ||
|- | |- | ||
| [[Residue]] || < | | [[Residue]] || <syntaxhighlight lang=apl inline>|</source> || <syntaxhighlight lang=apl inline>0</source> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Minimum]] || < | | [[Minimum]] || <syntaxhighlight lang=apl inline>⌊</source> || <syntaxhighlight lang=apl inline>∞</source> || {{Yes}} || {{Yes}} || the maximum representable number | ||
|- | |- | ||
| [[Maximum]] || < | | [[Maximum]] || <syntaxhighlight lang=apl inline>⌈</source> || <syntaxhighlight lang=apl inline>-∞</source> || {{Yes}} || {{Yes}} || the minimum representable number | ||
|- | |- | ||
| [[Power]] || < | | [[Power]] || <syntaxhighlight lang=apl inline>*</source> || <syntaxhighlight lang=apl inline>1</source> || {{No}} || {{Yes}} || | ||
|- | |- | ||
| [[Circle function]] || < | | [[Circle function]] || <syntaxhighlight lang=apl inline>○</source> || <syntaxhighlight lang=apl inline>¯9</source> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Binomial]] || < | | [[Binomial]] || <syntaxhighlight lang=apl inline>!</source> || <syntaxhighlight lang=apl inline>1</source> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Root]] || < | | [[Root]] || <syntaxhighlight lang=apl inline>√</source> || <syntaxhighlight lang=apl inline>1</source> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[And]]/[[LCM]] || < | | [[And]]/[[LCM]] || <syntaxhighlight lang=apl inline>∧</source> || <syntaxhighlight lang=apl inline>1</source> || {{Yes}} || {{Yes}} || | ||
|- | |- | ||
| [[Or]]/[[GCD]] || < | | [[Or]]/[[GCD]] || <syntaxhighlight lang=apl inline>∨</source> || <syntaxhighlight lang=apl inline>0</source> || {{Yes}} || {{Yes}} || Non-negative reals only | ||
|- | |- | ||
| [[Less]] || < | | [[Less]] || <syntaxhighlight lang=apl inline><</source> || <syntaxhighlight lang=apl inline>0</source> || {{Yes}} || {{No}} || [[Boolean]]s only | ||
|- | |- | ||
| [[Less Or Equal]] || < | | [[Less Or Equal]] || <syntaxhighlight lang=apl inline>≤</source> || <syntaxhighlight lang=apl inline>1</source> || {{Yes}} || {{No}} || [[Boolean]]s only | ||
|- | |- | ||
| [[Equal to]] || < | | [[Equal to]] || <syntaxhighlight lang=apl inline>=</source> || <syntaxhighlight lang=apl inline>1</source> || {{Yes}} || {{Yes}} || [[Boolean]]s only | ||
|- | |- | ||
| [[Greater Or Equal]] || < | | [[Greater Or Equal]] || <syntaxhighlight lang=apl inline>≥</source> || <syntaxhighlight lang=apl inline>1</source> || {{No}} || {{Yes}} || [[Boolean]]s only | ||
|- | |- | ||
| [[Greater]] || < | | [[Greater]] || <syntaxhighlight lang=apl inline>></source> || <syntaxhighlight lang=apl inline>0</source> || {{No}} || {{Yes}} || [[Boolean]]s only | ||
|- | |- | ||
| [[Not Equal]] || < | | [[Not Equal]] || <syntaxhighlight lang=apl inline>≠</source> || <syntaxhighlight lang=apl inline>0</source> || {{Yes}} || {{Yes}} || [[Boolean]]s only | ||
|- | |- | ||
| [[Reshape]] || < | | [[Reshape]] || <syntaxhighlight lang=apl inline>⍴</source> || <syntaxhighlight lang=apl inline>⍴P</source> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Catenate]] || < | | [[Catenate]] || <syntaxhighlight lang=apl inline>,</source> || <syntaxhighlight lang=apl inline>P⍴⍨ρ∘⊂⍨0,⍨¯1↓ρP</source> || {{Yes}} || {{No}} || <syntaxhighlight lang=apl inline>1≤≢⍴Y</source> | ||
|- | |- | ||
| [[Rotate]] || < | | [[Rotate]] || <syntaxhighlight lang=apl inline>⌽</source> || <syntaxhighlight lang=apl inline>0</source> or <syntaxhighlight lang=apl inline>0⍴⍨¯1↓⍴P</source> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Rotate First]] || < | | [[Rotate First]] || <syntaxhighlight lang=apl inline>⊖</source> || <syntaxhighlight lang=apl inline>0</source> or <syntaxhighlight lang=apl inline>0⍴⍨1↓⍴P</source> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Transpose]] || < | | [[Transpose]] || <syntaxhighlight lang=apl inline>⍉</source> || <syntaxhighlight lang=apl inline>⍳≢⍴P</source> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Pick]] || < | | [[Pick]] || <syntaxhighlight lang=apl inline>⊃</source> || <syntaxhighlight lang=apl inline>⍬</source> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Drop]] || < | | [[Drop]] || <syntaxhighlight lang=apl inline>↓</source> || <syntaxhighlight lang=apl inline>⍬</source> or <syntaxhighlight lang=apl inline>0×⍴P</source> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Take]] || < | | [[Take]] || <syntaxhighlight lang=apl inline>↑</source> || <syntaxhighlight lang=apl inline>⍬</source> or <syntaxhighlight lang=apl inline>⍴P</source>|| {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Squad Index]] || < | | [[Squad Index]] || <syntaxhighlight lang=apl inline>⌷</source> || <syntaxhighlight lang=apl inline>⍬</source> or <syntaxhighlight lang=apl inline>⍳¨⍴P</source>|| {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Without]] || < | | [[Without]] || <syntaxhighlight lang=apl inline>~</source> || <syntaxhighlight lang=apl inline>0⌿P</source> || {{No}} || {{Yes}} || <syntaxhighlight lang=apl inline>1≤≢⍴Y</source> | ||
|- | |- | ||
| [[Matrix Divide]] || < | | [[Matrix Divide]] || <syntaxhighlight lang=apl inline>⌹</source> || <syntaxhighlight lang=apl inline>∘.=⍨⍳≢P</source> || {{No}} || {{Yes}} || | ||
|- | |- | ||
| [[Encode]] || < | | [[Encode]] || <syntaxhighlight lang=apl inline>⊤</source> || <syntaxhighlight lang=apl inline>0</source> || {{No}} || {{Yes}} || | ||
|- | |- | ||
| [[Union]] || < | | [[Union]] || <syntaxhighlight lang=apl inline>∪</source> || <syntaxhighlight lang=apl inline>0⌿P</source> || {{Yes}} || {{Yes}} || <syntaxhighlight lang=apl inline>1≤≢⍴Y</source> | ||
|- | |- | ||
| [[Replicate]] || < | | [[Replicate]] || <syntaxhighlight lang=apl inline>/</source> || <syntaxhighlight lang=apl inline>1</source> || {{Yes}} || {{No}} || <syntaxhighlight lang=apl inline>1≤≢⍴Y</source> | ||
|- | |- | ||
| [[Expand]] || < | | [[Expand]] || <syntaxhighlight lang=apl inline>\</source> || <syntaxhighlight lang=apl inline>∘.=⍨⍳≢P</source> || {{Yes}} || {{No}} || <syntaxhighlight lang=apl inline>1≤≢⍴Y</source> | ||
|- | |- | ||
| [[Inner product]]s || < | | [[Inner product]]s || <syntaxhighlight lang=apl inline>+.×</source><br><syntaxhighlight lang=apl inline>∨.∧</source> || <syntaxhighlight lang=apl inline>∘.=⍨⍳≢P</source> || {{Yes}} || {{Yes}} || | ||
|- | |- | ||
| [[Inner product]] || < | | [[Inner product]] || <syntaxhighlight lang=apl inline>∧.∨</source> || <syntaxhighlight lang=apl inline>∘.≠⍨⍳≢P</source> || {{Yes}} || {{Yes}} || | ||
|} | |} | ||
Revision as of 22:04, 10 September 2022
The identity element for a dyadic function is a value inherent to that function. It is defined as the value which would preserve the other argument of the dyadic function application, possibly only for a well-defined subset of the function's domain.
Left and right identities
Since the identity element preserves the other argument, it can be a left and/or a right identity. For example, Add (<syntaxhighlight lang=apl inline>+</source>) has the left and right identity element <syntaxhighlight lang=apl inline>0</source> because <syntaxhighlight lang=apl inline>N≡N+0</source> and <syntaxhighlight lang=apl inline>N≡0+N</source> for all arrays <syntaxhighlight lang=apl inline>N</source> in the domain of <syntaxhighlight lang=apl inline>+</source>. However, the identity of Divide (<syntaxhighlight lang=apl inline>÷</source>), <syntaxhighlight lang=apl inline>1</source>, is only a right identity because while <syntaxhighlight lang=apl inline>N≡N÷1</source> is true for all <syntaxhighlight lang=apl inline>N</source> in the domain of <syntaxhighlight lang=apl inline>÷</source>, this isn't so for <syntaxhighlight lang=apl inline>N≡1÷N</source>, and no alternative identity element value exists which would fulfil the condition.
If a function <syntaxhighlight lang=apl inline>f</source> has both a left identity element and a right identity element (call them <syntaxhighlight lang=apl inline>l</source> and <syntaxhighlight lang=apl inline>r</source>), then they must be the same. This is because <syntaxhighlight lang=apl inline>l f r</source> <syntaxhighlight lang=apl inline>r</source>, since <syntaxhighlight lang=apl inline>l</source> is a left identity, and <syntaxhighlight lang=apl inline>l f r</source> <syntaxhighlight lang=apl inline>l</source>, since <syntaxhighlight lang=apl inline>r</source> is a right identity, so <syntaxhighlight lang=apl inline>l</source> <syntaxhighlight lang=apl inline>r</source>.
Reduction over a length-0 axis
If a reduction (using one of <syntaxhighlight lang=apl inline>/</source>, <syntaxhighlight lang=apl inline>⌿</source>, <syntaxhighlight lang=apl inline>\</source>, or <syntaxhighlight lang=apl inline>⍀</source>) is performed over an axis of length 0, the resulting array is filled with identity elements. For example, the sum of an empty list is <syntaxhighlight lang=apl inline>0</source>, while the columnar sum of a two-column matrix with no rows is <syntaxhighlight lang=apl inline>0 0</source>: <syntaxhighlight lang=apl>
+/0⍴0
0
+/0 2⍴0
0 0 </source>
Support
Dialects differ in their support for such reductions. All define identity elements for most scalar primitives, and some stop there (e.g. SAX), while others (e.g. Dyalog APL and APL2) define identity elements for various mixed functions.
The identity element value for each function is defined in terms of the prototype <syntaxhighlight lang=apl inline>P</source> of the array <syntaxhighlight lang=apl inline>Y</source>:
Function name | Glyph | Value | Left | Right | Notes |
---|---|---|---|---|---|
Add | <syntaxhighlight lang=apl inline>+</source> | <syntaxhighlight lang=apl inline>0</source> | Yes | Yes | |
Subtract | <syntaxhighlight lang=apl inline>-</source> | <syntaxhighlight lang=apl inline>0</source> | No | Yes | |
Multiply | <syntaxhighlight lang=apl inline>×</source> | <syntaxhighlight lang=apl inline>1</source> | Yes | Yes | |
Divide | <syntaxhighlight lang=apl inline>÷</source> | <syntaxhighlight lang=apl inline>1</source> | No | Yes | |
Residue | </source> | <syntaxhighlight lang=apl inline>0</source> | Yes | No | |
Minimum | <syntaxhighlight lang=apl inline>⌊</source> | <syntaxhighlight lang=apl inline>∞</source> | Yes | Yes | the maximum representable number |
Maximum | <syntaxhighlight lang=apl inline>⌈</source> | <syntaxhighlight lang=apl inline>-∞</source> | Yes | Yes | the minimum representable number |
Power | <syntaxhighlight lang=apl inline>*</source> | <syntaxhighlight lang=apl inline>1</source> | No | Yes | |
Circle function | <syntaxhighlight lang=apl inline>○</source> | <syntaxhighlight lang=apl inline>¯9</source> | Yes | No | |
Binomial | <syntaxhighlight lang=apl inline>!</source> | <syntaxhighlight lang=apl inline>1</source> | Yes | No | |
Root | <syntaxhighlight lang=apl inline>√</source> | <syntaxhighlight lang=apl inline>1</source> | Yes | No | |
And/LCM | <syntaxhighlight lang=apl inline>∧</source> | <syntaxhighlight lang=apl inline>1</source> | Yes | Yes | |
Or/GCD | <syntaxhighlight lang=apl inline>∨</source> | <syntaxhighlight lang=apl inline>0</source> | Yes | Yes | Non-negative reals only |
Less | <syntaxhighlight lang=apl inline><</source> | <syntaxhighlight lang=apl inline>0</source> | Yes | No | Booleans only |
Less Or Equal | <syntaxhighlight lang=apl inline>≤</source> | <syntaxhighlight lang=apl inline>1</source> | Yes | No | Booleans only |
Equal to | <syntaxhighlight lang=apl inline>=</source> | <syntaxhighlight lang=apl inline>1</source> | Yes | Yes | Booleans only |
Greater Or Equal | <syntaxhighlight lang=apl inline>≥</source> | <syntaxhighlight lang=apl inline>1</source> | No | Yes | Booleans only |
Greater | <syntaxhighlight lang=apl inline>></source> | <syntaxhighlight lang=apl inline>0</source> | No | Yes | Booleans only |
Not Equal | <syntaxhighlight lang=apl inline>≠</source> | <syntaxhighlight lang=apl inline>0</source> | Yes | Yes | Booleans only |
Reshape | <syntaxhighlight lang=apl inline>⍴</source> | <syntaxhighlight lang=apl inline>⍴P</source> | Yes | No | |
Catenate | <syntaxhighlight lang=apl inline>,</source> | <syntaxhighlight lang=apl inline>P⍴⍨ρ∘⊂⍨0,⍨¯1↓ρP</source> | Yes | No | <syntaxhighlight lang=apl inline>1≤≢⍴Y</source> |
Rotate | <syntaxhighlight lang=apl inline>⌽</source> | <syntaxhighlight lang=apl inline>0</source> or <syntaxhighlight lang=apl inline>0⍴⍨¯1↓⍴P</source> | Yes | No | |
Rotate First | <syntaxhighlight lang=apl inline>⊖</source> | <syntaxhighlight lang=apl inline>0</source> or <syntaxhighlight lang=apl inline>0⍴⍨1↓⍴P</source> | Yes | No | |
Transpose | <syntaxhighlight lang=apl inline>⍉</source> | <syntaxhighlight lang=apl inline>⍳≢⍴P</source> | Yes | No | |
Pick | <syntaxhighlight lang=apl inline>⊃</source> | <syntaxhighlight lang=apl inline>⍬</source> | Yes | No | |
Drop | <syntaxhighlight lang=apl inline>↓</source> | <syntaxhighlight lang=apl inline>⍬</source> or <syntaxhighlight lang=apl inline>0×⍴P</source> | Yes | No | |
Take | <syntaxhighlight lang=apl inline>↑</source> | <syntaxhighlight lang=apl inline>⍬</source> or <syntaxhighlight lang=apl inline>⍴P</source> | Yes | No | |
Squad Index | <syntaxhighlight lang=apl inline>⌷</source> | <syntaxhighlight lang=apl inline>⍬</source> or <syntaxhighlight lang=apl inline>⍳¨⍴P</source> | Yes | No | |
Without | <syntaxhighlight lang=apl inline>~</source> | <syntaxhighlight lang=apl inline>0⌿P</source> | No | Yes | <syntaxhighlight lang=apl inline>1≤≢⍴Y</source> |
Matrix Divide | <syntaxhighlight lang=apl inline>⌹</source> | <syntaxhighlight lang=apl inline>∘.=⍨⍳≢P</source> | No | Yes | |
Encode | <syntaxhighlight lang=apl inline>⊤</source> | <syntaxhighlight lang=apl inline>0</source> | No | Yes | |
Union | <syntaxhighlight lang=apl inline>∪</source> | <syntaxhighlight lang=apl inline>0⌿P</source> | Yes | Yes | <syntaxhighlight lang=apl inline>1≤≢⍴Y</source> |
Replicate | <syntaxhighlight lang=apl inline>/</source> | <syntaxhighlight lang=apl inline>1</source> | Yes | No | <syntaxhighlight lang=apl inline>1≤≢⍴Y</source> |
Expand | <syntaxhighlight lang=apl inline>\</source> | <syntaxhighlight lang=apl inline>∘.=⍨⍳≢P</source> | Yes | No | <syntaxhighlight lang=apl inline>1≤≢⍴Y</source> |
Inner products | <syntaxhighlight lang=apl inline>+.×</source> <syntaxhighlight lang=apl inline>∨.∧</source> |
<syntaxhighlight lang=apl inline>∘.=⍨⍳≢P</source> | Yes | Yes | |
Inner product | <syntaxhighlight lang=apl inline>∧.∨</source> | <syntaxhighlight lang=apl inline>∘.≠⍨⍳≢P</source> | Yes | Yes |
External links
Documentation
APL features [edit] | |
---|---|
Built-ins | Primitives (functions, operators) ∙ Quad name |
Array model | Shape ∙ Rank ∙ Depth ∙ Bound ∙ Index (Indexing) ∙ Axis ∙ Ravel ∙ Ravel order ∙ Element ∙ Scalar ∙ Vector ∙ Matrix ∙ Simple scalar ∙ Simple array ∙ Nested array ∙ Cell ∙ Major cell ∙ Subarray ∙ Empty array ∙ Prototype |
Data types | Number (Boolean, Complex number) ∙ Character (String) ∙ Box ∙ Namespace ∙ Function array |
Concepts and paradigms | Conformability (Scalar extension, Leading axis agreement) ∙ Scalar function (Pervasion) ∙ Identity element ∙ Complex floor ∙ Array ordering (Total) ∙ Tacit programming (Function composition, Close composition) ∙ Glyph ∙ Leading axis theory ∙ Major cell search ∙ First-class function |
Errors | LIMIT ERROR ∙ RANK ERROR ∙ SYNTAX ERROR ∙ DOMAIN ERROR ∙ LENGTH ERROR ∙ INDEX ERROR ∙ VALUE ERROR ∙ EVOLUTION ERROR |