R/logsumexp.R

Defines functions logsumexp

logsumexp = function(x,dimension=c(1,2)){
# % Compute log(sum(exp(x),dim)) while avoiding numerical underflow.
# %   By default dim = 2 (columns).
	if (is.null(dimension)) dimension=1 ;
# % subtract the largest in each column
	y = apply(x,1,max)
	x = x-y#sweep(x,1,y,"-")
	s = y+log(rowSums(exp(x)))
	i = is.infinite(y)
	if (sum(i) > 0) s[i]=y[i]
	return(s)
}

Try the xLLiM package in your browser

Any scripts or data that you put into this service are public.

xLLiM documentation built on Nov. 2, 2023, 5:17 p.m.