Scalar function: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
No edit summary
Line 3: Line 3:
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>.


== User defined scalar functions ==
In dialects with the [[Over]] 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 [[derived monadic operator]] <source lang=apl inline>perv←⍥0</source>. 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:
<source lang=apl>
perv←{⍺←⊢              ⍝ Scalar pervasion
    1=≡⍺ ⍵ ⍵:⍺ ⍺⍺ ⍵    ⍝ (⍺ and) ⍵ depth 0: operand fn application
            ⍺ ∇¨⍵      ⍝ (⍺ or) ⍵ deeper: recursive traversal.
}
</source>
== Standard scalar functions ==
== Standard scalar functions ==


Line 80: Line 72:
|-
|-
|}
|}
== User defined scalar functions ==
In [[nested array model]] dialects with the [[Depth (operator)|Depth]] operator (<source lang=apl inline>⍥</source> or in J, ''Level at'' <source lang=j inline>L:</source> ), any function can be used as a scalar function (that is, be applied to all simple scalars) using the [[derived monadic operator]] <source lang=apl inline>perv←⍥0</source>:
[https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/qKPtUduE6kd9U4HUo56mlMzigke9u2ofdbcAxYJdgUR4sLprWWpeibqGenBqcXFmfl5AUSaQq6AO1KyuyfWoo70CqNk5PyVVoeJ/QWpRGcio3qUGXH75ecHJiTmJRW55YEt6t1qpA/UBzVZQT1OvRZZXAOlT0DBQMNRUMFTQeNTVZKhgoAl0XjsXyOBHvWv@AwA Try it online!]


<source lang=apl>
      perv←⍥0
      NonScalarFn←{⍵:'t' ⋄ 'f'}
      NonScalarFn perv (0 1) 1 (⊂1 0)
┌──┬─┬────┐
│ft│t│┌──┐│
│  │ ││tf││
│  │ │└──┘│
└──┴─┴────┘
</source>
{{Works in|[[dzaima/APL]], [[Extended Dyalog 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 ==



Revision as of 23:46, 31 March 2020

A scalar function is one of a class of primitive functions that apply to arguments 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 languages scalar functions recursively descend into nested arrays until they can be applied to simple scalars; in 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 ~ with non-scalar dyad Without, and similarly scalar Roll and non-scalar Deal are both written ?.

Standard scalar functions

Most APLs use a set of scalar functions that was worked out fairly early in APL's development. These are listed in this section.

Monadic function Glyph Dyadic function
Conjugate + Plus
Negate - Minus
Signum or Direction × Times
Reciprocal ÷ Divide
Floor Minimum
Ceiling Maximum
Exponential * Power function
Natural Logarithm Logarithm
Magnitude or Absolute value | Residue
Pi Times Circle function
Factorial ! Binomial coefficient or combination function
Roll ?
Not ~
Logical And
Logical Or
Nand
Nor
< Less than
Less than or equal to
= Equal to
Greater than or equal to
> Greater than
Not equal to

Additional scalar functions

Very few additional scalar functions have been added later in various dialects:

Monadic function Glyph Dyadic function
Square Root Nth Root
Type or
Lowest Common Multiple (LCM)
Greatest Common Divisor (GCD)

User defined scalar functions

In nested array model dialects with the Depth operator ( or in J, Level at L: ), any function can be used as a scalar function (that is, be applied to all simple scalars) using the derived monadic operator perv←⍥0:

Try it online!

      perv←⍥0
      NonScalarFn←{⍵:'t' ⋄ 'f'}
      NonScalarFn perv (0 1) 1 (⊂1 0)
┌──┬─┬────┐
│ft│t│┌──┐│
│  │ ││tf││
│  │ │└──┘│
└──┴─┴────┘

In dialects that support dfns, this operator can be defined[1] as:

perv←{⍺←⊢               ⍝ Scalar pervasion
    1=≡⍺ ⍵ ⍵:⍺ ⍺⍺ ⍵     ⍝ (⍺ and) ⍵ depth 0: operand fn application
             ⍺ ∇¨⍵      ⍝ (⍺ or) ⍵ deeper: recursive traversal.
}
Works in: Dyalog APL

External links


APL features [edit]
Built-ins Primitives (functions, operators) ∙ Quad name
Array model ShapeRankDepthBoundIndex (Indexing) ∙ AxisRavelRavel orderElementScalarVectorMatrixSimple scalarSimple arrayNested arrayCellMajor cellSubarrayEmpty arrayPrototype
Data types Number (Boolean, Complex number) ∙ Character (String) ∙ BoxNamespaceFunction array
Concepts and paradigms Conformability (Scalar extension, Leading axis agreement) ∙ Scalar function (Pervasion) ∙ Identity elementComplex floorArray ordering (Total) ∙ Tacit programming (Function composition, Close composition) ∙ GlyphLeading axis theoryMajor cell search
Errors LIMIT ERRORRANK ERRORSYNTAX ERRORDOMAIN ERRORLENGTH ERRORINDEX ERRORVALUE ERROREVOLUTION ERROR
  1. John Scholes, perv (Scalar pervasion). dfns workspace, 2019-02-17.