Find: Difference between revisions
(→Empty left argument: Cut "overlay model" discussion: see talk page) |
m (Text replacement - "<source" to "<syntaxhighlight") Tags: Mobile edit Mobile web edit |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
{{Built-in|Find|⍷}} is a [[dyadic]] [[primitive function]] which | {{Built-in|Find|⍷}} is a [[dyadic]] [[primitive function]] which indicates where the left [[argument]] appears as a contiguous [[subarray]] of the right argument. | ||
== Examples == | == Examples == | ||
Line 5: | Line 5: | ||
Both arguments can be arrays of any [[shape]]. The entire left argument is tested against each position in the right argument. The result is a [[boolean]] array having the same shape as the right argument, where a 1 indicates the position of the [[first]] element of the matched subarray (which can be seen as the "leftmost" or "top left" position in case of a [[vector]] or [[matrix]]). If the left argument has lower [[rank]], it is treated as if the shape is prepended with ones. If the left argument has higher rank, Find does not error, but it is never found in the right argument (resulting in an all-zero array). | Both arguments can be arrays of any [[shape]]. The entire left argument is tested against each position in the right argument. The result is a [[boolean]] array having the same shape as the right argument, where a 1 indicates the position of the [[first]] element of the matched subarray (which can be seen as the "leftmost" or "top left" position in case of a [[vector]] or [[matrix]]). If the left argument has lower [[rank]], it is treated as if the shape is prepended with ones. If the left argument has higher rank, Find does not error, but it is never found in the right argument (resulting in an all-zero array). | ||
< | <syntaxhighlight lang=apl> | ||
'ANA'⍷'BANANA' ⍝ Matches may overlap | 'ANA'⍷'BANANA' ⍝ Matches may overlap | ||
0 1 0 1 0 0 | 0 1 0 1 0 0 | ||
Line 27: | Line 27: | ||
WEEK⍷'DAY' ⍝ WEEK not found in 'DAY'; left arg may have higher rank but it is never found | WEEK⍷'DAY' ⍝ WEEK not found in 'DAY'; left arg may have higher rank but it is never found | ||
0 0 0 | 0 0 0 | ||
</ | </syntaxhighlight> | ||
For [[nested array|nested arrays]], Find tests for exact [[match]] between the elements. | For [[nested array|nested arrays]], Find tests for exact [[match]] between the elements. | ||
< | <syntaxhighlight lang=apl> | ||
'BIRDS' 'NEST'⍷'BIRDS' 'NEST' 'SOUP' | 'BIRDS' 'NEST'⍷'BIRDS' 'NEST' 'SOUP' | ||
1 0 0 | 1 0 0 | ||
</ | </syntaxhighlight> | ||
== Model == | == Model == | ||
Find can be modelled as follows, where all possible subarrays of the right argument are checked to see if they [[match]] the left argument:<ref>[[Roger Hui|Hui, Roger]]. [https://forums.dyalog.com/viewtopic.php?f=30&t=1735 ⍷ follies]. Dyalog Forums. 16 Feb 2021.</ref> | Find can be modelled as follows (for non-empty left arguments), where all possible subarrays of the right argument are checked to see if they [[match]] the left argument:<ref>[[Roger Hui|Hui, Roger]]. [https://forums.dyalog.com/viewtopic.php?f=30&t=1735 ⍷ follies]. Dyalog Forums. 16 Feb 2021.</ref> | ||
< | <syntaxhighlight lang=apl> | ||
ebar←{⎕IO←0 | ebar←{⎕IO←0 | ||
r←(≢⍴⍺)⌈≢⍴⍵ ⍝ maximum rank | r←(≢⍴⍺)⌈≢⍴⍵ ⍝ maximum rank | ||
Line 46: | Line 46: | ||
(⍴⍵) ↑ ⍺∘{⍺≡(⍴⍺)↑⍵↓ww}¨ ⍳(×⍴⍺)+(⍴⍵)-⍴⍺ | (⍴⍵) ↑ ⍺∘{⍺≡(⍴⍺)↑⍵↓ww}¨ ⍳(×⍴⍺)+(⍴⍵)-⍴⍺ | ||
} | } | ||
</ | </syntaxhighlight> | ||
== Empty left argument == | === Empty left argument === | ||
Implementations differ in their treatment of empty left arguments: | Implementations differ in their treatment of empty left arguments: | ||
* [[APL2]], [[GNU APL]], [[NARS2000]], and [[Dyalog APL]] indicate positions where the left argument can fit, even if the [[prototype]]s don't match. | * [[APL2]], [[GNU APL]], [[NARS2000]], and [[Dyalog APL]] indicate positions where the left argument can fit, even if the [[prototype]]s don't match. | ||
Line 53: | Line 53: | ||
* [[APL+]] finds empty arrays everywhere, even where they would extend beyond the edges of the right argument. | * [[APL+]] finds empty arrays everywhere, even where they would extend beyond the edges of the right argument. | ||
The prototype is never used, in contrast to [[Match]], which in many APLs compares prototypes of empty arrays. The behavior may come from the use of the [[inner product]] < | The prototype is never used, in contrast to [[Match]], which in many APLs compares prototypes of empty arrays. The behavior may come from the use of the [[inner product]] <syntaxhighlight lang=apl inline>∧.=</syntaxhighlight> in early [[Array_model#Flat_array_theory|flat]] APLs where [[Match]] is not a primitive; this function naturally checks elements and not the prototype. [[Adin Falkoff]] presented code for Find-like functions using <syntaxhighlight lang=apl inline>∧.=</syntaxhighlight> at [[APL79]].<ref>[[Adin Falkoff|Falkoff, Adin]]. [https://dl.acm.org/doi/10.1145/390009.804448 A note on pattern matching: Where do you find the match to an empty array?] at [[APL79]].</ref> | ||
Correspondingly, the above model can be amended to match the behaviour of the primitive (in APL2, etc.) by replacing the <syntaxhighlight lang=apl inline>≡</syntaxhighlight> with <syntaxhighlight lang=apl inline>{(⍺≡⍥⍴⍵)∧(∧/⍺≡¨⍥,⍵)}</syntaxhighlight> which ignores prototypes, only comparing shape and elements:<ref>[[Adám Brudzewsky|Brudzewsky, Adám]]. [https://forums.dyalog.com/viewtopic.php?f=30&t=1735&p=7416#p7416 RE: ⍷ follies]. Dyalog Forums. 23 Aug 2022.</ref> | |||
<syntaxhighlight lang=apl> | |||
ebar2←{⎕IO←0 | |||
r←(≢⍴⍺)⌈≢⍴⍵ ⍝ maximum rank | |||
r>≢⍴⍺:(⍺⍴⍨(⍴⍺),⍨(r-≢⍴⍺)⍴1)∇ ⍵ ⍝ if ⍺ has lesser rank, make it the same rank | |||
(⍴⍺)∨.>r↑(⍴⍵),¯1:(⍴⍵)⍴0 ⍝ return 0s if ⍺ has greater rank or is longer | |||
ww←⍵ | |||
(⍴⍵) ↑ ⍺∘{⍺ {(⍺≡⍥⍴⍵)∧(∧/⍺≡¨⍥,⍵)} (⍴⍺)↑⍵↓ww}¨ ⍳(×⍴⍺)+(⍴⍵)-⍴⍺ | |||
} | |||
</syntaxhighlight> | |||
== See also == | == See also == | ||
* [[Membership]] | * [[Membership]] |
Latest revision as of 21:36, 10 September 2022
⍷
|
Find (⍷
) is a dyadic primitive function which indicates where the left argument appears as a contiguous subarray of the right argument.
Examples
Both arguments can be arrays of any shape. The entire left argument is tested against each position in the right argument. The result is a boolean array having the same shape as the right argument, where a 1 indicates the position of the first element of the matched subarray (which can be seen as the "leftmost" or "top left" position in case of a vector or matrix). If the left argument has lower rank, it is treated as if the shape is prepended with ones. If the left argument has higher rank, Find does not error, but it is never found in the right argument (resulting in an all-zero array).
'ANA'⍷'BANANA' ⍝ Matches may overlap 0 1 0 1 0 0 WEEK SUNDAY MONDAY TUESDAY WEDNESDAY THURSDAY FRIDAY SATURDAY 'DAY'⍷WEEK ⍝ Find the pattern 'DAY' in WEEK; right arg may have higher rank 0 0 0 1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 WEEK⍷'DAY' ⍝ WEEK not found in 'DAY'; left arg may have higher rank but it is never found 0 0 0
For nested arrays, Find tests for exact match between the elements.
'BIRDS' 'NEST'⍷'BIRDS' 'NEST' 'SOUP' 1 0 0
Model
Find can be modelled as follows (for non-empty left arguments), where all possible subarrays of the right argument are checked to see if they match the left argument:[1]
ebar←{⎕IO←0 r←(≢⍴⍺)⌈≢⍴⍵ ⍝ maximum rank r>≢⍴⍺:(⍺⍴⍨(⍴⍺),⍨(r-≢⍴⍺)⍴1)∇ ⍵ ⍝ if ⍺ has lesser rank, make it the same rank (⍴⍺)∨.>r↑(⍴⍵),¯1:(⍴⍵)⍴0 ⍝ return 0s if ⍺ has greater rank or is longer ww←⍵ (⍴⍵) ↑ ⍺∘{⍺≡(⍴⍺)↑⍵↓ww}¨ ⍳(×⍴⍺)+(⍴⍵)-⍴⍺ }
Empty left argument
Implementations differ in their treatment of empty left arguments:
- APL2, GNU APL, NARS2000, and Dyalog APL indicate positions where the left argument can fit, even if the prototypes don't match.
- APLX never finds any empty arrays.
- APL+ finds empty arrays everywhere, even where they would extend beyond the edges of the right argument.
The prototype is never used, in contrast to Match, which in many APLs compares prototypes of empty arrays. The behavior may come from the use of the inner product ∧.=
in early flat APLs where Match is not a primitive; this function naturally checks elements and not the prototype. Adin Falkoff presented code for Find-like functions using ∧.=
at APL79.[2]
Correspondingly, the above model can be amended to match the behaviour of the primitive (in APL2, etc.) by replacing the ≡
with {(⍺≡⍥⍴⍵)∧(∧/⍺≡¨⍥,⍵)}
which ignores prototypes, only comparing shape and elements:[3]
ebar2←{⎕IO←0 r←(≢⍴⍺)⌈≢⍴⍵ ⍝ maximum rank r>≢⍴⍺:(⍺⍴⍨(⍴⍺),⍨(r-≢⍴⍺)⍴1)∇ ⍵ ⍝ if ⍺ has lesser rank, make it the same rank (⍴⍺)∨.>r↑(⍴⍵),¯1:(⍴⍵)⍴0 ⍝ return 0s if ⍺ has greater rank or is longer ww←⍵ (⍴⍵) ↑ ⍺∘{⍺ {(⍺≡⍥⍴⍵)∧(∧/⍺≡¨⍥,⍵)} (⍴⍺)↑⍵↓ww}¨ ⍳(×⍴⍺)+(⍴⍵)-⍴⍺ }
See also
External links
Documentation
- Dyalog
- APLX
- J Dictionary, NuVoc
- BQN
References
- ↑ Hui, Roger. ⍷ follies. Dyalog Forums. 16 Feb 2021.
- ↑ Falkoff, Adin. A note on pattern matching: Where do you find the match to an empty array? at APL79.
- ↑ Brudzewsky, Adám. RE: ⍷ follies. Dyalog Forums. 23 Aug 2022.