4,505
edits
m (Text replacement - "<source" to "<syntaxhighlight") |
m (Text replacement - "</source>" to "</syntaxhighlight>") |
||
Line 1: | Line 1: | ||
In the APL [[array model]], an '''empty''' array is one with a [[bound]] of zero, that is, an array with no [[element]]s. While a nested list model has only one empty list, APL has many different empty arrays. These arrays are distinguished by their [[shape]] and [[prototype]]. | In the APL [[array model]], an '''empty''' array is one with a [[bound]] of zero, that is, an array with no [[element]]s. While a nested list model has only one empty list, APL has many different empty arrays. These arrays are distinguished by their [[shape]] and [[prototype]]. | ||
Examples of empty arrays are the empty [[numeric]] array [[Zilde]] (<syntaxhighlight lang=apl inline>⍬</ | Examples of empty arrays are the empty [[numeric]] array [[Zilde]] (<syntaxhighlight lang=apl inline>⍬</syntaxhighlight>) and the empty [[character]] array <syntaxhighlight lang=apl inline>''</syntaxhighlight>. These arrays have different prototypes, and do not [[match]] in most APLs. | ||
== Empty array shape == | == Empty array shape == | ||
Line 15: | Line 15: | ||
⍴ ⍳⍤0 ⊢0⍴3 | ⍴ ⍳⍤0 ⊢0⍴3 | ||
0 0 | 0 0 | ||
</ | </syntaxhighlight> | ||
{{Works in|[[Dyalog APL]], [[NARS2000]]}} | {{Works in|[[Dyalog APL]], [[NARS2000]]}} | ||
The example above shows how an arithmetic rule can fail when using Rank with an empty array. For any positive integer <syntaxhighlight lang=apl inline>n</ | The example above shows how an arithmetic rule can fail when using Rank with an empty array. For any positive integer <syntaxhighlight lang=apl inline>n</syntaxhighlight>, it's clear that <syntaxhighlight lang=apl inline>(n,3) ≡ ⍴ ⍳⍤0 ⊢n⍴3</syntaxhighlight> because each result cell is the array <syntaxhighlight lang=apl inline>⍳3</syntaxhighlight>. However, the empty array <syntaxhighlight lang=apl inline>0⍴3</syntaxhighlight> is indistinguishable from any other empty numeric array because it has shape <syntaxhighlight lang=apl inline>,0</syntaxhighlight> and prototype <syntaxhighlight lang=apl inline>0</syntaxhighlight>. This means Rank cannot determine the appropriate result shape. The Rank operator still attempts to find a sensible result shape: it executes the operand on a prototype cell obtained by reshaping the argument to the argument cell shape (here, the prototype cell is a scalar <syntaxhighlight lang=apl inline>0</syntaxhighlight>), and uses the resulting shape to determine the shape of its final result. In this case, this succeeds in finding the appropriate result rank but not the desired shape. | ||
If Rank is implemented by [[split]]ting the argument(s) into cells, applying the operand with [[Each]], and [[mix]]ing, then the above problem actually becomes an issue of empty array prototypes: the result of Split is empty, and calling Each on an empty array uses prototypes to determine prototypes, an unreliable operation. | If Rank is implemented by [[split]]ting the argument(s) into cells, applying the operand with [[Each]], and [[mix]]ing, then the above problem actually becomes an issue of empty array prototypes: the result of Split is empty, and calling Each on an empty array uses prototypes to determine prototypes, an unreliable operation. | ||
Line 31: | Line 31: | ||
a ≡ ⍬,a | a ≡ ⍬,a | ||
b ≡ b,'' | b ≡ b,'' | ||
</ | </syntaxhighlight> | ||
These identities always hold when <syntaxhighlight lang=apl inline>a</ | These identities always hold when <syntaxhighlight lang=apl inline>a</syntaxhighlight> and <syntaxhighlight lang=apl inline>b</syntaxhighlight> are non-empty, because the result is non-empty and its elements are entirely determined by the non-empty argument. However, if we consider the catenation | ||
<syntaxhighlight lang=apl> | <syntaxhighlight lang=apl> | ||
⍬,'' | ⍬,'' | ||
</ | </syntaxhighlight> | ||
then the first identity tells us that the result should be <syntaxhighlight lang=apl inline>''</ | then the first identity tells us that the result should be <syntaxhighlight lang=apl inline>''</syntaxhighlight> while the second gives a result of <syntaxhighlight lang=apl inline>⍬</syntaxhighlight>. These two arrays do not match, so one of the identities must be wrong! In fact, the choice of which prototype to use for the result is a source of incompatibility among APLs. In [[Dyalog APL]] it has even been changed in the past. At one point the right argument's prototype was used; now we can inspect the [[first]] element of <syntaxhighlight lang=apl inline>⍬,''</syntaxhighlight> to see that the left argument's prototype is used. | ||
<syntaxhighlight lang=apl> | <syntaxhighlight lang=apl> | ||
⊃⍬,'' | ⊃⍬,'' | ||
0 | 0 | ||
</ | </syntaxhighlight> | ||
{{Works in|[[Dyalog APL]]}} | {{Works in|[[Dyalog APL]]}} | ||
== Reduction over a length-0 axis == | == Reduction over a length-0 axis == | ||
If a [[Reduce|reduction]] (using one of <syntaxhighlight lang=apl inline>/</ | 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 element]]s. For example, the sum of an empty list is <syntaxhighlight lang=apl inline>0</syntaxhighlight> because the identity element for [[addition]] is <syntaxhighlight lang=apl inline>0</syntaxhighlight>: | ||
<syntaxhighlight lang=apl> | <syntaxhighlight lang=apl> | ||
+/0⍴0 | +/0⍴0 | ||
0 | 0 | ||
</ | </syntaxhighlight> | ||
The identity element for a specific reduction always depends on the operand, and may depend on the argument array prototype. Dialects differ in their support for such reductions. All define identity elements for most [[scalar functions#Standard scalar functions|scalar primitives]], and some stop there (e.g. [[SAX]]), while others (e.g. [[Dyalog APL]] and [[APL2]]) define identity elements for various [[mixed function]]s. | The identity element for a specific reduction always depends on the operand, and may depend on the argument array prototype. Dialects differ in their support for such reductions. All define identity elements for most [[scalar functions#Standard scalar functions|scalar primitives]], and some stop there (e.g. [[SAX]]), while others (e.g. [[Dyalog APL]] and [[APL2]]) define identity elements for various [[mixed function]]s. | ||
{{APL features}}[[Category:Kinds of array]] | {{APL features}}[[Category:Kinds of array]] |