Code covered by the BSD License  

Highlights from
Methods for calculating precise logarithm of a sum and subtraction

Be the first to rate this file! 8 Downloads (last 30 days) File Size: 105.87 KB File ID: #25273

Methods for calculating precise logarithm of a sum and subtraction

by Roland Pihlakas

 

10 Sep 2009 (Updated 07 Dec 2010)

Methods for operating on log's of values without calculating the original values during the process

| Watch this File

File Information
Description

Disclosed are methods to compute the precise value of the logarithm of a sum and the logarithm of a subtraction.

A number of practical problems can result in having too big or small values in intermediate values of a calculation. Then one tries to take logarithm of these values and operate on logarithms instead.

In the case: log (p • q) = log p + log q is very easy to compute, but the problem is then to compute (or approximate) the value of log (a + b) from the value of log a and log b.

Let us assume that log a and log b are known, and that we want to approximate log (a + b).
Most basic solution would be calculating
sum_log = log(exp(a_log) + exp(b_log),
where a_log = log a and b_log = log b and therefore sum_log = log(a + b).
But the method I propose requires calling only one exp() and one log(), instead of two exp() and one log() in the basic solution.
Additionally, the proposed method has the critical advantage of not overflowing in case of large numbers of a and b.

The method is based on the notion that
ln(a + b) = ln{exp[ln(a) - ln(b)] + 1} + ln(b).

In a similar manner we can compute the logarithm of subtraction, based on the notion that
ln(a - b) = ln{exp[ln(a) - ln(b)] - 1} + ln(b), if a > b.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
10 Sep 2009 Darren Rowland

I can see this being a useful utility for some applications. However the code could be improved. I would like to see you add
- a H1 line
- an example (or two) showing usage, particularly comparing your method with a method which will produce overflow
- Comments within the code explaining the procedure so a user does not need to consult the pdf file

Lastly I think the actual computation should be on just one line
i.e. R = log(exp(a_ln - b_ln) + 1) + b_ln;
rather than spread out over many lines. Let comments explain the shifting.

Darren

Please login to add a comment or rating.
Updates
04 Dec 2010

1) Included method for calculating logarithm of subtraction.
And according to the suggestions of Darren Rowland:
2) Added H1 line.
3) Inlined the main formula.
4) An example comparing the methods with a naive solution which will produce overflow.

07 Dec 2010

Seems like the updated zip file did not upload with my previous post. I will now resend it.

Tag Activity for this File
Tag Applied By Date/Time
mathematics Roland Pihlakas 10 Sep 2009 09:46:09
statistics Roland Pihlakas 10 Sep 2009 09:46:09
logarithm Roland Pihlakas 10 Sep 2009 09:46:09
article Roland Pihlakas 10 Sep 2009 09:46:09
paper Roland Pihlakas 08 Dec 2010 13:05:58

Contact us at files@mathworks.com