## #acl AutoAdminGroup:read,write,admin,delete,revert All:read {{{#!html
}}} ||<[[../Display|Back]]|| [[../FurtherTopics|Contents]]|| [[../VectorNotation|Next]]>|| {{{#!html
}}} = Array Type and Prototype (2 of 9) = <> == Empty Arrays == Any array may have one or more dimensions of zero length. Such an array is known as an '''empty array'''. It may be generated directly by the {{{⍴}}} '''reshape''' function or by an operation such as a test which selects no elements in an array. An empty array has a '''type''' (simple numeric, simple character or nested) depending on how it was created. Empty arrays are useful in a number of areas: as a means of initialising arrays to which successive data may be added; as a means of creating scalars from arrays; or as an argument to the {{{→}}} '''branch''' function within APL functions. == Array Type and Prototype == Any array has a '''type''' which is zero for numeric elements and the blank character for character elements. The type of the first element in an array is known as the '''prototype''' of the array. The prototype of an array is used in two important areas. Firstly, the prototype of an array is used to determine the structure of an empty array formed from that array. Secondly, the prototype of an array is used as a 'fill' element by those APL functions that can generate extra elements (for example {{{↑}}} '''take'''). An empty array is one in which at least one of the dimensions is zero. For example, an empty vector is a vector of length 0. An array with four rows and zero columns is an empty matrix; it has shape ( 4 by 0), but does not contain any data elements. A empty vector can most simply be made by one of the expressions shown below. Note that the '''type''' of the resultant empty vector can be be numeric or character. {{{ ⎕DISPLAY ⍳0 ⎕DISPLAY '' .⊖. .⊖. |0| | | '~' '-' empty numeric vector empty character vector }}} The alternative expression {{{0⍴}}} is often used, and may again create either a numeric or character empty vector when used with the appropriate argument. (Higher dimensional empty arrays may be made by expressions of the form {{{0 2⍴..}}} and so on). {{{ ⎕DISPLAY 0⍴676 ⎕DISPLAY 0⍴'PETER' .⊖. .⊖. |0| | | '~' '-' empty numeric vector empty character vector }}} An empty numeric vector can also be created using {{{⍬}}} (Zilde), a primitive constant, which is equivalent to {{{⍳0}}} or {{{0⍴0}}}. {{{ X←⍬ ⍴X 0 X≡0⍴0 1 ⎕DISPLAY ⍬ ⍝ Empty numeric vector ┌⊖┐ │0│ └~┘ }}} == Prototypes of nested arrays == More complex empty arrays result when a nested array is used to generate the empty array. In each case, it is the '''prototype''' (derived from the first element of the original array) that dictates the type and structure of the resultant empty array. {{{ ⎕DISPLAY 'ABC' (⍳3) ⎕DISPLAY 0⍴'ABC' (⍳3) .→---------------. .⊖------. | .→--. .→----. | | .→--. | | |ABC| |1 2 3| | | | | | | '---' '~----' | | '---' | '∊---------------' '∊------' }}} The original array (on the left) is a two element nested array whose first element is itself a character vector. The resultant empty vector (the expression on the right) is an empty vector which is itself a nested array with the prototype a length 3 character vector. {{{ ⎕DISPLAY (2 2⍴⍳4) 'ABC' ⎕DISPLAY 0⍴(2 2⍴⍳4) 'ABC' .→--------------. .⊖-------. | .→---. .→--. | | .→---. | | ↓ 1 2| |ABC| | | ↓ 0 0| | | | 3 4| '---' | | | 0 0| | | '~---' | | '~---' | '∊--------------' '∊-------' }}} Similar considerations apply above, except that the type of the first element of the original array is numeric, or even, as below, when the first element is mixed. {{{ ⎕DISPLAY (2 2⍴ 1 'K' 2 'J' ) (⍳4) ⎕DISPLAY 0⍴(2 2⍴ 1 'K' 2 'J') (⍳4) .→-------------------. .⊖--------. | .→----. .→------. | | .→----. | | ↓ 1 K | |1 2 3 4| | | ↓ 0 | | | | 2 J | '~------' | | | 0 | | | '+----' | | '+----' | '∊-------------------' '∊--------' }}} The prototype concept can be used to display the '''type''' of an array. In the example below, the array is first enclosed ({{{⊂}}}) to form a scalar and an empty vector made from the scalar ({{{0⍴}}}). Finally the {{{↑}}} ({{{first}}}) function removes the additional level of nesting introduced. {{{ ⎕DISPLAY VAR .→-----------------------------------. | .→------------------------. .→--. | | | .→----. .→-----------. | |ABC| | | | ↓ 1 A | | .→--. | | '---' | | | | B 2 | | |1 2| 7 | | | | | '+----' | '~--' | | | | | '∊-----------' | | | '∊------------------------' | '∊-----------------------------------' ⎕DISPLAY ↑0⍴⊂VAR .→-----------------------------------. | .→------------------------. .→--. | | | .→----. .→-----------. | | | | | | ↓ 0 | | .→--. | | '---' | | | | 0 | | |0 0| 0 | | | | | '+----' | '~--' | | | | | '∊-----------' | | | '∊------------------------' | '∊-----------------------------------' }}} == The prototype as a fill element == Certain functions require the addition of fill elements to arrays, for example the functions {{{↑}}} ('''take'''), {{{\}}} ('''expand''') and {{{/}}} ('''replicate'''). These function can add extra elements to an existing array; the prototype is used to determine the type and shape of the extra elements. The fill element depends on the type of the array being extended, as follows: || Type of array||Fill Element|| ||Numeric||Zero|| ||Character||Space|| ||Nested or mixed||Prototype or first element, with numbers replaced by zeroes and characters by spaces|| {{{ 5↑1 2 3 (fill element for simple numeric is 0) 1 2 3 0 0 ⎕DISPLAY 5↑'ABC' (fill element for simple character array .→----. is blank) |ABC | '-----' ⎕DISPLAY VAR (nested array - vector of length 2 ) .→-----------------------------------. | .→------------------------. .→--. | | | .→----. .→-----------. | |ABC| | | | ↓ 1 A | | .→--. | | '---' | | | | B 2 | | |1 2| 7 | | | | | '+----' | '~--' | | | | | '∊-----------' | | | '∊------------------------' | '∊-----------------------------------' ⎕DISPLAY 3↑VAR (prototype used as trailing fill element) .→----------------------------------------------------------------. | .→------------------------. .→--. .→------------------------. | | | .→----. .→-----------. | |ABC| | .→----. .→-----------. | | | | ↓ 1 A | | .→--. | | '---' | ↓ 0 | | .→--. | | | | | | B 2 | | |1 2| 7 | | | | 0 | | |0 0| 0 | | | | | '+----' | '~--' | | | '+----' | '~--' | | | | | '∊-----------' | | '∊-----------' | | | '∊------------------------' '∊------------------------' | '∊----------------------------------------------------------------' ⎕DISPLAY ¯3↑VAR (prototype used as leading fill element) .→----------------------------------------------------------------. | .→------------------------. .→------------------------. .→--. | | | .→----. .→-----------. | | .→----. .→-----------. | |ABC| | | | ↓ 0 | | .→--. | | | ↓ 1 A | | .→--. | | '---' | | | | 0 | | |0 0| 0 | | | | B 2 | | |1 2| 7 | | | | | '+----' | '~--' | | | '+----' | '~--' | | | | | '∊-----------' | | '∊-----------' | | | '∊------------------------' '∊------------------------' | '∊----------------------------------------------------------------' }}} '''Note:''' For a further discussion of array prototypes, see [[EmptyArraysAndPrototypes]] {{{#!html
}}} ||<[[../Display|Back]]|| [[../FurtherTopics|Contents]]|| [[../VectorNotation|Next]]>|| {{{#!html
}}} ---- CategoryAboutApl