Equal to

From APL Wiki
Revision as of 22:14, 10 September 2022 by Adám Brudzewsky (talk | contribs) (Text replacement - "<source" to "<syntaxhighlight")
Jump to navigation Jump to search
=

Equal to (=) is a comparison function which tests whether argument elements are tolerantly equal to each other: it returns 1 if the elements being compared match and 0 if they do not. It is the negation of Not Equal to (<syntaxhighlight lang=apl inline>≠</source>).

APL's <syntaxhighlight lang=apl inline>=</source> differs from the usage of in traditional mathematical notation by having an implied Iverson bracket. Thus, <syntaxhighlight lang=apl inline>i=j</source> is APL is equivalent to or (Kronecker delta) in mathematics.

Examples

Equal to compares arrays one element at a time, returning 1 when elements match and 0 when they do not. <syntaxhighlight lang=apl>

     3 2 1 = 1 2 3

0 1 0

     '321' = '123'

0 1 0

     3j2 2j2 1j2 = 2j2

0 1 0 </source> It is subject to comparison tolerance: floating-point numbers which are very close together are considered equal, even though they are slightly different: <syntaxhighlight lang=apl>

     ⎕CT

1E¯14

     x ← 1 + (⎕CT×0.6)ׯ2 ¯1 0 1 2     ⍝ Numbers close to 1
     1 - x                             ⍝ Nonzero difference

1.199040867E¯14 5.995204333E¯15 0 ¯5.995204333E¯15 ¯1.199040867E¯14

     1 = x                             ⍝ Close but different values are equal

0 1 1 1 0

     1 {⎕CT←0⋄⍺=⍵} x                   ⍝ Overriding ⎕CT within a dfn

0 0 1 0 0 </source> In a nested APL, Equal to is pervasive, and compares all simple scalars found in a nested array. <syntaxhighlight lang=apl>

     ⎕←n ← ((2 1)0)(1 0 3)(2 2⍴4 0)

┌───────┬─────┬───┐ │┌───┬─┐│1 0 3│4 0│ ││2 1│0││ │4 0│ │└───┴─┘│ │ │ └───────┴─────┴───┘

     0 = n

┌───────┬─────┬───┐ │┌───┬─┐│0 1 0│0 1│ ││0 0│1││ │0 1│ │└───┴─┘│ │ │ └───────┴─────┴───┘ </source> In a flat array language such as J, it instead compares Boxes directly, since they are the elements of a boxed array. <syntaxhighlight lang=j>

  (<'string') = 'which';'is';'string';'?'

0 0 1 0 </source>

Boolean function

Main article: Boolean function

When the arguments to Equal to are Boolean, it is the Logical biconditional function, or the negation of the xor function (which is Not Equal to restricted to Boolean arguments):

<syntaxhighlight lang=apl inline>=</source> <syntaxhighlight lang=apl inline>0</source> <syntaxhighlight lang=apl inline>1</source>
<syntaxhighlight lang=apl inline>0</source> <syntaxhighlight lang=apl inline>1</source> <syntaxhighlight lang=apl inline>0</source>
<syntaxhighlight lang=apl inline>1</source> <syntaxhighlight lang=apl inline>0</source> <syntaxhighlight lang=apl inline>1</source>

In the context of logic, it can be read as if and only if. This function is also known as xnor or the Kronecker delta.

Reduction with <syntaxhighlight lang=apl inline>=</source> is the same as reduction with xor (<syntaxhighlight lang=apl inline>≠</source>), but inverted if the number of reduced elements is even. The left and right identity element for Boolean <syntaxhighlight lang=apl inline>=</source> is 1.

External links

Documentation

APL built-ins [edit]
Primitives (Timeline) Functions
Scalar
Monadic ConjugateNegateSignumReciprocalMagnitudeExponentialNatural LogarithmFloorCeilingFactorialNotPi TimesRollTypeImaginarySquare Root
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 axis
Dyadic BindCompositions (Compose, Reverse Compose, Beside, Withe, Atop, Over) ∙ Inner ProductDeterminantPowerAtUnderRankDepthVariantStencilCutDirect definition (operator)
Quad names Index originComparison toleranceMigration levelAtomic vector