-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathlogsum.m
More file actions
29 lines (25 loc) · 700 Bytes
/
logsum.m
File metadata and controls
29 lines (25 loc) · 700 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
%
% The original code was downloaed from
% Ruslan Salakhutdinov\s hompeage:
% http://http://www.mit.edu/~rsalakhu/
%
function ls = logsum(xx,dim)
% ls = logsum(x,dim)
%
% returns the log of sum of logs
% computes ls = log(sum(exp(x),dim))
% but in a way that tries to avoid underflow/overflow
%
% basic idea: shift before exp and reshift back
% log(sum(exp(x))) = alpha + log(sum(exp(x-alpha)));
%
% This program was originally written by Sam Roweis
if(length(xx(:))==1) ls=xx; return; end
xdims=size(xx);
if(nargin<2)
dim=find(xdims>1);
end
alpha = max(xx,[],dim)-log(realmax)/2;
repdims=ones(size(xdims));
repdims(dim)=xdims(dim);
ls = alpha+log(sum(exp(xx-repmat(alpha,repdims)),dim));