4
$\begingroup$

I'm trying to calculate entropies for an absolutely giant system by counting states, and this means I have to use some obscenely large numbers. I'm running

Nn = 5*10^20
- 1.381 * 10^-23  Log[Multinomial[Nn/3, Nn/3, Nn/3]]

and (of COURSE) it comes out to an obscenely large number. Honestly, I would be alright with just the order of magnitude without having to do each factorial estimation separately by hand... Are there any little tricks to getting around this Overflow[]? I'm sure Mathematica has some workarounds for avoiding machine precision errors.... there's no way this number is eating all my 8 gigs of ram.

For reference, the output of $MaxNumber is 1.605216761933662*10^1355718576299609.

$\endgroup$
4
  • $\begingroup$ LogGamma[1 + Nn] - 3 LogGamma[1 + Nn/3] is quite huge ($\approx 5.5\times 10^{20}$); no wonder you're hitting overflow when it's the exponent. $\endgroup$ – J. M.'s torpor May 12 '16 at 2:25
  • $\begingroup$ Yeah.... Is there any gross hacky way to get it to happily store bigger numbers? $\endgroup$ – laudiacay May 12 '16 at 2:29
  • $\begingroup$ Also, @J.M. it's not the exponent, it's just being multiplied by the Boltzmann constant up front. $\endgroup$ – laudiacay May 12 '16 at 2:33
  • $\begingroup$ I see; in any event, I've already shown you an expression you can use instead of Log[Multinomial[Nn/3, Nn/3, Nn/3]]; LogGamma[] is often the thing to use in situations like yours. $\endgroup$ – J. M.'s torpor May 12 '16 at 2:35
3
$\begingroup$

Since your number is quite large, you can use Stirling's approximation to do this. It's also very common to use this approximation in statistical mechanics: For large number $n$

$$\log(n!)\approx n\log(n)-n$$

So in your case

$$n=\frac{Nn}{3}$$

then

$$ \log\left[\frac{(3n)!}{n!\times n!\times n!}\right]\\ =\log[(3n)!]-3\log[n!]\\ \approx 3n \log(3n)-3n \log(n) $$

So

-1.381*10^-23 Nn*(Log[Nn] - Log[Nn/3])
(*-0.00758592*)
$\endgroup$
1
  • 1
    $\begingroup$ As expected, this is close to the result of -QuantityMagnitude[UnitConvert[Quantity[1, "BoltzmannConstant"]]] (LogGamma[1 + Nn] - 3 LogGamma[1 + Nn/3]) /. Nn -> 5.*^20 $\endgroup$ – J. M.'s torpor May 12 '16 at 2:42
1
$\begingroup$

It looks like there isn't a good way to bypass Overflow[]. However, for very large factorial calculations, it's useful and incredibly accurate to use Stirling or Nemes approximations, depending on the size of the factorial.

@Mathematica devs, an idea- maybe catch overflow errors, tell Factorial to substitute the Stirling or Nemes approximation, then try to make some cancellations with the rest of the calculation before plugging in the factorial argument? This would work SUPER well on Bi/Multinomial calculations, because a bunch of Stirling cancels out.

$\endgroup$
4
  • 1
    $\begingroup$ ...or, work with expressions in terms of LogGamma[] as much as possible, and delay the exponentiation as long as you can. $\endgroup$ – J. M.'s torpor May 12 '16 at 2:30
  • $\begingroup$ That also works. I wonder, do you know how are they calculating Log(Gamma()) well? $\endgroup$ – laudiacay May 12 '16 at 2:34
  • 1
    $\begingroup$ This claims that "recursion, functional equations, and the Binet asymptotic formula" are used for Gamma[]; probably suitable modifications of these are done for LogGamma[]. $\endgroup$ – J. M.'s torpor May 12 '16 at 2:39
  • $\begingroup$ Oh wow, Binet is like tailor-made for taking logs. That's so cool- thanks! $\endgroup$ – laudiacay May 12 '16 at 2:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.