Scalar extension: Difference between revisions

Jump to navigation Jump to search
487 bytes added ,  14:26, 30 April 2020
m
Categories
Miraheze>Marshall
(Created page with "Scalar extension is a way to apply a function with a scalar argument when an array of a particular non-empty shape would be expected. The scalar is ''extended'' to thi...")
 
m (Categories)
(14 intermediate revisions by 5 users not shown)
Line 1: Line 1:
Scalar extension is a way to apply a function with a [[scalar]] argument when an array of a particular non-empty [[shape]] would be expected. The scalar is ''extended'' to this shape by treating it as an array with each element equal to the scalar's only element. This is equivalent to reshaping the scalar to fit the desired shape.
'''Scalar extension''' is a way to apply a [[function]] with a [[scalar]] argument when an array of a particular non-[[empty]] [[shape]] would be expected. The scalar is ''extended'' to this shape by treating it as an array with each [[element]] equal to the scalar's only element. This is equivalent to [[Reshape|reshaping]] the scalar to fit the desired shape.


== History and terminology ==
== History and terminology ==


The concept of scalar extension has been around since [[APL\360]]. An example which extends the scalar <code>2</code> is:
The concept of scalar extension has been around since [[APL\360]]. An example which extends the scalar <source lang=apl inline>2</source> is:
<pre>
<source lang=apl>
       2 × 1 2 3 4
       2 × 1 2 3 4
2 4 6 8
2 4 6 8
</pre>
</source>
[[A Programming Language]] describes the above computation as a "scalar multiple" but does not generalise it to arbitrary [[scalar functions]], so it's unclear when scalar extension as a unified concept was adopted in [[Iverson notation]].
[[A Programming Language]] describes the above computation as a "scalar multiple" but does not generalise it to arbitrary [[scalar function]]s, so it's unclear when scalar extension as a unified concept was adopted in [[Iverson notation]].


The word "extension" applies to scalar extension in two ways: first, a function is extended by making a case which would have been a [[RANK ERROR]] into a valid application. Second, the application works by conceptually extending the scalar to function as though it were an array of higher rank.
The word "extension" applies to scalar extension in two ways: first, a function is extended by making a case which would have been a [[RANK ERROR]] into a valid application. Second, the application works by conceptually extending the scalar to function as though it were an array of higher rank.


The term "scalar extension" is sometimes used to refer to the practice of treating a scalar as a one-element vector in cases where a vector is expected. For example, <code>⍳8</code> and <code>8⍴'a'</code> both produce an 8-element array even though the shape of an array is always a vector and so cannot be equal to <code>8</code>. This type of extension, which differs from ordinary scalar extension in that there is no expected shape and only an expected rank, has also been present since [[APL\360]].
Two arrays are said to [[Conformability|conform]] if they have the same shape or at least one can be extended (it is a scalar, or, in langauges with singleton extension, has exactly one element). A pair of conforming arrays defines a single shape which describes how their elements are paired: if neither is a scalar, it is their shared shape; if one is a scalar, it is the other's shape; if both are scalars, it is the [[empty]] vector, <source lang=apl inline></source> ([[Zilde]]).


Two arrays are said to [[Conformability|conform]] if they have the same shape or at least one can be extended (it is a scalar, or, in langauges with singleton extension, has exactly one element). A pair of conforming arrays defines a single shape which describes how their elements are paired: if neither is a scalar, it is their shared shape; if one is a scalar, it is the other's shape; if both are scalars, it is <code></code>.
=== Rank extension ===
 
The term "scalar extension" is sometimes used to refer to the practice of allowing a scalar when a higher rank is expected. The scalar is treated as an array of the expected minimum rank whose shape is a vector of 1s (that is, a [[singleton]]). For example, <source lang=apl inline>⍳8</source> and <source lang=apl inline>8⍴'a'</source> both produce an 8-element array even though the shape of an array is always a vector and so cannot be equal to <source lang=apl inline>8</source>. This type of extension, which differs from ordinary scalar extension in that there is no expected shape and only an expected rank, has also been present since [[APL\360]].


=== Singleton extension ===
=== Singleton extension ===


Some APLs, such as [[Dyalog APL]], treat arrays with one element (singletons) as scalars for the purposes of scalar extension. This practice is referred to as "singleton extension". For example,
Some APLs, such as [[Dyalog APL]], treat arrays with one element ([[singleton]]s) as scalars for the purposes of scalar extension. This practice is referred to as "singleton extension". For example,
<pre>
<source lang=apl>
       (1 1⍴5) + 10 20
       (1 1⍴5) + 10 20
15 25
15 25
       ⍴ (1 1⍴5) + 10 20
       ⍴ (1 1⍴5) + 10 20
2
2
</pre>
</source>
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]], [[APLX]]}}
In this case addition accepts a singleton, and discards its shape. If two singletons are used as arguments, they are still considered to [[Conformability|conform]]; the shape of the result is taken from the argument with higher rank.
In this case addition accepts a singleton, and discards its shape. If two singletons are used as arguments, they are still considered to [[Conformability|conform]]; the shape of the result is taken from the argument with higher rank.


Line 36: Line 38:
== Examples ==
== Examples ==


Dyadic [[scalar functions]] and the [[Each operator]] use scalar extension to pair their arguments:
Dyadic [[scalar functions]] and the [[Each]] operator use scalar extension to pair their arguments:
<pre>
<source lang=apl>
       1 2 3 4 * 2
       1 2 3 4 * 2
1 4 9 16
1 4 9 16
</pre>
</source>




[[Replicate]] and [[Expand]] extend a scalar left argument to apply to each element of the right argument:
[[Replicate]] and [[Expand]] extend a scalar left argument to apply to each element of the right argument:
<pre>
<source lang=apl>
       2/'abc'
       2/'abc'
aabbcc
aabbcc
</pre>
</source>
[[APL2]] and [[Dyalog APL]] use a variant of singleton extension when the selected axis of the right argument has length one: each element along that axis is reused for every element of the left argument.
[[APL2]] and [[Dyalog APL]] use a variant of singleton extension when the selected axis of the right argument has length one: each element along that axis is reused for every element of the left argument.
<pre>
<source lang=apl>
       ⍴ 2 ¯3 /[2] 7 1 8⍴⍳56
       ⍴ 2 ¯3 /[2] 7 1 8⍴⍳56
7 5 8
7 5 8
</pre>
</source>
{{Works in|[[Dyalog APL]]}}
{{Works in|[[Dyalog APL]], [[APL2]], [[APLX]]}}




In languages which allow a vector left argument to [[Rotate]], the behavior with a scalar left argument follows from scalar extension. In the following example a length-2 vector could be used to rotate each row by a different amount. A scalar rotates both rows by the same amount.
In languages which allow a vector left argument to [[Rotate]], the behavior with a scalar left argument follows from scalar extension. In the following example a length-2 vector could be used to rotate each row by a different amount. A scalar rotates both rows by the same amount.
<pre>
<source lang=apl>
       3⌽2 6⍴'extendscalar'
       3⌽2 6⍴'extendscalar'
endext
endext
larsca
larsca
</pre>
</source>
 
 
{{APL features}}[[Category:Function characteristics]][[Category:Conformability]]

Navigation menu