Comparison function: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Text replacement - "{{APL built-ins}}" to "{{APL built-ins}}Category:Primitive functions")
m (Text replacement - "<source" to "<syntaxhighlight")
Tags: Mobile edit Mobile web edit
 
(3 intermediate revisions by 3 users not shown)
Line 3: Line 3:
|}
|}


A '''comparison function''' is one of the six [[primitive function|primitive]] [[dyadic]] [[scalar function]]s whose result on scalars is a [[Boolean]] determined by the numerical ordering of the two [[argument]]s. Comparisons are subject to [[comparison tolerance]]. The comparison functions can be subdivided into two equalities <source lang=apl inline>=</source> <source lang=apl inline>≠</source>, which depend only on whether the arguments are equal and not the ordering of unequal argument, and may be applied to [[character]]s, and four inequalities <source lang=apl inline><</source> <source lang=apl inline>≤</source> <source lang=apl inline>≥</source> <source lang=apl inline>></source>, which depend on ordering and may not be applied to [[character]]s.
A '''comparison function''' is one of the six [[primitive function|primitive]] [[dyadic]] [[scalar function]]s whose result on scalars is a [[Boolean]] determined by the numerical ordering of the two [[argument]]s. Comparisons are subject to [[comparison tolerance]]. The comparison functions can be subdivided into two equalities <syntaxhighlight lang=apl inline>=</syntaxhighlight> <syntaxhighlight lang=apl inline>≠</syntaxhighlight>, which depend only on whether the arguments are equal and not on the ordering of the arguments, and may be applied to [[character]]s, and four inequalities <syntaxhighlight lang=apl inline><</syntaxhighlight> <syntaxhighlight lang=apl inline>≤</syntaxhighlight> <syntaxhighlight lang=apl inline>≥</syntaxhighlight> <syntaxhighlight lang=apl inline>></syntaxhighlight>, which depend on ordering and may not be applied to [[character]]s.


The six comparison functions are [[Less Than]] (<source lang=apl inline><</source>), [[Less Than or Equal]] (<source lang=apl inline>≤</source>), [[Equal to]] (<source lang=apl inline>=</source>), [[Greater Than or Equal]] (<source lang=apl inline>≥</source>), [[Greater Than]] (<source lang=apl inline>></source>), and [[Not Equal to]] (<source lang=apl inline>≠</source>).
The six comparison functions are [[Less Than]] (<syntaxhighlight lang=apl inline><</syntaxhighlight>), [[Less Than or Equal]] (<syntaxhighlight lang=apl inline>≤</syntaxhighlight>), [[Equal to]] (<syntaxhighlight lang=apl inline>=</syntaxhighlight>), [[Greater Than or Equal]] (<syntaxhighlight lang=apl inline>≥</syntaxhighlight>), [[Greater Than]] (<syntaxhighlight lang=apl inline>></syntaxhighlight>), and [[Not Equal to]] (<syntaxhighlight lang=apl inline>≠</syntaxhighlight>). You can take a closer look at how these six comparison functions behave in [https://nbviewer.jupyter.org/github/Dyalog/dyalog-jupyter-notebooks/blob/master/Closer%20look%20at%20the%20comparison%20functions.ipynb this APL Jupyter notebook].


Because comparison functions have Boolean results on Boolean arguments (they always have Boolean results), they are [[Boolean function]]s. In fact, the majority of nontrivial dyadic Boolean functions are comparison functions.
Because comparison functions have Boolean results on Boolean arguments (they always have Boolean results), they are [[Boolean function]]s. In fact, the majority of nontrivial dyadic Boolean functions are comparison functions.
Line 18: Line 18:
!                                      < !! = !! >
!                                      < !! = !! >
|-
|-
| <source lang=apl inline><</source> || 1 || 0 || 0
| <syntaxhighlight lang=apl inline><</syntaxhighlight> || 1 || 0 || 0
|-
|-
| <source lang=apl inline>≤</source> || 1 || 1 || 0
| <syntaxhighlight lang=apl inline>≤</syntaxhighlight> || 1 || 1 || 0
|-
|-
| <source lang=apl inline>=</source> || 0 || 1 || 0
| <syntaxhighlight lang=apl inline>=</syntaxhighlight> || 0 || 1 || 0
|-
|-
| <source lang=apl inline>≥</source> || 0 || 1 || 1
| <syntaxhighlight lang=apl inline>≥</syntaxhighlight> || 0 || 1 || 1
|-
|-
| <source lang=apl inline>></source> || 0 || 0 || 1
| <syntaxhighlight lang=apl inline>></syntaxhighlight> || 0 || 0 || 1
|-
|-
| <source lang=apl inline>≠</source> || 1 || 0 || 1
| <syntaxhighlight lang=apl inline>≠</syntaxhighlight> || 1 || 0 || 1
|}
|}


