Match: Difference between revisions
m (→History) |
m (Text replacement - "</source>" to "</syntaxhighlight>") |
||
Line 9: | Line 9: | ||
1 2 3 ≡ 1 2 5 | 1 2 3 ≡ 1 2 5 | ||
0 | 0 | ||
</ | </syntaxhighlight> | ||
Using [[strand notation]] to create [[nested]] arrays, we see that Match also compares elements nested within arrays: | Using [[strand notation]] to create [[nested]] arrays, we see that Match also compares elements nested within arrays: | ||
<source lang=apl> | <source lang=apl> | ||
(1 2)3 ≡ (2 1)3 | (1 2)3 ≡ (2 1)3 | ||
0 | 0 | ||
</ | </syntaxhighlight> | ||
Arrays with the same elements but different shapes do not match. Unlike [[Equal to]], Match never gives a [[RANK ERROR]] or [[LENGTH ERROR]] because of argument shapes. | Arrays with the same elements but different shapes do not match. Unlike [[Equal to]], Match never gives a [[RANK ERROR]] or [[LENGTH ERROR]] because of argument shapes. | ||
<source lang=apl> | <source lang=apl> | ||
Line 21: | Line 21: | ||
'a' ≡ ,'a' ⍝ Scalar versus singleton | 'a' ≡ ,'a' ⍝ Scalar versus singleton | ||
0 | 0 | ||
</ | </syntaxhighlight> | ||
Match depends on [[comparison tolerance]]. Here two arrays which are not exactly identical are still reported as matching: | Match depends on [[comparison tolerance]]. Here two arrays which are not exactly identical are still reported as matching: | ||
<source lang=apl> | <source lang=apl> | ||
Line 31: | Line 31: | ||
a {⎕CT←0⋄⍺≡⍵} b ⍝ ...but not intolerantly | a {⎕CT←0⋄⍺≡⍵} b ⍝ ...but not intolerantly | ||
0 | 0 | ||
</ | </syntaxhighlight> | ||
== Description == | == Description == | ||
Line 43: | Line 43: | ||
=== APL model === | === APL model === | ||
The following model implements Match in [[Dyalog APL]]. Because Match compares the same characteristic of both arguments, [[Over]] (<source lang=apl inline>⍥</ | The following model implements Match in [[Dyalog APL]]. Because Match compares the same characteristic of both arguments, [[Over]] (<source lang=apl inline>⍥</syntaxhighlight>) is used throughout. | ||
<source lang=apl> | <source lang=apl> | ||
Match ← { | Match ← { | ||
Line 52: | Line 52: | ||
⍺∧.∇⍥,⍵ ⍝ Recurse | ⍺∧.∇⍥,⍵ ⍝ Recurse | ||
} | } | ||
</ | </syntaxhighlight> | ||
{{Works in|[[Dyalog APL 18.0]]}} | {{Works in|[[Dyalog APL 18.0]]}} | ||
Line 64: | Line 64: | ||
* [http://microapl.com/apl_help/ch_020_020_360.htm APLX] | * [http://microapl.com/apl_help/ch_020_020_360.htm APLX] | ||
* [https://mlochbaum.github.io/BQN/doc/match.html BQN] | * [https://mlochbaum.github.io/BQN/doc/match.html BQN] | ||
* J [https://www.jsoftware.com/help/dictionary/d122.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/minusco#dyadic NuVoc] (as <source lang=j inline>-:</ | * J [https://www.jsoftware.com/help/dictionary/d122.htm Dictionary], [https://code.jsoftware.com/wiki/Vocabulary/minusco#dyadic NuVoc] (as <source lang=j inline>-:</syntaxhighlight>) | ||
== See also == | == See also == |
Revision as of 22:13, 10 September 2022
≡
|
Match (≡
) is a dyadic primitive function which indicates whether the left and right argument arrays are identical, taking into account comparison tolerance and possibly ignoring prototypes. The result of Match is always a Boolean scalar: it is 1 if the arrays match and 0 otherwise. Two arrays match if they have the same shape and their corresponding elements and possibly prototypes match—in flat array theory, the elements should be tolerantly equal to each other; in nested array theory, the definition is recursive with the base case of simple scalars, which also match if they are tolerantly equal.
Examples
Match can be used to compare simple vectors to see if they are the same: <source lang=apl>
1 2 3 ≡ 1 2 3
1
1 2 3 ≡ 1 2 5
0 </syntaxhighlight> Using strand notation to create nested arrays, we see that Match also compares elements nested within arrays: <source lang=apl>
(1 2)3 ≡ (2 1)3
0 </syntaxhighlight> Arrays with the same elements but different shapes do not match. Unlike Equal to, Match never gives a RANK ERROR or LENGTH ERROR because of argument shapes. <source lang=apl>
(3 2⍴⍳6) ≡ (2 3⍴⍳6)
0
'a' ≡ ,'a' ⍝ Scalar versus singleton
0 </syntaxhighlight> Match depends on comparison tolerance. Here two arrays which are not exactly identical are still reported as matching: <source lang=apl>
a←⎕CT+b←2 3 4 ⍝ Two vectors a - b ⍝ Difference is not zero
1.021405183E¯14 1.021405183E¯14 9.769962617E¯15
a ≡ b ⍝ They match tolerantly
1
a {⎕CT←0⋄⍺≡⍵} b ⍝ ...but not intolerantly
0 </syntaxhighlight>
Description
Arrays match if they are equal in all the characteristics defined by APL's array model:
- Shape (rank, and the length of each axis)
- Elements
- For empty arrays, possibly the array's type or prototype
The way elements are compared depends on the particular array model used. In flat array theory, elements match if they have the same type (such as numeric or character) and are tolerantly equal. If boxes are possible, then this definition is recursive, because boxes are defined to be equal when the arrays they contain match. In nested array theory, the definition is always recursive. As a base case, simple scalars are defined to match when they are tolerantly equal.
APL model
The following model implements Match in Dyalog APL. Because Match compares the same characteristic of both arguments, Over (<source lang=apl inline>⍥</syntaxhighlight>) is used throughout. <source lang=apl> Match ← {
⍺≠⍥(≢⍴)⍵: 0 ⍝ Rank ~⍺∧.=⍥⍴⍵: 0 ⍝ Shape 0=⍺⌈⍥≡⍵: ⍺=⍵ ⍝ For simple scalars, use = 0=×/⍴⍵: ⍺∇⍥⊃⍵ ⍝ Prototype for empty arrays ⍺∧.∇⍥,⍵ ⍝ Recurse
} </syntaxhighlight>
History
Match was defined in Jim Brown's 1971 Ph.D. thesis, "A Generalization of APL",[1] with the name "same". It was implemented in both NARS (called it "Equivalent") and SHARP APL[2] in 1981. Both Dyalog APL and APL2 included it with the name "Match" taken from SHARP, which has generally been used in later APLs as well.
External links
Documentation
- Dyalog
- APLX
- BQN
- J Dictionary, NuVoc (as <source lang=j inline>-:</syntaxhighlight>)
See also
References
- ↑ Jim Brown. "A Generalization of APL". 1971.
- ↑ "Composition and Enclosure". SATN-41, 1981-06-20.