Scalar extension: Difference between revisions

Jump to navigation Jump to search
162 bytes added ,  22:11, 10 September 2022
m
Text replacement - "</source>" to "</syntaxhighlight>"
m (Text replacement - "<source" to "<syntaxhighlight")
Tags: Mobile edit Mobile web edit
m (Text replacement - "</source>" to "</syntaxhighlight>")
Line 3: Line 3:
== History and terminology ==
== History and terminology ==


The concept of scalar extension has been around since [[APL\360]]. An example which extends the scalar <syntaxhighlight lang=apl inline>2</source> is:
The concept of scalar extension has been around since [[APL\360]]. An example which extends the scalar <syntaxhighlight lang=apl inline>2</syntaxhighlight> is:
<syntaxhighlight lang=apl>
<syntaxhighlight lang=apl>
       2 × 1 2 3 4
       2 × 1 2 3 4
2 4 6 8
2 4 6 8
</source>
</syntaxhighlight>
[[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]].
[[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 the [[empty]] vector, <syntaxhighlight 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 the [[empty]] vector, <syntaxhighlight lang=apl inline>⍬</syntaxhighlight> ([[Zilde]]).


=== Rank extension ===
=== 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, <syntaxhighlight lang=apl inline>⍳8</source> and <syntaxhighlight lang=apl inline>8⍴'a'</source> both produce an array of shape <syntaxhighlight lang=apl inline>,8</source> (a vector) even though they were given a shape specification of  <syntaxhighlight lang=apl inline>8</source> (a scalar). 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]].
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, <syntaxhighlight lang=apl inline>⍳8</syntaxhighlight> and <syntaxhighlight lang=apl inline>8⍴'a'</syntaxhighlight> both produce an array of shape <syntaxhighlight lang=apl inline>,8</syntaxhighlight> (a vector) even though they were given a shape specification of  <syntaxhighlight lang=apl inline>8</syntaxhighlight> (a scalar). 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 ===
Line 26: Line 26:
       ⍴ (1 1⍴5) + 10 20
       ⍴ (1 1⍴5) + 10 20
2
2
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]], [[APLX]]}}
{{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 46: Line 46:
│100 1│100 2│100 3│100 4│
│100 1│100 2│100 3│100 4│
└─────┴─────┴─────┴─────┘
└─────┴─────┴─────┴─────┘
</source>
</syntaxhighlight>




[[Replicate]] and [[Partitioned Enclose]] (and also [[Partition]], but this is less useful, as it is equivalent to <syntaxhighlight lang=apl inline>,⊂</source> or  <syntaxhighlight lang=apl inline>{0⍴⊂0⍴⍵}</source>) extend a scalar left argument to apply to each column of the right argument:
[[Replicate]] and [[Partitioned Enclose]] (and also [[Partition]], but this is less useful, as it is equivalent to <syntaxhighlight lang=apl inline>,⊂</syntaxhighlight> or  <syntaxhighlight lang=apl inline>{0⍴⊂0⍴⍵}</syntaxhighlight>) extend a scalar left argument to apply to each column of the right argument:
<syntaxhighlight lang=apl>
<syntaxhighlight lang=apl>
       2/'abc'
       2/'abc'
Line 57: Line 57:
││a││b││c│
││a││b││c│
└┴─┴┴─┴┴─┘
└┴─┴┴─┴┴─┘
</source>
</syntaxhighlight>


[[Decode]] uses extends a single radix left argument to apply to all digit values in the right argument:
[[Decode]] uses extends a single radix left argument to apply to all digit values in the right argument:
Line 63: Line 63:
       2⊥1 0 1
       2⊥1 0 1
5
5
</source>
</syntaxhighlight>


[[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.
Line 69: Line 69:
       ⍴ 2 ¯3 /[2] 7 1 8⍴⍳56
       ⍴ 2 ¯3 /[2] 7 1 8⍴⍳56
7 5 8
7 5 8
</source>
</syntaxhighlight>
{{Works in|[[Dyalog APL]], [[APL2]], [[APLX]]}}
{{Works in|[[Dyalog APL]], [[APL2]], [[APLX]]}}


Line 78: Line 78:
endext
endext
larsca
larsca
</source>
</syntaxhighlight>


=== Each left and each right ===
=== Each left and each right ===
Line 92: Line 92:
│1 2 3 100│1 2 3 200│
│1 2 3 100│1 2 3 200│
└─────────┴─────────┘
└─────────┴─────────┘
</source>
</syntaxhighlight>
For [[scalar function]]s, explicit use of the [[Each]] operator is unnecessary:
For [[scalar function]]s, explicit use of the [[Each]] operator is unnecessary:
<syntaxhighlight lang=apl>
<syntaxhighlight lang=apl>
Line 103: Line 103:
│101 102 103│201 202 203│
│101 102 103│201 202 203│
└───────────┴───────────┘
└───────────┴───────────┘
</source>
</syntaxhighlight>
[[Bind]]ing one argument to the function also works, but this always requires the Each operator:
[[Bind]]ing one argument to the function also works, but this always requires the Each operator:
<syntaxhighlight lang=apl>
<syntaxhighlight lang=apl>
Line 114: Line 114:
│1 100 200│2 100 200│3 100 200│
│1 100 200│2 100 200│3 100 200│
└─────────┴─────────┴─────────┘
└─────────┴─────────┴─────────┘
</source>
</syntaxhighlight>
{{APL features}}[[Category:Function characteristics]][[Category:Conformability]]
{{APL features}}[[Category:Function characteristics]][[Category:Conformability]]

Navigation menu