Intersection: Difference between revisions

From APL Wiki
Jump to navigation Jump to search
m (Text replacement - "http://help.dyalog.com" to "https://help.dyalog.com")
m (Text replacement - "<source" to "<syntaxhighlight")
Line 3: Line 3:
== Examples ==
== Examples ==


Both arguments of Intersection is usually restricted to [[vector|vectors]]. Unlike sets in the mathematical sense, duplicate elements are allowed in both sides, and Intersection is usually implemented as "left argument [[replicate|filtered]] by its [[membership|existence]] in the right argument" <source lang=apl inline>(X∊Y)/X</source>, which preserves the order and multiplicity in the left argument. Both arguments can be [[nested array|nested arrays]].
Both arguments of Intersection is usually restricted to [[vector|vectors]]. Unlike sets in the mathematical sense, duplicate elements are allowed in both sides, and Intersection is usually implemented as "left argument [[replicate|filtered]] by its [[membership|existence]] in the right argument" <syntaxhighlight lang=apl inline>(X∊Y)/X</source>, which preserves the order and multiplicity in the left argument. Both arguments can be [[nested array|nested arrays]].


<source lang=apl>
<syntaxhighlight lang=apl>
       'ABRA'∩'CAR'
       'ABRA'∩'CAR'
ARA
ARA
Line 21: Line 21:
== Extension ==
== Extension ==
Some dialects allow Intersection to work on [[major cell]]s:
Some dialects allow Intersection to work on [[major cell]]s:
[https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/P@JR2wQTBaNHvVvUHR2dnJyBQJ0rEihojBB0dFTnetQ3FSgY8ahjZeT//wA Try it online!]<source lang=apl>
[https://tio.run/##SyzI0U2pTMzJT9dNrShJzUtJTfn/P@JR2wQTBaNHvVvUHR2dnJyBQJ0rEihojBB0dFTnetQ3FSgY8ahjZeT//wA Try it online!]<syntaxhighlight lang=apl>
       X←4 2⍴'AABBCCCC'
       X←4 2⍴'AABBCCCC'
       Y←3 2⍴'AABBAA'
       Y←3 2⍴'AABBAA'
Line 29: Line 29:
</source>{{Works in|[[Extended Dyalog APL]]}}
</source>{{Works in|[[Extended Dyalog APL]]}}
Others can easily define such a function:
Others can easily define such a function:
[https://tio.run/##SyzI0U2pTMzJT///P@JR2wQTBaNHvVvUHR2dnJyBQJ0rEihojBB0dFTn8swrSS0qTk0uyczPA8pWP@rd9ahn/6PeFRqPOhc96t2q@ahzKZB61LsZKFPL9ahvKlBVhAKyNoXI//8B Try it online!]<source lang=apl>
[https://tio.run/##SyzI0U2pTMzJT///P@JR2wQTBaNHvVvUHR2dnJyBQJ0rEihojBB0dFTn8swrSS0qTk0uyczPA8pWP@rd9ahn/6PeFRqPOhc96t2q@ahzKZB61LsZKFPL9ahvKlBVhAKyNoXI//8B Try it online!]<syntaxhighlight lang=apl>
       X←4 2⍴'AABBCCCC'
       X←4 2⍴'AABBCCCC'
       Y←3 2⍴'AABBAA'
       Y←3 2⍴'AABBAA'

Revision as of 21:49, 10 September 2022

Intersection () is a dyadic set function which computes the set intersection of the two vector arguments.

Examples

Both arguments of Intersection is usually restricted to vectors. Unlike sets in the mathematical sense, duplicate elements are allowed in both sides, and Intersection is usually implemented as "left argument filtered by its existence in the right argument" <syntaxhighlight lang=apl inline>(X∊Y)/X</source>, which preserves the order and multiplicity in the left argument. Both arguments can be nested arrays.

<syntaxhighlight lang=apl>

     'ABRA'∩'CAR'

ARA

      A←'THIS' 'AND' 'THAT'                                                  
      A∩'T'
                             ⍝ (No match for the single character T)
      A∩'AND'
                             ⍝ (No match for any of the three characters A N D)
      A∩⊂'AND'

┌───┐ │AND│ └───┘ </source>

Extension

Some dialects allow Intersection to work on major cells: Try it online!<syntaxhighlight lang=apl>

     X←4 2⍴'AABBCCCC'
     Y←3 2⍴'AABBAA'
     X∩Y

AA BB

</source>

Others can easily define such a function: Try it online!<syntaxhighlight lang=apl>

     X←4 2⍴'AABBCCCC'
     Y←3 2⍴'AABBAA'
     Intersection←{⍺⌿⍨(≢⍵)≥⍵⍳⍺}
     X Intersection Y

AA BB

</source>

Works in: Dyalog APL

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