Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Select and Cases Give Different Answers

  • To: mathgroup at smc.vnet.net
  • Subject: [mg99168] Re: [mg99125] Select and Cases Give Different Answers
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Wed, 29 Apr 2009 03:49:28 -0400 (EDT)
  • References: <200904280842.EAA28447@smc.vnet.net>

On 28 Apr 2009, at 17:42, Gregory Lypny wrote:

> Hello everyone,
>
> Suppose I have the following list that is a mixture of the string "NA"
> and five numeric quantities.
>
> 	theList = {"NA", "NA", -0.01315, 0.0120957, -1/41, 0.00625, "NA",
> "NA", 5/8,  "NA"}
>
> I want to weed out the NAs, and all of the following
>
> 	Cases[theList, _?NumericQ]
>
> 	DeleteCases[theList, _?StringQ]
>
> 	Select[theList, # =E2=88=88 Reals &]
>
> 	Select[theList, # =!= "NA" &]
>
> work fine because each returns {-0.01315, 0.0120957, -(1/41), 0.00625,
> 5/8}.
>
> But I don't understand why Unequal, when used as the criterion for
> Select as in
>
> 	Select[theList, # != "NA" &],
>
> fails to return the rationals, and I get only three elements
> {-0.01315, 0.0120957, 0.00625}.  I'd appreciate any clarification on
> the difference between UnsameQ and Unequal because my inclination in
> most circumstances is to use an equal sign or an unequal sign to make
> simple comparisons yet it doesn't give me the answer I expect, and I'd
> hate to make a costly mistake somewhere down the road!
>
> Regards,
>
> 	Gregory
>


I think that there is indeed something strange here, but it it the  
opposite of what you seem to find strange. To me the strange thing  
here is the difference between strings and symbols and between exact  
and approximate numbers . Replace your strings "NA" by symbols NA and  
note that now you get:

Select[theList2, #1 != NA & ]
{}

which is what I would have expected also in your example. In the case  
of a symbol Unequal and UnsameQ are different, as one would expect:

UnsameQ[x, 2]
True

Unequal[x, 2]
x != 2

An undefined symbol x is certainly "not the same as" (i.e. not  
identical) the number 2, but it could be equal to it (depending on the  
context) so Unequal[x,2] can't be answered. But when we replace x by  
the string "x" things become mysterious. First of all we have:

UnsameQ["x", 2]
True

  Unequal["x", 2]
  True

so now there is no difference between UnsameQ and Unequal. One might  
think that Strings are never equal to numbers (which would be  
reasonable, since they can't be assigned values) but then:

  "x" =!= 2/3
True

"x" != 2/3
"x" != 2/3

so when we have a rational on the RHS strings behave the same as  
symbols! But not so with reals:

"x" =!= N[2/3]
True
"x" != N[2/3]
True

By the way, if we replace Rational[2,3] with Complex[2,3] the  
behaviour will remain the same. All this is puzzling to me and I can't  
see any other explanation but that this is just an unintended side- 
effect of something. Obviously it does not make any mathematical sense  
to ask the question whether a string is equal to a rational number of  
a real, so I would not attach any semantic significance to these  
answers.

Andrzej Kozlowski






  • Prev by Date: Re: Select and Cases Give Different Answers
  • Next by Date: Re: Select and Cases Give Different Answers
  • Previous by thread: Select and Cases Give Different Answers
  • Next by thread: Re: Select and Cases Give Different Answers