Identity element: Difference between revisions

Jump to navigation Jump to search
1,035 bytes added ,  22:04, 10 September 2022
m
Text replacement - "<source" to "<syntaxhighlight"
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]] (<source lang=apl inline>+</source>) has the left and right identity element <source lang=apl inline>0</source> because <source lang=apl inline>N≡N+0</source> and <source lang=apl inline>N≡0+N</source> for all arrays <source lang=apl inline>N</source> in the domain of <source lang=apl inline>+</source>. However, the identity of [[Divide]] (<source lang=apl inline>÷</source>), <source lang=apl inline>1</source>, is only a right identity because while <source lang=apl inline>N≡N÷1</source> is true for all <source lang=apl inline>N</source> in the domain of <source lang=apl inline>÷</source>, this isn't so for <source lang=apl inline>N≡1÷N</source>, and no alternative identity element value exists which would fulfil the condition.
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 <source lang=apl inline>f</source> has both a left identity element and a right identity element (call them <source lang=apl inline>l</source> and <source lang=apl inline>r</source>), then they must be the same. This is because <source lang=apl inline>l f r</source> {{←→}} <source lang=apl inline>r</source>, since <source lang=apl inline>l</source> is a left identity, and <source lang=apl inline>l f r</source> {{←→}} <source lang=apl inline>l</source>, since <source lang=apl inline>r</source> is a right identity, so <source lang=apl inline>l</source> {{←→}} <source lang=apl inline>r</source>.
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 <source lang=apl inline>/</source>, <source lang=apl inline>⌿</source>, <source lang=apl inline>\</source>, or <source 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 <source lang=apl inline>0</source>, while the columnar sum of a two-column [[matrix]] with no rows is <source lang=apl inline>0 0</source>:
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>:
<source lang=apl>
<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]] <source lang=apl inline>P</source> of the array <source lang=apl inline>Y</source>:
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]] || <source lang=apl inline>+</source> || <source lang=apl inline>0</source> || {{Yes}} || {{Yes}} ||
| [[Add]] || <syntaxhighlight lang=apl inline>+</source> || <syntaxhighlight lang=apl inline>0</source> || {{Yes}} || {{Yes}} ||
|-
|-
| [[Subtract]] || <source lang=apl inline>-</source> || <source lang=apl inline>0</source> || {{No}} || {{Yes}} ||
| [[Subtract]] || <syntaxhighlight lang=apl inline>-</source> || <syntaxhighlight lang=apl inline>0</source> || {{No}} || {{Yes}} ||
|-
|-
| [[Multiply]] || <source lang=apl inline>×</source> || <source lang=apl inline>1</source> || {{Yes}} || {{Yes}} ||
| [[Multiply]] || <syntaxhighlight lang=apl inline>×</source> || <syntaxhighlight lang=apl inline>1</source> || {{Yes}} || {{Yes}} ||
|-
|-
| [[Divide]] || <source lang=apl inline>÷</source> || <source lang=apl inline>1</source> || {{No}} || {{Yes}} ||
| [[Divide]] || <syntaxhighlight lang=apl inline>÷</source> || <syntaxhighlight lang=apl inline>1</source> || {{No}} || {{Yes}} ||
|-
|-
| [[Residue]] || <source lang=apl inline>|</source> || <source lang=apl inline>0</source> || {{Yes}} || {{No}} ||
| [[Residue]] || <syntaxhighlight lang=apl inline>|</source> || <syntaxhighlight lang=apl inline>0</source> || {{Yes}} || {{No}} ||
|-
|-
| [[Minimum]] || <source lang=apl inline>⌊</source> || <source lang=apl inline>∞</source> || {{Yes}} || {{Yes}} || the maximum representable number
| [[Minimum]] || <syntaxhighlight lang=apl inline>⌊</source> || <syntaxhighlight lang=apl inline>∞</source> || {{Yes}} || {{Yes}} || the maximum representable number
|-
|-
| [[Maximum]] || <source lang=apl inline>⌈</source> || <source lang=apl inline>-∞</source> || {{Yes}} || {{Yes}} || the minimum representable number
| [[Maximum]] || <syntaxhighlight lang=apl inline>⌈</source> || <syntaxhighlight lang=apl inline>-∞</source> || {{Yes}} || {{Yes}} || the minimum representable number
|-
|-
| [[Power]] || <source lang=apl inline>*</source> || <source lang=apl inline>1</source> || {{No}} || {{Yes}} ||
| [[Power]] || <syntaxhighlight lang=apl inline>*</source> || <syntaxhighlight lang=apl inline>1</source> || {{No}} || {{Yes}} ||
|-
|-
| [[Circle function]] || <source lang=apl inline>○</source> || <source lang=apl inline>¯9</source> || {{Yes}} || {{No}} ||  
| [[Circle function]] || <syntaxhighlight lang=apl inline>○</source> || <syntaxhighlight lang=apl inline>¯9</source> || {{Yes}} || {{No}} ||  
|-
|-
| [[Binomial]] || <source lang=apl inline>!</source> || <source lang=apl inline>1</source> || {{Yes}} || {{No}} ||
| [[Binomial]] || <syntaxhighlight lang=apl inline>!</source> || <syntaxhighlight lang=apl inline>1</source> || {{Yes}} || {{No}} ||
|-
|-
| [[Root]] || <source lang=apl inline>√</source> || <source lang=apl inline>1</source> || {{Yes}} || {{No}} ||
| [[Root]] || <syntaxhighlight lang=apl inline>√</source> || <syntaxhighlight lang=apl inline>1</source> || {{Yes}} || {{No}} ||
|-
|-
| [[And]]/[[LCM]] || <source lang=apl inline>∧</source> || <source lang=apl inline>1</source> || {{Yes}} || {{Yes}} ||
| [[And]]/[[LCM]] || <syntaxhighlight lang=apl inline>∧</source> || <syntaxhighlight lang=apl inline>1</source> || {{Yes}} || {{Yes}} ||
|-
|-
| [[Or]]/[[GCD]] || <source lang=apl inline>∨</source> || <source lang=apl inline>0</source> || {{Yes}} || {{Yes}} || Non-negative reals only
| [[Or]]/[[GCD]] || <syntaxhighlight lang=apl inline>∨</source> || <syntaxhighlight lang=apl inline>0</source> || {{Yes}} || {{Yes}} || Non-negative reals only
|-
|-
| [[Less]] || <source lang=apl inline><</source> || <source lang=apl inline>0</source> || {{Yes}} || {{No}} || [[Boolean]]s only
| [[Less]] || <syntaxhighlight lang=apl inline><</source> || <syntaxhighlight lang=apl inline>0</source> || {{Yes}} || {{No}} || [[Boolean]]s only
|-
|-
| [[Less Or Equal]] || <source lang=apl inline>≤</source> || <source lang=apl inline>1</source> || {{Yes}} || {{No}} || [[Boolean]]s only
| [[Less Or Equal]] || <syntaxhighlight lang=apl inline>≤</source> || <syntaxhighlight lang=apl inline>1</source> || {{Yes}} || {{No}} || [[Boolean]]s only
|-
|-
| [[Equal to]] || <source lang=apl inline>=</source> || <source lang=apl inline>1</source> || {{Yes}} || {{Yes}} || [[Boolean]]s only
| [[Equal to]] || <syntaxhighlight lang=apl inline>=</source> || <syntaxhighlight lang=apl inline>1</source> || {{Yes}} || {{Yes}} || [[Boolean]]s only
|-
|-
| [[Greater Or Equal]] || <source lang=apl inline>≥</source> || <source lang=apl inline>1</source> || {{No}} || {{Yes}} || [[Boolean]]s only
| [[Greater Or Equal]] || <syntaxhighlight lang=apl inline>≥</source> || <syntaxhighlight lang=apl inline>1</source> || {{No}} || {{Yes}} || [[Boolean]]s only
|-
|-
| [[Greater]] || <source lang=apl inline>></source> || <source lang=apl inline>0</source> || {{No}} || {{Yes}} || [[Boolean]]s only
| [[Greater]] || <syntaxhighlight lang=apl inline>></source> || <syntaxhighlight lang=apl inline>0</source> || {{No}} || {{Yes}} || [[Boolean]]s only
|-
|-
| [[Not Equal]] || <source lang=apl inline>≠</source> || <source lang=apl inline>0</source> || {{Yes}} || {{Yes}} || [[Boolean]]s only
| [[Not Equal]] || <syntaxhighlight lang=apl inline>≠</source> || <syntaxhighlight lang=apl inline>0</source> || {{Yes}} || {{Yes}} || [[Boolean]]s only
|-
|-
| [[Reshape]] || <source lang=apl inline>⍴</source> || <source lang=apl inline>⍴P</source> || {{Yes}} || {{No}} ||
| [[Reshape]] || <syntaxhighlight lang=apl inline>⍴</source> || <syntaxhighlight lang=apl inline>⍴P</source> || {{Yes}} || {{No}} ||
|-
|-
| [[Catenate]] || <source lang=apl inline>,</source> || <source lang=apl inline>P⍴⍨ρ∘⊂⍨0,⍨¯1↓ρP</source> || {{Yes}} || {{No}} || <source lang=apl inline>1≤≢⍴Y</source>
| [[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]] || <source lang=apl inline>⌽</source> || <source lang=apl inline>0</source> or <source lang=apl inline>0⍴⍨¯1↓⍴P</source> || {{Yes}} || {{No}} ||
| [[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]] || <source lang=apl inline>⊖</source> || <source lang=apl inline>0</source> or <source 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]] || <source lang=apl inline>⍉</source> || <source lang=apl inline>⍳≢⍴P</source> || {{Yes}} || {{No}} ||
| [[Transpose]] || <syntaxhighlight lang=apl inline>⍉</source> || <syntaxhighlight lang=apl inline>⍳≢⍴P</source> || {{Yes}} || {{No}} ||
|-
|-
| [[Pick]] || <source lang=apl inline>⊃</source> || <source lang=apl inline>⍬</source> || {{Yes}} || {{No}} ||
| [[Pick]] || <syntaxhighlight lang=apl inline>⊃</source> || <syntaxhighlight lang=apl inline>⍬</source> || {{Yes}} || {{No}} ||
|-
|-
| [[Drop]] || <source lang=apl inline>↓</source> || <source lang=apl inline>⍬</source> or <source lang=apl inline>0×⍴P</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]] || <source lang=apl inline>↑</source> || <source lang=apl inline>⍬</source> or <source lang=apl inline>⍴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]] || <source lang=apl inline>⌷</source> || <source lang=apl inline>⍬</source> or <source 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]] || <source lang=apl inline>~</source> || <source lang=apl inline>0⌿P</source> || {{No}} || {{Yes}} || <source lang=apl inline>1≤≢⍴Y</source>
| [[Without]] || <syntaxhighlight lang=apl inline>~</source> || <syntaxhighlight lang=apl inline>0⌿P</source> || {{No}} || {{Yes}} || <syntaxhighlight lang=apl inline>1≤≢⍴Y</source>
|-
|-
| [[Matrix Divide]] || <source lang=apl inline>⌹</source> || <source lang=apl inline>∘.=⍨⍳≢P</source> || {{No}} || {{Yes}} ||
| [[Matrix Divide]] || <syntaxhighlight lang=apl inline>⌹</source> || <syntaxhighlight lang=apl inline>∘.=⍨⍳≢P</source> || {{No}} || {{Yes}} ||
|-
|-
| [[Encode]] || <source lang=apl inline>⊤</source> || <source lang=apl inline>0</source> || {{No}} || {{Yes}} ||
| [[Encode]] || <syntaxhighlight lang=apl inline>⊤</source> || <syntaxhighlight lang=apl inline>0</source> || {{No}} || {{Yes}} ||
|-
|-
| [[Union]] || <source lang=apl inline>∪</source> || <source lang=apl inline>0⌿P</source> || {{Yes}} || {{Yes}} || <source lang=apl inline>1≤≢⍴Y</source>
| [[Union]] || <syntaxhighlight lang=apl inline>∪</source> || <syntaxhighlight lang=apl inline>0⌿P</source> || {{Yes}} || {{Yes}} || <syntaxhighlight lang=apl inline>1≤≢⍴Y</source>
|-
|-
| [[Replicate]] || <source lang=apl inline>/</source> || <source lang=apl inline>1</source> || {{Yes}} || {{No}} || <source 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]] || <source lang=apl inline>\</source> || <source lang=apl inline>∘.=⍨⍳≢P</source> || {{Yes}} || {{No}} || <source 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 product]]s || <source lang=apl inline>+.×</source><br><source lang=apl inline>∨.∧</source> || <source lang=apl inline>∘.=⍨⍳≢P</source> || {{Yes}} || {{Yes}} ||
| [[Inner product]]s || <syntaxhighlight lang=apl inline>+.×</source><br><syntaxhighlight lang=apl inline>∨.∧</source> || <syntaxhighlight lang=apl inline>∘.=⍨⍳≢P</source> || {{Yes}} || {{Yes}} ||
|-
|-
| [[Inner product]] || <source lang=apl inline>∧.∨</source> || <source lang=apl inline>∘.≠⍨⍳≢P</source> || {{Yes}} || {{Yes}} ||
| [[Inner product]] || <syntaxhighlight lang=apl inline>∧.∨</source> || <syntaxhighlight lang=apl inline>∘.≠⍨⍳≢P</source> || {{Yes}} || {{Yes}} ||
|}
|}


Navigation menu