RE: Union[{0},{0.}] = {0,0.}?
- To: mathgroup at smc.vnet.net
- Subject: [mg12706] RE: [mg12662] Union[{0},{0.}] = {0,0.}?
- From: Ersek_Ted%PAX1A at mr.nawcad.navy.mil
- Date: Wed, 3 Jun 1998 02:20:58 -0400
- Sender: owner-wri-mathgroup at wolfram.com
farr at brown.edu wrote:
|
|Union[{0},{0.}] = {0,0.}.
|
|Why?
|
|I would like the above to return only {0}. |
|The root of the problem is that Mathematica thinks they are the same: |
|SameQ[{0},{0.}] = False
|
|How can I get around this? The following doesn't help either. |
|Union[{ N[0] },{0.}] = {0,0.}
|
|
You can specify a SameTest to get the result you want. The default
(Automatic) is ambiguous. Based on the results you get it seems like
the default (Automatic) is actually (SameQ). Wouldn't it be nice if it
would simply say
Out[1]= {SameTest\[Rule]SameQ} ??
In[1]:=
Options[Union]
Out[1]=
{SameTest\[Rule]Automatic}
____________________________________
The (SameTest) below will do what you want.
In[2]:=
compare[x_,y_]:=
If[ x===y || (x===0&&y===0.0) || (x===0.0&&y===0),
True, False]
In[3]:=
Clear[x,y];
Union[ {1, 0.0, 2, x, 0, 0, 1, y, 2, 2, 1, 0.0}, SameTest->compare ]
Out[3]=
{0,1,2,x,y}
Ted Ersek