Identity element: Difference between revisions
m (Text replacement - "http://help.dyalog.com" to "https://help.dyalog.com") |
m (Text replacement - "</source>" to "</syntaxhighlight>") |
||
(One intermediate revision by the same user not shown) | |||
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>+</syntaxhighlight>) has the left and right identity element <syntaxhighlight lang=apl inline>0</syntaxhighlight> because <syntaxhighlight lang=apl inline>N≡N+0</syntaxhighlight> and <syntaxhighlight lang=apl inline>N≡0+N</syntaxhighlight> for all arrays <syntaxhighlight lang=apl inline>N</syntaxhighlight> in the domain of <syntaxhighlight lang=apl inline>+</syntaxhighlight>. However, the identity of [[Divide]] (<syntaxhighlight lang=apl inline>÷</syntaxhighlight>), <syntaxhighlight lang=apl inline>1</syntaxhighlight>, is only a right identity because while <syntaxhighlight lang=apl inline>N≡N÷1</syntaxhighlight> is true for all <syntaxhighlight lang=apl inline>N</syntaxhighlight> in the domain of <syntaxhighlight lang=apl inline>÷</syntaxhighlight>, this isn't so for <syntaxhighlight lang=apl inline>N≡1÷N</syntaxhighlight>, and no alternative identity element value exists which would fulfil the condition. | ||
If a function < | If a function <syntaxhighlight lang=apl inline>f</syntaxhighlight> has both a left identity element and a right identity element (call them <syntaxhighlight lang=apl inline>l</syntaxhighlight> and <syntaxhighlight lang=apl inline>r</syntaxhighlight>), then they must be the same. This is because <syntaxhighlight lang=apl inline>l f r</syntaxhighlight> {{←→}} <syntaxhighlight lang=apl inline>r</syntaxhighlight>, since <syntaxhighlight lang=apl inline>l</syntaxhighlight> is a left identity, and <syntaxhighlight lang=apl inline>l f r</syntaxhighlight> {{←→}} <syntaxhighlight lang=apl inline>l</syntaxhighlight>, since <syntaxhighlight lang=apl inline>r</syntaxhighlight> is a right identity, so <syntaxhighlight lang=apl inline>l</syntaxhighlight> {{←→}} <syntaxhighlight lang=apl inline>r</syntaxhighlight>. | ||
== 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>/</syntaxhighlight>, <syntaxhighlight lang=apl inline>⌿</syntaxhighlight>, <syntaxhighlight lang=apl inline>\</syntaxhighlight>, or <syntaxhighlight lang=apl inline>⍀</syntaxhighlight>) 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</syntaxhighlight>, while the columnar sum of a two-column [[matrix]] with no rows is <syntaxhighlight lang=apl inline>0 0</syntaxhighlight>: | ||
< | <syntaxhighlight lang=apl> | ||
+/0⍴0 | +/0⍴0 | ||
0 | 0 | ||
+/0 2⍴0 | +/0 2⍴0 | ||
0 0 | 0 0 | ||
</ | </syntaxhighlight> | ||
== Support == | == Support == | ||
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</syntaxhighlight> of the array <syntaxhighlight lang=apl inline>Y</syntaxhighlight>: | ||
{| class=wikitable | {| class=wikitable | ||
! Function name || [[Glyph]] || Value || Left || Right || Notes | ! Function name || [[Glyph]] || Value || Left || Right || Notes | ||
|- | |- | ||
| [[Add]] || < | | [[Add]] || <syntaxhighlight lang=apl inline>+</syntaxhighlight> || <syntaxhighlight lang=apl inline>0</syntaxhighlight> || {{Yes}} || {{Yes}} || | ||
|- | |- | ||
| [[Subtract]] || < | | [[Subtract]] || <syntaxhighlight lang=apl inline>-</syntaxhighlight> || <syntaxhighlight lang=apl inline>0</syntaxhighlight> || {{No}} || {{Yes}} || | ||
|- | |- | ||
| [[Multiply]] || < | | [[Multiply]] || <syntaxhighlight lang=apl inline>×</syntaxhighlight> || <syntaxhighlight lang=apl inline>1</syntaxhighlight> || {{Yes}} || {{Yes}} || | ||
|- | |- | ||
| [[Divide]] || < | | [[Divide]] || <syntaxhighlight lang=apl inline>÷</syntaxhighlight> || <syntaxhighlight lang=apl inline>1</syntaxhighlight> || {{No}} || {{Yes}} || | ||
|- | |- | ||
| [[Residue]] || < | | [[Residue]] || <syntaxhighlight lang=apl inline>|</syntaxhighlight> || <syntaxhighlight lang=apl inline>0</syntaxhighlight> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Minimum]] || < | | [[Minimum]] || <syntaxhighlight lang=apl inline>⌊</syntaxhighlight> || <syntaxhighlight lang=apl inline>∞</syntaxhighlight> || {{Yes}} || {{Yes}} || the maximum representable number | ||
|- | |- | ||
| [[Maximum]] || < | | [[Maximum]] || <syntaxhighlight lang=apl inline>⌈</syntaxhighlight> || <syntaxhighlight lang=apl inline>-∞</syntaxhighlight> || {{Yes}} || {{Yes}} || the minimum representable number | ||
|- | |- | ||
| [[Power]] || < | | [[Power]] || <syntaxhighlight lang=apl inline>*</syntaxhighlight> || <syntaxhighlight lang=apl inline>1</syntaxhighlight> || {{No}} || {{Yes}} || | ||
|- | |- | ||
| [[Circle function]] || < | | [[Circle function]] || <syntaxhighlight lang=apl inline>○</syntaxhighlight> || <syntaxhighlight lang=apl inline>¯9</syntaxhighlight> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Binomial]] || < | | [[Binomial]] || <syntaxhighlight lang=apl inline>!</syntaxhighlight> || <syntaxhighlight lang=apl inline>1</syntaxhighlight> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Root]] || < | | [[Root]] || <syntaxhighlight lang=apl inline>√</syntaxhighlight> || <syntaxhighlight lang=apl inline>1</syntaxhighlight> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[And]]/[[LCM]] || < | | [[And]]/[[LCM]] || <syntaxhighlight lang=apl inline>∧</syntaxhighlight> || <syntaxhighlight lang=apl inline>1</syntaxhighlight> || {{Yes}} || {{Yes}} || | ||
|- | |- | ||
| [[Or]]/[[GCD]] || < | | [[Or]]/[[GCD]] || <syntaxhighlight lang=apl inline>∨</syntaxhighlight> || <syntaxhighlight lang=apl inline>0</syntaxhighlight> || {{Yes}} || {{Yes}} || Non-negative reals only | ||
|- | |- | ||
| [[Less]] || < | | [[Less]] || <syntaxhighlight lang=apl inline><</syntaxhighlight> || <syntaxhighlight lang=apl inline>0</syntaxhighlight> || {{Yes}} || {{No}} || [[Boolean]]s only | ||
|- | |- | ||
| [[Less Or Equal]] || < | | [[Less Or Equal]] || <syntaxhighlight lang=apl inline>≤</syntaxhighlight> || <syntaxhighlight lang=apl inline>1</syntaxhighlight> || {{Yes}} || {{No}} || [[Boolean]]s only | ||
|- | |- | ||
| [[Equal to]] || < | | [[Equal to]] || <syntaxhighlight lang=apl inline>=</syntaxhighlight> || <syntaxhighlight lang=apl inline>1</syntaxhighlight> || {{Yes}} || {{Yes}} || [[Boolean]]s only | ||
|- | |- | ||
| [[Greater Or Equal]] || < | | [[Greater Or Equal]] || <syntaxhighlight lang=apl inline>≥</syntaxhighlight> || <syntaxhighlight lang=apl inline>1</syntaxhighlight> || {{No}} || {{Yes}} || [[Boolean]]s only | ||
|- | |- | ||
| [[Greater]] || < | | [[Greater]] || <syntaxhighlight lang=apl inline>></syntaxhighlight> || <syntaxhighlight lang=apl inline>0</syntaxhighlight> || {{No}} || {{Yes}} || [[Boolean]]s only | ||
|- | |- | ||
| [[Not Equal]] || < | | [[Not Equal]] || <syntaxhighlight lang=apl inline>≠</syntaxhighlight> || <syntaxhighlight lang=apl inline>0</syntaxhighlight> || {{Yes}} || {{Yes}} || [[Boolean]]s only | ||
|- | |- | ||
| [[Reshape]] || < | | [[Reshape]] || <syntaxhighlight lang=apl inline>⍴</syntaxhighlight> || <syntaxhighlight lang=apl inline>⍴P</syntaxhighlight> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Catenate]] || < | | [[Catenate]] || <syntaxhighlight lang=apl inline>,</syntaxhighlight> || <syntaxhighlight lang=apl inline>P⍴⍨ρ∘⊂⍨0,⍨¯1↓ρP</syntaxhighlight> || {{Yes}} || {{No}} || <syntaxhighlight lang=apl inline>1≤≢⍴Y</syntaxhighlight> | ||
|- | |- | ||
| [[Rotate]] || < | | [[Rotate]] || <syntaxhighlight lang=apl inline>⌽</syntaxhighlight> || <syntaxhighlight lang=apl inline>0</syntaxhighlight> or <syntaxhighlight lang=apl inline>0⍴⍨¯1↓⍴P</syntaxhighlight> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Rotate First]] || < | | [[Rotate First]] || <syntaxhighlight lang=apl inline>⊖</syntaxhighlight> || <syntaxhighlight lang=apl inline>0</syntaxhighlight> or <syntaxhighlight lang=apl inline>0⍴⍨1↓⍴P</syntaxhighlight> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Transpose]] || < | | [[Transpose]] || <syntaxhighlight lang=apl inline>⍉</syntaxhighlight> || <syntaxhighlight lang=apl inline>⍳≢⍴P</syntaxhighlight> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Pick]] || < | | [[Pick]] || <syntaxhighlight lang=apl inline>⊃</syntaxhighlight> || <syntaxhighlight lang=apl inline>⍬</syntaxhighlight> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Drop]] || < | | [[Drop]] || <syntaxhighlight lang=apl inline>↓</syntaxhighlight> || <syntaxhighlight lang=apl inline>⍬</syntaxhighlight> or <syntaxhighlight lang=apl inline>0×⍴P</syntaxhighlight> || {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Take]] || < | | [[Take]] || <syntaxhighlight lang=apl inline>↑</syntaxhighlight> || <syntaxhighlight lang=apl inline>⍬</syntaxhighlight> or <syntaxhighlight lang=apl inline>⍴P</syntaxhighlight>|| {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Squad Index]] || < | | [[Squad Index]] || <syntaxhighlight lang=apl inline>⌷</syntaxhighlight> || <syntaxhighlight lang=apl inline>⍬</syntaxhighlight> or <syntaxhighlight lang=apl inline>⍳¨⍴P</syntaxhighlight>|| {{Yes}} || {{No}} || | ||
|- | |- | ||
| [[Without]] || < | | [[Without]] || <syntaxhighlight lang=apl inline>~</syntaxhighlight> || <syntaxhighlight lang=apl inline>0⌿P</syntaxhighlight> || {{No}} || {{Yes}} || <syntaxhighlight lang=apl inline>1≤≢⍴Y</syntaxhighlight> | ||
|- | |- | ||
| [[Matrix Divide]] || < | | [[Matrix Divide]] || <syntaxhighlight lang=apl inline>⌹</syntaxhighlight> || <syntaxhighlight lang=apl inline>∘.=⍨⍳≢P</syntaxhighlight> || {{No}} || {{Yes}} || | ||
|- | |- | ||
| [[Encode]] || < | | [[Encode]] || <syntaxhighlight lang=apl inline>⊤</syntaxhighlight> || <syntaxhighlight lang=apl inline>0</syntaxhighlight> || {{No}} || {{Yes}} || | ||
|- | |- | ||
| [[Union]] || < | | [[Union]] || <syntaxhighlight lang=apl inline>∪</syntaxhighlight> || <syntaxhighlight lang=apl inline>0⌿P</syntaxhighlight> || {{Yes}} || {{Yes}} || <syntaxhighlight lang=apl inline>1≤≢⍴Y</syntaxhighlight> | ||
|- | |- | ||
| [[Replicate]] || < | | [[Replicate]] || <syntaxhighlight lang=apl inline>/</syntaxhighlight> || <syntaxhighlight lang=apl inline>1</syntaxhighlight> || {{Yes}} || {{No}} || <syntaxhighlight lang=apl inline>1≤≢⍴Y</syntaxhighlight> | ||
|- | |- | ||
| [[Expand]] || < | | [[Expand]] || <syntaxhighlight lang=apl inline>\</syntaxhighlight> || <syntaxhighlight lang=apl inline>∘.=⍨⍳≢P</syntaxhighlight> || {{Yes}} || {{No}} || <syntaxhighlight lang=apl inline>1≤≢⍴Y</syntaxhighlight> | ||
|- | |- | ||
| [[Inner product]]s || < | | [[Inner product]]s || <syntaxhighlight lang=apl inline>+.×</syntaxhighlight><br><syntaxhighlight lang=apl inline>∨.∧</syntaxhighlight> || <syntaxhighlight lang=apl inline>∘.=⍨⍳≢P</syntaxhighlight> || {{Yes}} || {{Yes}} || | ||
|- | |- | ||
| [[Inner product]] || < | | [[Inner product]] || <syntaxhighlight lang=apl inline>∧.∨</syntaxhighlight> || <syntaxhighlight lang=apl inline>∘.≠⍨⍳≢P</syntaxhighlight> || {{Yes}} || {{Yes}} || | ||
|} | |} | ||
Latest revision as of 22:14, 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 (+
) has the left and right identity element 0
because N≡N+0
and N≡0+N
for all arrays N
in the domain of +
. However, the identity of Divide (÷
), 1
, is only a right identity because while N≡N÷1
is true for all N
in the domain of ÷
, this isn't so for N≡1÷N
, and no alternative identity element value exists which would fulfil the condition.
If a function f
has both a left identity element and a right identity element (call them l
and r
), then they must be the same. This is because l f r
r
, since l
is a left identity, and l f r
l
, since r
is a right identity, so l
r
.
Reduction over a length-0 axis
If a reduction (using one of /
, ⌿
, \
, or ⍀
) 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 0
, while the columnar sum of a two-column matrix with no rows is 0 0
:
+/0⍴0 0 +/0 2⍴0 0 0
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 P
of the array Y
:
Function name | Glyph | Value | Left | Right | Notes |
---|---|---|---|---|---|
Add | + |
0 |
Yes | Yes | |
Subtract | - |
0 |
No | Yes | |
Multiply | × |
1 |
Yes | Yes | |
Divide | ÷ |
1 |
No | Yes | |
Residue | | |
0 |
Yes | No | |
Minimum | ⌊ |
∞ |
Yes | Yes | the maximum representable number |
Maximum | ⌈ |
-∞ |
Yes | Yes | the minimum representable number |
Power | * |
1 |
No | Yes | |
Circle function | ○ |
¯9 |
Yes | No | |
Binomial | ! |
1 |
Yes | No | |
Root | √ |
1 |
Yes | No | |
And/LCM | ∧ |
1 |
Yes | Yes | |
Or/GCD | ∨ |
0 |
Yes | Yes | Non-negative reals only |
Less | < |
0 |
Yes | No | Booleans only |
Less Or Equal | ≤ |
1 |
Yes | No | Booleans only |
Equal to | = |
1 |
Yes | Yes | Booleans only |
Greater Or Equal | ≥ |
1 |
No | Yes | Booleans only |
Greater | > |
0 |
No | Yes | Booleans only |
Not Equal | ≠ |
0 |
Yes | Yes | Booleans only |
Reshape | ⍴ |
⍴P |
Yes | No | |
Catenate | , |
P⍴⍨ρ∘⊂⍨0,⍨¯1↓ρP |
Yes | No | 1≤≢⍴Y
|
Rotate | ⌽ |
0 or 0⍴⍨¯1↓⍴P |
Yes | No | |
Rotate First | ⊖ |
0 or 0⍴⍨1↓⍴P |
Yes | No | |
Transpose | ⍉ |
⍳≢⍴P |
Yes | No | |
Pick | ⊃ |
⍬ |
Yes | No | |
Drop | ↓ |
⍬ or 0×⍴P |
Yes | No | |
Take | ↑ |
⍬ or ⍴P |
Yes | No | |
Squad Index | ⌷ |
⍬ or ⍳¨⍴P |
Yes | No | |
Without | ~ |
0⌿P |
No | Yes | 1≤≢⍴Y
|
Matrix Divide | ⌹ |
∘.=⍨⍳≢P |
No | Yes | |
Encode | ⊤ |
0 |
No | Yes | |
Union | ∪ |
0⌿P |
Yes | Yes | 1≤≢⍴Y
|
Replicate | / |
1 |
Yes | No | 1≤≢⍴Y
|
Expand | \ |
∘.=⍨⍳≢P |
Yes | No | 1≤≢⍴Y
|
Inner products | +.× ∨.∧ |
∘.=⍨⍳≢P |
Yes | Yes | |
Inner product | ∧.∨ |
∘.≠⍨⍳≢P |
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 |