Re: Intersection[...,SameTest] ?

• To: mathgroup at smc.vnet.net
• Subject: [mg33077] Re: [mg33054] Intersection[...,SameTest] ?
• From: Daniel Lichtblau <danl at wolfram.com>
• Date: Fri, 1 Mar 2002 06:51:31 -0500 (EST)
• References: <200202270548.AAA18183@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

Konstantin L Kouptsov wrote:
>
> Intersection[..., SameTest->test] seem to do just that. However:
>
> 1. It is not clear from the manual (book or help browser), what this command
> is supposed to return.
>
> 2. It simply does not work:
>
> In[83]:=
> a={1,2,3,4};
> b={2,4,7,8};
> Intersection[a,b,SameTest->(#1==2*#2&)]
>
> Out[83]=
> {1}
>
> or more sophisticated:
>
> In[85]:=
> a={1,2,3,4};
> b={2,4,7,8};
> Intersection[a,b,SameTest->
>         ((Print[#1,"=?=",#2]||True)
>         &&#1==2*#2
>         &&(Print[True]||True)&)
> ]
>
> 2 =?= 1
> True
> 3 =?= 1
> 4 =?= 3
> 2 =?= 1
> True
> 4 =?= 3
> 4 =?= 4
>
> Out[85]=
> {1}
>
> which does not seem to enumerate all pairs.
>
> What idea stands behind this function? what is supposed to do?
>
> (well, for my task I sure could run double For[] cycle, but I am

The result will be quite dependent on the internals of Intersect. It
will first sort the first argument (I am not sure if this is the right
thing to do, or if it should be regarded as a bug). It will next prune
that sorted first argument to remove duplicates. It then compares
elements in the first argument against elements in the second argument,
based on the specified SameTest predicate.

The fact that it does not compare all of them in your example may indeed
be a bug. What I get running version 4.1 of Mathematica is:

In[194]:= Intersection[a,b,SameTest->
((Print[#1,"=?=",#2]||True)
&&#1==2*#2
&&(Print[True]||True)&)
]

1=?=2
1=?=4
1=?=7
1=?=8
2=?=2
2=?=4
2=?=7
2=?=8
3=?=2
3=?=4
3=?=7
3=?=8
4=?=2
True

Out[194]= {4}

My guess is that you are running an older version of Mathematica and
that that version is reversing the arguments in the comparison. It may
also have obtained something different in the pruning stage, perhaps
again from comparing elements in reversed order.

Note that reversing the arguments to Intersect will give a different
result.

In[196]:= Intersection[b,a,SameTest->
((Print[#1,"=?=",#2]||True)
&&#1==2*#2
&&(Print[True]||True)&)
]

2=?=1
True
4=?=1
4=?=2
True
7=?=1
7=?=2
7=?=3
7=?=4
8=?=1
8=?=2
8=?=3
8=?=4
True

Out[196]= {2, 4, 8}

Daniel Lichtblau
Wolfram Research

• Prev by Date: Re: Finding pattern Matched series
• Next by Date: Re: Notebooks and CVS: how to automatically strip output?
• Previous by thread: Re: Intersection[...,SameTest] ?
• Next by thread: Re: Intersection[...,SameTest] ?