MathGroup Archive 2006

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

Search the Archive

Re: Or in a Select question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg67107] Re: [mg67066] Or in a Select question
  • From: ggroup at sarj.ca
  • Date: Fri, 9 Jun 2006 01:09:07 -0400 (EDT)
  • References: <200606080854.EAA12393@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

You are missing a basic identity in logic:

Assume A & B are True/False statements, then

!A || !B  == !( A && B )
!A && !B  == !( A || B )

As far as I can tell, the logic you've used in the first example is:
First Pair don't match OR Second Pair don't match

Symbolically,
A = First Pair match
B = Second Pair match
!A || !B

What you want is:
First Pair don't match AND Second Pair don't match

Symbolically,
A = First Pair match
B = Second Pair Match
!A && !B

Depending on how you like to think about these things, it may be more obvious 
to write the equivalent of the last example:
Neither the first nor the second pair match

Symbolically
A = First pair match
B = Second pair match
!(A||B)

Hope that helps!


On Thu, 8 Jun 2006 04:54:07 -0400 (EDT), János wrote
> Hi,
> 
> I have a list
> 
> lst={a,b,c}
> 
> I make another list from it the following way:
> 
> In[2]:=
> pp = Partition[Tuples[lst,
>      2], 2, 1]
> Out[2]=
> {{{a, a}, {a, b}},
>    {{a, b}, {a, c}},
>    {{a, c}, {b, a}},
>    {{b, a}, {b, b}},
>    {{b, b}, {b, c}},
>    {{b, c}, {c, a}},
>    {{c, a}, {c, b}},
>    {{c, b}, {c, c}}}
> 
>  From here I would like to select all the elements whose sublists  
> contain only different elements.  So my "logical" selection was:
> 
> In[54]:=
> Select[pp,
>    #1[[1,1]] =!= #1[[1,2]] ||
>      #1[[2,1]] =!= #1[[2,
>        2]] & ]
> Out[54]=
> {{{a, a}, {a, b}},
>    {{a, b}, {a, c}},
>    {{a, c}, {b, a}},
>    {{b, a}, {b, b}},
>    {{b, b}, {b, c}},
>    {{b, c}, {c, a}},
>    {{c, a}, {c, b}},
>    {{c, b}, {c, c}}}
> 
> Well, that did not do any damage to the list.  After some time I came  
> up with this one:
> 
> In[49]:=
> Complement[pp, Select[pp,
>     Xor[#1[[1,1]] =!=
>        #1[[1,2]],
>       #1[[2,1]] =!= #1[[2,
>         2]]] & ]]
> Out[49]=
> {{{a, b}, {a, c}},
>    {{a, c}, {b, a}},
>    {{b, c}, {c, a}},
>    {{c, a}, {c, b}}}
> 
> That looks OK, but also looks too complicated.  Why my "logical" one  
> does not work here?
> 
> Interestingly if I just use either the left or right side of the Or,  
> that partial select is working.  For example:
> 
> In[65]:=
> Select[pp, #1[[1,1]] =!=
>      #1[[1,2]] & ]
> Out[65]=
> {{{a, b}, {a, c}},
>    {{a, c}, {b, a}},
>    {{b, a}, {b, b}},
>    {{b, c}, {c, a}},
>    {{c, a}, {c, b}},
>    {{c, b}, {c, c}}}
> 
> Now if I try with Cases and conditional pattern matching then the  
> selection for sublists with identical elements works:
> 
> In[98]:=
> Cases[pp, {u_, v_} /;
>     u[[1]] === u[[2]] ||
>      v[[1]] === v[[2]]]
> Out[98]=
> {{{a, a}, {a, b}},
>    {{b, a}, {b, b}},
>    {{b, b}, {b, c}},
>    {{c, b}, {c, c}}}
> 
> If I change here the === to =!=, then I do not get again that I expect:
> 
> In[103]:=
> Cases[pp,
>    (({u_, v_} /; u[[1]]) =!=
>       u[[2]] || v[[1]]) =!=
>     v[[2]]]
>  From In[103]:=
> \!\(\*
>    RowBox[{\(Part::"partd"\),
>      ":", "\<\"Part specification \\!\\(u \[LeftDoubleBracket] 2 \
> \[RightDoubleBracket]\\) is longer than depth of object. \
> \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", ButtonStyle->\\ 
> \"RefGuideLinkText\
> \\\", ButtonFrame->None, ButtonData:>\\\"General::partd\\\"]\\)
> \"\>"}]\) From In[103]:= \!\(\*   RowBox[{\(Part::"partd"\),     ":",
>  "\<\"Part specification \\!\\(v \[LeftDoubleBracket] 2 \ 
> \[RightDoubleBracket]\\) is longer than depth of object. \ 
> \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", ButtonStyle->\\ 
\"RefGuideLinkText\
> \\\", ButtonFrame->None, ButtonData:>\\\"General::partd\\\"]\\)
> \"\>"}]\) Out[103]= {} From In[104]:= Part::"partd":"Part 
> specification \!\(u \[LeftDoubleBracket] 2 \ \[RightDoubleBracket]\) 
> is longer than depth of object. \ \!\(\*ButtonBox[\"More\[Ellipsis]\", 
> ButtonStyle->\"RefGuideLinkText \", \ ButtonFrame->None, 
> ButtonData:>\"General::partd\"]\)" From In[104]:= Part::"partd":"Part 
> specification \!\(v \[LeftDoubleBracket] 2 \ \[RightDoubleBracket]\) 
> is longer than depth of object. \ \!\(\*ButtonBox[\"More\[Ellipsis]\", 
> ButtonStyle->\"RefGuideLinkText \", \ ButtonFrame->None, 
> ButtonData:>\"General::partd\"]\)"
> 
> if I change =!= to only != then I still do not get that I expect:
> 
> In[108]:=
> Cases[pp, {u_, v_} /;
>     u[[1]] != u[[2]] ||
>      v[[1]] != v[[2]]]
> Out[108]=
> {}
> 
> Obviously I am not GETting something here :)
> 
> Thanks ahead,
> 
> János
> P.S.  It is 5.1 on OSX 10.4.6.  I know that Or evaluates in a non- 
> traditional way and looked the Appendix - that is how I ended up with  
> Xor.
> 
> ----------------------------------------------
> Trying to argue with a politician is like lifting up the head of a  
> corpse.
> (S. Lem: His Master Voice)





  • Prev by Date: Re: Or in a Select question
  • Next by Date: Re: Or in a Select question
  • Previous by thread: Re: Or in a Select question
  • Next by thread: Re: Or in a Select question