# Efficient Implementation of the Log Sum Exp Log trick for Preventing Underflow erros

So I'm constructing a Gibbs sampler for a moderately sized graphical model, and I started encountering underflow errors (the computed relative frequencies were all very small). To get around this, I've started using the LOG-SUM-EXP-LOG trick found here:

https://math.stackexchange.com/questions/648514/preventing-underflow-log-sum-exp-trick

Now however I have to compute a large number of logs and exponentials. Does anyone know how to do this efficiently in C/C++? I'm considering making a look-up table, but that's a lot of memory...

• What is your question? To do logs in C/C++, you call log or log10, to do exponentials you call exp. What specifically is the problem you are encountering in "computing a large number of logs and exponentials"? Apr 23 '15 at 15:17
• @Wolfgang Thanks for replying! It's not a problem computing a small number or even $N^2$ in the data size (I'm working on a network problem), but since I have to iterate many times for the sampler to be effective I will need to do this many times for each data point. (at least several times more than $N^2 * Number of Iterations$). I know that someone must be doing this on a regular basis, so I was hoping they'd have a trick or two to share. I found this though, which speeds things up a bit.
– Dan
Apr 24 '15 at 17:57