Re: Intersection[...,SameTest] ?

• To: mathgroup at smc.vnet.net
• Subject: [mg33083] Re: [mg33054] Intersection[...,SameTest] ?
• From: Andrzej Kozlowski <andrzej at bekkoame.ne.jp>
• Date: Fri, 1 Mar 2002 06:51:46 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```The function works perfectly well in both cases in my version of
Mathematica. (Mathematica 4.1 for MacOS X).  I get
a = {1, 2, 3, 4};
b = {2, 4, 7, 8};

In[3]:=
Intersection[a, b, SameTest -> (#1 == 2*#2 & )]

Out[3]=
{4}

To see that this is indeed right one need only look at Trace. However,
this becomes clearer if you first look at the result of Union:

In[4]:=
Union[a, b, SameTest -> (#1 == 2*#2 & )]

Out[4]=
{1, 3, 4, 4, 7}

Observe that you got two 4's but no 2 or 8. Indeed, both of them are
equal to 4 however the two 4's are no longer equal to each other!  In
other words your SameTest is not an equivalence relation.

Looking at Trace you can see exactly what happened:

In[5]:=
Trace[Union[a, b, SameTest -> (#1 == 2*#2 & )],
TraceInternal -> True]

Out[5]=
{{a, {1, 2, 3, 4}}, {b, {2, 4, 7, 8}},

{SameTest -> (#1 == 2 #2 & ),

SameTest -> (#1 == 2 #2 & )},

Union[{1, 2, 3, 4}, {2, 4, 7, 8},

SameTest -> (#1 == 2 #2 & )],

{(#1 == 2 #2 & )[2, 1], 2 == 2 1, {2 1, 2}, 2 == 2, True},

{(#1 == 2 #2 & )[2, 1], 2 == 2 1, {2 1, 2}, 2 == 2, True},

{(#1 == 2 #2 & )[3, 1], 3 == 2 1, {2 1, 2}, 3 == 2,

False}, {(#1 == 2 #2 & )[4, 3], 4 == 2 3, {2 3, 6},

4 == 6, False}, {(#1 == 2 #2 & )[4, 1], 4 == 2 1,

{2 1, 2}, 4 == 2, False},

{(#1 == 2 #2 & )[4, 4], 4 == 2 4, {2 4, 8}, 4 == 8,

False}, {(#1 == 2 #2 & )[4, 3], 4 == 2 3, {2 3, 6},

4 == 6, False}, {(#1 == 2 #2 & )[4, 1], 4 == 2 1,

{2 1, 2}, 4 == 2, False},

{(#1 == 2 #2 & )[7, 4], 7 == 2 4, {2 4, 8}, 7 == 8,

False}, {(#1 == 2 #2 & )[7, 4], 7 == 2 4, {2 4, 8},

7 == 8, False}, {(#1 == 2 #2 & )[7, 3], 7 == 2 3,

{2 3, 6}, 7 == 6, False},

{(#1 == 2 #2 & )[7, 1], 7 == 2 1, {2 1, 2}, 7 == 2,

False}, {(#1 == 2 #2 & )[8, 7], 8 == 2 7, {2 7, 14},

8 == 14, False}, {(#1 == 2 #2 & )[8, 4], 8 == 2 4,

{2 4, 8}, 8 == 8, True}, {1, 3, 4, 4, 7}}

Doing the same thing with Intersection shows that everything is exactly
as it should be in your other cases. However, this can be a bit
confusing since the answer is the same as you would get with the Default
value of SameTest. This is of course only an accident, as you can
clearly see by doing Trace as above. Moreover, Intersection with your
SameTest is not commutative:

In[6]:=
Intersection[b, a, SameTest -> (#1 == 2*#2 & )]

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

This is again completely right. The point is of course that SameTest
really ought to be given by an equivalence relation, other wise you can
expect "weird" answers. The confusion is however entirely your own.

Andrzej Kozlowski
Toyama International University
JAPAN
http://platon.c.u-tokyo.ac.jp/andrzej/

On Wednesday, February 27, 2002, at 06:48  AM, 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
> curious about Intersection[])
>
>
>

```

• Prev by Date: perhaps? RE: Finding pattern Matched series
• Next by Date: Definition needed !
• Previous by thread: Re: Intersection[...,SameTest] ?
• Next by thread: Re: Intersection[...,SameTest] ?