I need to compute the np.sum(np.exp(L))
where L is a long list of numbers which can be as high as 3000. np.float64 will overflow around exp(700). However, np.logaddexp
is immune to overflow but only works for two elements.
I wrote the following recursive function to compute it for a list.
import numpy as np
from smartprint import smartprint as sprint
def logsum(l):
if len(l) == 1:
return l[0]
return logsum([np.logaddexp(l[0], l[1])] + l[2:])
# odd length
l = [-10, 1, 2, 45, 100]
assert (logsum(l) == np.log(np.sum(np.exp(l))))
sprint (logsum(l))
# even length
l = [-10, 1, 2, 43]
assert (logsum(l) == np.log(np.sum(np.exp(l))))
sprint (logsum(l))
Output:
logsum(l) : 100.0
logsum(l) : 43.0
Additionally,
logsum([1,2,3,4,50000])
outputs 50000
as expected whereas the np.exp
would overflow. Now, the problem is that my list L is huge and has upto 10 million elements and I need to compute the logsum at least 1000 times. So, I wonder if there is a way to vectorize using the np.logaddexp
so that I can use it for a longer list instead of just two elements.