2

I want to count degeneracies in a list called spectrum. The list contains numbers of double (~ 15digits) precision. So it makes no sense if the noise below this precision is taken into account since it falsifies the result of degeneracy counting using Tally[]. So I applied SetPrecision.

degList = Tally[{#1, SetPrecision[#2, 15]} & @@@ spectrum];

But I still get e.g. {-0.047063080371693544, 4} {-0.047063080371693294, 4} (last number is the degeneracy level) which should be seen as equal numbers but are counted by Tally[] as different ones. What am I doing wrong?

pawel_winzig
  • 1,577
  • 1
  • 12
  • 21

1 Answers1

4

Use the second argument of Tally, thus:

Tally[{.00123, .00154}, Abs[#1 - #2] < .01 &]
(*{{0.00123, 2}}*)

Alternatively, look at energy level differences, omegaij=Chop[Differences[spectrum], tol], then Tally[omegaij].

Reply to comment (on what's wrong with the attempt in the question):

First, SetPrecision doesn't chop off digits; it instead sets the precision, see here).

Second, I am not sure what you intended {#1, SetPrecision[#2, 15]} & @@@ spectrum to do, but g@@@l just replaces the heads of everything at level 1 of l by g, for example,

g @@@ {a[2], b[c[d]], {{c}}}
{g[2], g[c[d]], g[{c}]}

If you try g[#1, #2] & @@@ {a, b, c} then nothing happens because the objects at level 1 are atomic (ie for the same reason that g @@ 3 evaluates to 3). If instead you try it on something with non-atomic objects at level 1, then it bombs:

g[#1, #2] & @@@ {a[2], b, c}

Mathematica graphics

Maybe you're looking for Map,

g /@ {1, 2, 3}
(*{g[1], g[2], g[3]}*)

But I am probably missing something; what did you intend this to do?

acl
  • 19,834
  • 3
  • 66
  • 91