Scalar extension: Difference between revisions

Jump to navigation Jump to search
2,576 bytes added ,  09:54, 8 July 2021
Miraheze>Adám Brudzewsky
m (Text replacement - "</code>" to "</source>")
(10 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 ==
Line 8: Line 8:
2 4 6 8
2 4 6 8
</source>
</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.


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 <source lang=apl inline>⍬</source>.
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]]).


=== Rank extension ===
=== Rank extension ===
Line 20: Line 20:
=== 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,
<source lang=apl>
<source lang=apl>
       (1 1⍴5) + 10 20
       (1 1⍴5) + 10 20
Line 38: 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:
<source lang=apl>
<source lang=apl>
       1 2 3 4 * 2
       1 2 3 4 * 2
1 4 9 16
1 4 9 16
    100,¨1 2 3 4
┌─────┬─────┬─────┬─────┐
│100 1│100 2│100 3│100 4│
└─────┴─────┴─────┴─────┘
</source>
</source>




[[Replicate]] and [[Expand]] extend a scalar left argument to apply to each element of the right argument:
[[Replicate]] and [[Partitioned Enclose]] (and also [[Partition]], but this is less useful, as it is equivalent to <source lang=apl inline>,⊂</source> or  <source lang=apl inline>{0⍴⊂0⍴⍵}</source>) extend a scalar left argument to apply to each column of the right argument:
<source lang=apl>
<source lang=apl>
       2/'abc'
       2/'abc'
aabbcc
aabbcc
      2⊂'abc'
┌┬─┬┬─┬┬─┐
││a││b││c│
└┴─┴┴─┴┴─┘
</source>
</source>
[[Decode]] uses extends a single radix left argument to apply to all digit values in the right argument:
<source lang=apl>
      2⊥1 0 1
5
</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.
<source lang=apl>
<source lang=apl>
Line 64: Line 79:
larsca
larsca
</source>
</source>
=== Each left and each right ===
A common problem is pairing up each [[element]] of one [[argument]] with all the elements of another argument. Because of scalar extension, [[enclose|making one argument a scalar]] accomplishes this:
<source lang=apl>
      1 2 3,¨⊂100 200
┌─────────┬─────────┬─────────┐
│1 100 200│2 100 200│3 100 200│
└─────────┴─────────┴─────────┘
      (⊂1 2 3),¨100 200
┌─────────┬─────────┐
│1 2 3 100│1 2 3 200│
└─────────┴─────────┘
</source>
For [[scalar function]]s, explicit use of the [[Each]] operator is unnecessary:
<source lang=apl>
      1 2 3+⊂100 200
┌───────┬───────┬───────┐
│101 201│102 202│103 203│
└───────┴───────┴───────┘
      (⊂1 2 3)+100 200
┌───────────┬───────────┐
│101 102 103│201 202 203│
└───────────┴───────────┘
</source>
[[Bind]]ing one argument to the function also works, but this always requires the Each operator:
<source lang=apl>
      1 2 3∘,¨100 200
┌─────────┬─────────┐
│1 2 3 100│1 2 3 200│
└─────────┴─────────┘
      (,∘100 200)¨1 2 3
┌─────────┬─────────┬─────────┐
│1 100 200│2 100 200│3 100 200│
└─────────┴─────────┴─────────┘
</source>
{{APL features}}[[Category:Function characteristics]][[Category:Conformability]]

Navigation menu