Array model: Difference between revisions

Jump to navigation Jump to search
2,259 bytes added ,  22:14, 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>")
(8 intermediate revisions by 2 users not shown)
Line 18: Line 18:


== "Array languages" without arrays ==
== "Array languages" without arrays ==
:''See also: [[Timeline of influential array languages]]''


Some languages, despite deriving from APL, do not use APL-style arrays at all! Examples include [[K]] and [[I]], which only have vectors, and [[MATLAB]], which has true multidimensional arrays but usually treats data as matrices. Languages like K are usually considered part of the array language or APL family, but may or may not be considered array languages themselves.
Some languages, despite deriving from APL, do not use APL-style arrays at all! Examples include [[K]] and [[I]], which only have vectors, and [[MATLAB]], which has true multidimensional arrays but usually treats data as matrices. Languages like K are usually considered part of the array language or APL family, but may or may not be considered array languages themselves.
Line 38: Line 37:


A second version of the APL array model was developed in order to more transparently handle nested data, without the need to explicitly box and unbox arrays. The Nested Array Research System ([[NARS]]) was developed to study this model. In it, arrays contain other arrays directly. In this way they resemble the [[wikipedia:inductive type|inductive type]]s used in type theory.
A second version of the APL array model was developed in order to more transparently handle nested data, without the need to explicitly box and unbox arrays. The Nested Array Research System ([[NARS]]) was developed to study this model. In it, arrays contain other arrays directly. In this way they resemble the [[wikipedia:inductive type|inductive type]]s used in type theory.
 
{| class=wikitable style="float:right"
|{{quote | "An array is a rectangular collection of numbers, characters and arrays, arranged along zero or more axes."|[[John Scholes]]}}
|}
In nested APLs each individual number or character is encapsulated in a [[simple scalar]]. Such a scalar may be referred to as "a number" or "a character" but it maintains the properties of an array. Other arrays used by the language are defined inductively: an array can be formed which contains as elements any array which has already been defined. Such an array cannot, by the nature of inductive definition, contain itself, even within many levels of nesting inside it. Arrays which contain only simple scalars, or are themselves simple scalars, are called [[Simple array|simple]]. Non-simple arrays are called "nested". The simple arrays are a superset of the arrays allowed in flat array theory without boxes: they include all arrays of numbers and characters, as well as arrays which mix numbers and characters. Arrays which would not be representable in flat array theory—those which contain a mixture of simple scalar types, or contain both simple scalars and other arrays—are called [[Mixed array|mixed]].
In nested APLs each individual number or character is encapsulated in a [[simple scalar]]. Such a scalar may be referred to as "a number" or "a character" but it maintains the properties of an array. Other arrays used by the language are defined inductively: an array can be formed which contains as elements any array which has already been defined. Such an array cannot, by the nature of inductive definition, contain itself, even within many levels of nesting inside it. Arrays which contain only simple scalars, or are themselves simple scalars, are called [[Simple array|simple]]. Non-simple arrays are called "nested". The simple arrays are a superset of the arrays allowed in flat array theory without boxes: they include all arrays of numbers and characters, as well as arrays which mix numbers and characters. Arrays which would not be representable in flat array theory—those which contain a mixture of simple scalar types, or contain both simple scalars and other arrays—are called [[Mixed array|mixed]].


Line 54: Line 55:


Flat array theory is often called "grounded" in contrast to "floating" nested array theory.
Flat array theory is often called "grounded" in contrast to "floating" nested array theory.
== Based array theory ==
Based array theory discards the principle that all data should be stored in arrays, instead defining basic types such as characters and numbers independently of arrays and arrays as a collection type—possibly one of many—that can contain any data. This model does not have any widely accepted name, with the term "based system" introduced in an [[APL Quote Quad]] paper in 1981.<ref>Randall Mercer. [https://dl.acm.org/doi/abs/10.1145/586656.586663 "A based system for general arrays"]. [[APL Quote Quad]] Volume 12, Issue 2. 1981-12.</ref> However, as it is the natural model when arrays are added to an existing programming system, it is common in array libraries such as [[NumPy]], [[wikipedia:ILNumerics|ILNumerics]], and [[wikipedia:Haskell (programming language)|Haskell]]'s [https://hackage.haskell.org/package/repa Repa], as well as the language [[Julia]]. It has also been promoted with the mnemonic "APL+1" by Jacob Brickman<ref>Jacob Brickman. "Behavior of APL primitives in a system with depth-1 scalars (APL+1)". [[APL-Germany]] ''APL-Journal'' 2019 1-2 ([https://apl-germany.de/wp-content/uploads/2020/06/APL_Journal_2019_12.pdf#page=5 pdf]).</ref>, and is used by the APL-family language [[BQN]].
=== Mutable based arrays ===
In many languages with this array style, such as NumPy and Julia, the arrays are [[wikipedia:Immutable object|mutable]], meaning that copies of an array can be made, so that one copy reflects changes made to any copy. In contrast, APL operations that appear to modify an array, like [[indexed assignment]], will only change the particular copy of the array used, and can be said to create a new array rather than change an existing one: there is no special connection between the old and modified array. Mutable arrays make it possible for an array to contain itself, by replacing one element of an existing array with the whole array. This means that more values are possible than in an immutable based array language, and that some properties of immutable arrays, such as a finite [[depth]], do not hold.


== Other features of the array model ==
== Other features of the array model ==
Line 86: Line 95:
In nested APLs, a simple non-scalar array has depth 1, an array containing only depth 1 arrays has depth 2, and a simple scalar (e.g a number or character) has depth 0.
In nested APLs, a simple non-scalar array has depth 1, an array containing only depth 1 arrays has depth 2, and a simple scalar (e.g a number or character) has depth 0.


Most APLs provide a Depth function <source lang=apl inline>≡</source> to find an array's depth. For example:
Most APLs provide a Depth function <syntaxhighlight lang=apl inline>≡</syntaxhighlight> to find an array's depth. For example:
<source lang=apl>
<syntaxhighlight lang=apl>
       ≡('ab' 'cde')('fg' 'hi')
       ≡('ab' 'cde')('fg' 'hi')
3
3
</source>
</syntaxhighlight>


APLs vary in their definition of depth: for example some may return the depth with a sign to indicate that some level of the array mixes elements of different depths.
APLs vary in their definition of depth: for example some may return the depth with a sign to indicate that some level of the array mixes elements of different depths.
Line 99: Line 108:
* [https://chat.stackexchange.com/rooms/52405/conversation/lesson-1-introduction-to-arrays-in-apl APL Cultivation]
* [https://chat.stackexchange.com/rooms/52405/conversation/lesson-1-introduction-to-arrays-in-apl APL Cultivation]
* [https://www.sacrideo.us/tag/apl-a-day/ APL a Day] series
* [https://www.sacrideo.us/tag/apl-a-day/ APL a Day] series
* [https://www.jsoftware.com/papers/array.htm What is an Array?] by [[Roger Hui]] (in [[J]])
* [https://www.jsoftware.com/papers/array.htm What is an Array?] by [[Roger Hui]]
 
== References ==
<references />
{{APL features}}[[Category:Arrays| ]]
{{APL features}}[[Category:Arrays| ]]

Navigation menu