Question

I have to programmatically determine the value of the expression:

 S = log(x1y1 + x2y2 + x3y3 ...) 

Using only the values of:

lxi = log(xi)
lyi = log(yi)

Calculating anti-logs of each of lxi and lyi would probably be impractical and is not desired ...

Is there any way this evaluation can be broken down into a simple summation?

EDIT

I saw a C function somewhere that does the computation in a simple summation:

double log_add(double lx, double ly)
{
   double temp,diff,z;

   if (lx<ly) {
      temp = lx; lx = ly; ly = temp;
   }
   diff = ly-lx;
   z = exp(diff);
   return lx+log(1.0+z);
}

The return values are added for each pair of values, and this seems to be giving the correct answer. But I'm not able to figure out how and why it's working!

Was it helpful?

Solution

The direct way is to perform two exponentiations:

ln(x+y) = ln(eln(x) + eln(y))

The log_add function uses a slightly different approach to get the same result with only one:

ln(x+y) = ln((x+y)x/x)
= ln((x+y)/x) + ln(x)
= ln(1 + y/x) + ln(x)
= ln(1 + eln(y/x)) + ln(x)
= ln(1 + eln(y)-ln(x)) + ln(x)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top