Scalar function: Difference between revisions

Jump to navigation Jump to search
1,551 bytes removed ,  10:29, 4 May 2020
no edit summary
(Remove derived op to work in dzaima/APL)
No edit summary
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
A '''scalar function''' is one of a class of [[primitive function]]s that apply to [[argument]]s one [[element]] at a time. [[Dyadic]] scalar functions pair elements of their arguments based on [[conformability]] rules, and thus are subject to [[scalar extension]]. In [[Nested array model|nested]] array languages scalar functions recursively descend into nested arrays until they can be applied to [[simple scalars]]; in [[Flat array model|flat]] array languages they usually do not apply inside [[boxes]].
A '''scalar function''' is one of a class of [[primitive function]]s that apply to [[argument]]s one [[element]] at a time. [[Dyadic]] scalar functions pair elements of their arguments based on [[conformability]] rules, and thus are subject to [[scalar extension]]. In [[Nested array model|nested]] array languages, scalar functions [[pervasion|pervade]] any [[nested array]]s by [[recursion|recursively]] descending into them until reaching [[simple scalars]]; in [[Flat array model|flat]] array languages they usually do not apply inside [[boxes]].


Only a particular [[valence]] of a function is labelled "scalar". The scalar monad [[Not]] usually shares the glyph <source lang=apl inline>~</source> with non-scalar dyad [[Without]], and similarly scalar [[Roll]] and non-scalar [[Deal]] are both written <source lang=apl inline>?</source>.
Only a particular [[valence]] of a function is labelled "scalar". The scalar monad [[Not]] usually shares the glyph <source lang=apl inline>~</source> with non-scalar dyad [[Without]], and similarly scalar [[Roll]] and non-scalar [[Deal]] are both written <source lang=apl inline>?</source>.
Line 72: Line 72:
|-
|-
|}
|}
== User defined scalar functions ==
In [[nested array model]] dialects with the [[Depth (operator)|Depth]] operator (<source lang=apl inline>⍥</source>), any function can be used as a scalar function (that is, be applied to all simple scalars) using the <source lang=apl inline>perv←⍥0</source>:
[https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/qKPtUduE6kd9U4HUo56mlMzigke9u2ofdbcAxYJdgUR4sLprWWpeibqGenBqcXFmfl5AUSaQq6AO1KyuyfWoo70CqNk5PyVVoeK/X35ecHJiTmKRWx7Y4N6tVupAtUDzFNTT1Gu5NJAV9C410FTQMFAw1FQwVNB41NVkqGCgCXRTOxfItEe9a/4DAA Try it online!]
<source lang=apl>
      NonScalarFn←{⍵:'t' ⋄ 'f'}
      (NonScalarFn⍥0) (0 1) 1 (⊂1 0)
┌──┬─┬────┐
│ft│t│┌──┐│
│  │ ││tf││
│  │ │└──┘│
└──┴─┴────┘
</source>
{{Works in|[[Extended Dyalog APL]], [[dzaima/APL]]}}
In dialects that support [[dfn]]s, this operator can be defined<ref>[[John Scholes]], [https://dfns.dyalog.com/n_perv.htm perv] (Scalar pervasion). dfns workspace, 2019-02-17.</ref> as:
<!-- [https://tio.run/##VZGxTsMwEIb3PMVtlwygZo3EVJURIWVAjFbsQKTItpwQFaEuCCUhKKgLIwOwMLB26cij@EXSc6IIasn23e@77@wz0/kJv2e5uhlsW9t6@2Bf32jjWaFtv7fdFzoTSV1eI09lgRv78kRuvKLlKsZVJWSJPsaiKDIlL01GLiDRMPBs26yJtlRcwHrQwlRjCQLXW9t9wvGw/TvECcuZARfKHM9zB@GZff6gLIrYuRlN9n6W5mTfCUzyYFS50OUtLCJQRCMVUglM6zxLWDmT/9UmVNv8fs@8P6AyM08QKAIjkjtTZJWA0rBKmILlp97Gu1Byuvy5nB65i5A6Qd0CTPHofHwe@AsIAwipSPcYwiKgH2g81yrb/wwH Try it online!] -->
<source lang=apl>
perv←{⍺←⊢              ⍝ Scalar pervasion
    1=≡⍺ ⍵ ⍵:⍺ ⍺⍺ ⍵    ⍝ (⍺ and) ⍵ depth 0: operand fn application
            ⍺ ∇¨⍵      ⍝ (⍺ or) ⍵ deeper: recursive traversal.
}
</source>
{{Works in|[[Dyalog APL]]}}
== External links ==
== External links ==


* [https://www.sacrideo.us/apl-a-day-7-scalar-functions/ Scalar Functions] (part of [https://www.sacrideo.us/tag/apl-a-day/ APL a Day])
* [https://www.sacrideo.us/apl-a-day-7-scalar-functions/ Scalar Functions] (part of [https://www.sacrideo.us/tag/apl-a-day/ APL a Day])
* [https://forums.dyalog.com/viewtopic.php?f=30&t=1621 Scalar functions] by [[Roger Hui]]


{{APL features}}
{{APL features}}[[Category:Kinds of functions]][[Category:Scalar functions| ]]

Navigation menu