[Date Index]
[Thread Index]
[Author Index]
Re: Q: Union and SameTest Option
*To*: mathgroup at smc.vnet.net
*Subject*: [mg16048] Re: [mg16016] Q: Union and SameTest Option
*From*: BobHanlon at aol.com
*Date*: Sun, 21 Feb 1999 00:15:21 -0500
*Sender*: owner-wri-mathgroup at wolfram.com
In a message dated 2/20/99 6:43:15 AM, btml01 at uni-bayreuth.de writes:
>Can anybody explain to me what's going on here ?
>
>In[270]:=
>Union[ {{a,b,1},{a,c,1},{x,y,2},{a,b,2}},
> SameTest->(#1[[2]]===#2[[2]]&) ]
>Out[270]=
>{{a,b,1},{a,c,1},{x,y,2}}
>
>is ok since the first and the last list element have the same second
>component. But what is wrong here now ?
>
>In[272]:=
>Union[ {{a,b,1},{a,c,1},{x,y,2},{a,b,2}},
> SameTest->(#1[[3]]===#2[[3]]&) ]
>Out[272]=
>{{a,b,1},{a,b,2},{a,c,1},{x,y,2}}
>
Clemens,
test = {{a,b,1},{a,c,1},{x,y,2},{a,b,2}};
Union[ test, SameTest->(#1[[2]]===#2[[2]]&) ]
{{a, b, 1}, {a, c, 1}, {x, y, 2}}
Union[ test, SameTest->(#1[[3]]===#2[[3]]&) ]
{{a, b, 1}, {a, b, 2}, {a, c, 1}, {x, y, 2}}
The first case worked not because the second elements were equal,
but because the first elements were also equal. Note that the
first case does not work if {a, b, 2} is changed to {e, b, 2}.
Union[ {{a,b,1},{a,c,1},{x,y,2},{e,b,2}},
SameTest->(#1[[2]]===#2[[2]]&) ]
{{a, b, 1}, {a, c, 1}, {e, b, 2}, {x, y, 2}}
Based on this behavior, I would guess that Mathematica first sorts
the elements, then compares only adjacent elements for "sameness".
Consequently, to implement the "Union" that you want, you need to
modify the approach.
myUnion[x_List, n_Integer?Positive] := Module[
{temp = RotateLeft[#, n-1]& /@ x},
temp = Union[temp, SameTest -> (#1[[1]] === #2[[1]]&)];
RotateRight[#, n-1]& /@ temp];
Table[myUnion[test, k], {k, 3}]//ColumnForm
{{a, b, 1}, {x, y, 2}}
{{a, b, 1}, {a, c, 1}, {x, y, 2}}
{{a, b, 1}, {a, b, 2}}
Bob Hanlon
Prev by Date:
**Change Rank in Compile**
Next by Date:
**RE: Forms Interface?**
Previous by thread:
**Q: Union and SameTest Option**
Next by thread:
**Re: Q: Union and SameTest Option**
| |