about summary refs log tree commit diff
path: root/sourcecodes/bnt-master/KPMtools/logsumexp.m
blob: b639cbb29ae11f07ebe326fc5d39b8e82467e8be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function s = logsumexp(a, dim)
% Returns log(sum(exp(a),dim)) while avoiding numerical underflow.
% Default is dim = 1 (rows) or dim=2 for a row vector
% logsumexp(a, 2) will sum across columns instead of rows

% Written by Tom Minka, modified by Kevin Murphy

if nargin < 2
  dim = 1;
  if ndims(a) <= 2 & size(a,1)==1
    dim = 2;
  end
end

% subtract the largest in each column
[y, i] = max(a,[],dim);
dims = ones(1,ndims(a));
dims(dim) = size(a,dim);
a = a - repmat(y, dims);
s = y + log(sum(exp(a),dim));
%i = find(~finite(y));
%if ~isempty(i)
%  s(i) = y(i);
%end