hmmer-3.3/easel/esl_vectorops.c
/* Function:  esl_vec_{DF}LogSum(), esl_vec_{DF}Log2Sum()
 * Synopsis:  Given log p-vector (or log_2), return log (or log_2) of sum of probabilities.
 *
 * Purpose:   <vec> is a log probability vector; return the log of the scalar sum
 *            of the probabilities in <vec>. That is, the <n> elements in <vec>
 *            are log probabilities, but the summation is done in probability
 *            space, by exponentiating each of the <n> values in the vector,
 *            summing, and returning the log of the sum. 
 *            
 *            That is: return $\log \sum_i e^{v_i}$, but done in a numerically
 *            stable way.
 *            
 *            If you need high accuracy, you should sort <vec> from
 *            smallest to largest before calling for the logsum. We
 *            don't do that for you, because we don't want to change
 *            the order of the input <vec>, and nor do we spend an
 *            allocation.
 *
 *            The <Log2> versions do the same, but where the values
 *            are base log_2 (bits).
 *
 */


 is a log probability vector; return the log of the scalar sum
of the probabilities in . That is, the  elements in 
are log probabilities, but the summation is done in probability
space, by exponentiating each of the  values in the vector,
summing, and returning the log of the sum.

That is: return $\log \sum_i e^{v_i}$, but done in a numerically
stable way.

If you need high accuracy, you should sort  from
smallest to largest before calling for the logsum. We
don't do that for you, because we don't want to change
the order of the input , and nor do we spend an
allocation.

The  versions do the same, but where the values
are base log_2 (bits).
is a log probability vector ; return the log of the scalar sum of the probabilities in . That is the elements in are log probabilities but the summation is done in probability space by exponentiating each of the values in the vector summing and returning the log of the sum . That is : return $ \ log \ sum_i e^ { v_i } $ but done in a numerically stable way . If you need high accuracy you should sort from smallest to largest before calling for the logsum . We don ' t do that for you because we don ' t want to change the order of the input and nor do we spend an allocation . The versions do the same but where the values are base log_2 ( bits ) .
double
esl_vec_DLogSum(const double *vec, int n)
{
  double max, sum;
  int    i;
  max = esl_vec_DMax(vec, n);
  if (max == eslINFINITY) return eslINFINITY; 
  sum = 0.0;
  for (i = 0; i < n; i++)
    if (vec[i] > max - 500.)      
      sum += exp(vec[i] - max);
  sum = log(sum) + max;
  return sum;
}
double esl_vec_DLogSum ( const double * vec , int n ) { double max , sum ; int i ; max = esl_vec_DMax ( vec , n ) ; if ( max == eslINFINITY ) return eslINFINITY ; sum = 0.0 ; for ( i = 0 ; i < n ; i ++ ) if ( vec [ i ] > max - 500. ) sum += exp ( vec [ i ] - max ) ; sum = log ( sum ) + max ; return sum ; }
Function:  esl_vec_{DF}LogSum(), esl_vec_{DF}Log2Sum()
 Synopsis:  Given log p-vector (or log_2), return log (or log_2) of sum of probabilities.
Function : esl_vec_ { DF } LogSum () esl_vec_ { DF } Log2Sum () Synopsis : Given log p - vector ( or log_2 ) return log ( or log_2 ) of sum of probabilities .
/* avoid inf-inf below! */ // DBL_EPSILON ~ 2.2e-16; DBL_MIN ~ 2.2e-308; log() = -36, -708