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

MathGroup Archive 2008

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

Search the Archive

Re: Tally

  • To: mathgroup at smc.vnet.net
  • Subject: [mg86937] Re: Tally
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Wed, 26 Mar 2008 04:56:59 -0500 (EST)
  • Organization: University of Bergen
  • References: <fsa5eq$adg$1@smc.vnet.net>

Armen Kocharyan wrote:
> Dear Group,
> 
> I'm trying the following:
> 
> 
> *Dlist = {{{0,1},{0,1}},{{1,0},{1,0}},{{1,0},{0,1}},{{0,1},{1,0}}};*
> 
> *Tally[Dlist,(#1=== #2) \[Or] *
> 
> *            (#1[[1]][[1]]=== #2[[1]][[2]] \[And] *
> 
> *             #1[[1]][[2]]=== #2[[1]][[1]] \[And] *
> 
> *             #1[[2]][[1]]=== #2[[2]][[2]] \[And] *
> 
> *             #1[[2]][[2]]=== #2[[2]][[1]] )&]*
> 
> 
> 
> The output is:
> 
> *{{{{0,1},{0,1}},1},{{{1,0},{1,0}},1},{{{1,0},{0,1}},2}}*
> 
> instead of
> 
> *{{{{0,1},{0,1}},2},{{{1,0},{0,1}},2}}*
> 
> 
> 
> If I remove the last member from DList
> 
> *Dlist = {{{0,1},{0,1}},{{1,0},{1,0}},{{1,0},{0,1}}};*
> 
> then I got a correct answer
> 
> *{{{{0,1},{0,1}},2},{{{1,0},{0,1}},1}}.*
> 
> 
> 
> Is anything wrong with my original code?

I really *hope* that I am wrong, but it seems to me that your code is 
correct.  The test function is a "good" equivalence relation, i.e. it is 
reflexive, symmetric and transitive, so this cannot be the problem.

But something seems to be wrong with Tally:

test =
      #1 === #2 ||
     (#1[[1, 1]] === #2[[1, 2]] &&
      #1[[1, 2]] === #2[[1, 1]] &&
      #1[[2, 1]] === #2[[2, 2]] &&
      #1[[2, 2]] === #2[[2, 1]]) &

dlist = {{{0, 1}, {0, 1}}, {{1, 0}, {1, 0}}, {{1, 0}, {0, 1}},
          {{0, 1}, {1, 0}}}

test2[p_, q_] :=
  With[{val = test[p, q]},
   Print[{Position[dlist, p, {1}], Position[dlist, q, {1}], val}]; val]

( Using Position should work because this specific dlist has no 
repeating elements )

The output of Tally[dlist, test2] is:

{{{1}},{{4}},False}
{{{3}},{{2}},False}
{{{4}},{{2}},False}
{{{4}},{{3}},True}
{{{1}},{{3}},False}
{{{2}},{{4}},False}
{{{4}},{{3}},True}
{{{3}},{{1}},False}

{{{{0, 1}, {0, 1}}, 1}, {{{1, 0}, {1, 0}}, 1},
  {{{1, 0}, {0, 1}}, 2}}

It looks like Tally never tests element 1 against element 2, but it does 
test element 4 against element 3 *twice*, even though this is completely 
unnecessary.


This is the kind of bug that severely undermines one's trust in 
Mathematica.  I do not expect that Integrate or some numerical method 
should always return a correct answer.  I know that this is simply not 
possible.  But the algorithms that could be used by Tally are simple 
enough that I find this bug quite shocking.


  • Prev by Date: Re: Tagged list processing
  • Next by Date: Re: Importing text files
  • Previous by thread: Re: Tally
  • Next by thread: Re: Tally