2

In statistical physics, we often try to find out the partition function which is expressed as

Z=\sum_i e^(-\beta E_i) where \beta is inverse temperature. e^(-\beta E_i), the term under the summation is called the Boltzmann weight.

Now at low temperature, \beta becomes quite large and we face a situation where we have to calculate the exponential of a very large positive or negative number (depending on the sign of E_i).

In normal programming language (e.g. Python), the intrinsic exponential function gives infinity for e^x if x>=1000.

For instance, in Python 3, I tried to estimate in terms of Taylor series expansion:

x = 1000
n = int(input('Enter number of terms in Taylor series\n'))

# Taylor Series expansion up to n-th term
def exponential(n, x):
        sum = 1.0
        for i in range(n, 0, -1):
                sum = 1 + x * sum / i
        return sum


print('e^x =',exponential(n, x))

However, the result varies for n <= 300 and becomes inf for n >= 400.

Can we ever calculate the partition function for large beta (at least in the power of 10)? Could there be some trick?

hbaromega
  • 2,317
  • 2
  • 24
  • 32
  • using `numpy` types will buy you some more space. E.g., `np.exp(x, dtype=np.float128)` will work up to `x > 10000` – Marat Jun 13 '21 at 21:05

1 Answers1

2

One way would be to use the mpmath Python library which can handle arbitrary precision and large numbers.

e.g.

import mpmath as mp
mp.dps =50
print(mp.exp(500))

Result is 1.40359221785284e+217

But I wonder if some analytical approximation is a better approach? For example see this from Physics.SE

https://physics.stackexchange.com/questions/357824/what-happens-to-the-partition-functions-in-the-limit-t-to-0-or-beta-to-infty

paisanco
  • 4,098
  • 6
  • 27
  • 33
  • Thanks, sorry what is `dps`here? Could there be equivalent precision management in Fortran and C? The analytics in the link talks about zero temperature limit whereas I have a small temperature (say, T=0.001) but finite large beta=1000. – hbaromega Jun 14 '21 at 12:00
  • dps is digital precision, you can set this as needed. The documentation for mpmath is pretty good at https://mpmath.org/doc/current/ As to C++ I haven't worked with that in a while but there is an (old) answer https://stackoverflow.com/questions/117429/handling-large-numbers-in-c. Fortran haven't worked with. – paisanco Jun 15 '21 at 01:32