The traditional APL ordering of the comparison functions used above, which was used at least as early as [[APL\360]], resembles a [[wikipedia:Gray code|Gray code]] ordering of the values which define the comparison functions. Adjacent values differ by only a change of one bit, but the list is not a true Gray code because it excludes the all-0 and all-1 combinations.
The traditional APL ordering of the comparison functions used above, which was used at least as early as [[APL\360]], resembles a [[wikipedia:Gray code|Gray code]] ordering of the values which define the comparison functions. Adjacent values differ by only a change of one bit, but the list is not a true Gray code because it excludes the all-0 and all-1 combinations.
{{APL built-ins}}[[Category:Primitive functions]]
{{APL built-ins}}[[Category:Comparison functions| ]][[Category:Lists]]

Latest revision as of 22:24, 10 September 2022

< = >

A comparison function is one of the six primitive dyadic scalar functions whose result on scalars is a Boolean determined by the numerical ordering of the two arguments. Comparisons are subject to comparison tolerance. The comparison functions can be subdivided into two equalities = , which depend only on whether the arguments are equal and not on the ordering of the arguments, and may be applied to characters, and four inequalities < >, which depend on ordering and may not be applied to characters.

The six comparison functions are Less Than (<), Less Than or Equal (), Equal to (=), Greater Than or Equal (), Greater Than (>), and Not Equal to (). You can take a closer look at how these six comparison functions behave in this APL Jupyter notebook.

Because comparison functions have Boolean results on Boolean arguments (they always have Boolean results), they are Boolean functions. In fact, the majority of nontrivial dyadic Boolean functions are comparison functions.

Classification

The result of a comparison function on real numbers is determined by their ordering, which falls into one of three possible cases: the left argument is smaller (more negative), the arguments are tolerantly equal, or the right argument is larger. A particular comparison function can thus be thought of as a choice of one Boolean value (0 or 1) for each of these three cases, which implies that there are eight total possibilities. Two of the possibilities—all 0 and all 1—are trivial and cannot be said to depend on the ordering of the arguments; they are not considered comparison functions. Every remaining possibility is one of the APL comparisons:

Function Results
< = >
< 1 0 0
1 1 0
= 0 1 0
0 1 1
> 0 0 1
1 0 1

The traditional APL ordering of the comparison functions used above, which was used at least as early as APL\360, resembles a Gray code ordering of the values which define the comparison functions. Adjacent values differ by only a change of one bit, but the list is not a true Gray code because it excludes the all-0 and all-1 combinations.

APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare RootRound
Dyadic AddSubtractTimesDivideResiduePowerLogarithmMinimumMaximumBinomialComparison functionsBoolean functions (And, Or, Nand, Nor) ∙ GCDLCMCircularComplexRoot
Non-Scalar
Structural ShapeReshapeTallyDepthRavelEnlistTableCatenateReverseRotateTransposeRazeMixSplitEncloseNestCut (K)PairLinkPartitioned EnclosePartition
Selection FirstPickTakeDropUniqueIdentityStopSelectReplicateExpandSet functions (IntersectionUnionWithout) ∙ Bracket indexingIndexCartesian ProductSort
Selector Index generatorGradeIndex OfInterval IndexIndicesDealPrefix and suffix vectors
Computational MatchNot MatchMembershipFindNub SieveEncodeDecodeMatrix InverseMatrix DivideFormatExecuteMaterialiseRange
Operators Monadic EachCommuteConstantReplicateExpandReduceWindowed ReduceScanOuter ProductKeyI-BeamSpawnFunction axisIdentity (Null, Ident)
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)Identity (Lev, Dex)
Quad names Index originComparison toleranceMigration levelAtomic vector