R/log.space.R

Defines functions com.log.factorial com.log.difference com.log.sum

Documented in com.log.difference com.log.factorial com.log.sum

com.log.sum = function(x,y)		# log.sum(x,y) = log( exp(x) + exp(y) )
{
	if (x == -Inf)
		{ return (y); }
	else if (y == -Inf)
		{ return (x); }
	else if (x > y)
		{ return (x + log( 1 + exp(y - x) ) ); }
	else
		{ return (y + log( 1 + exp(x - y) ) ); }
}

com.log.difference = function(x,y)	# log.difference(x,y) = log( exp(x) - exp(y) )
{
	if (x == -Inf)
		{ return (NaN); }
	else if (y == -Inf)
		{ return (x); }
	else if (x > y)
		{ return (x + log( 1 - exp(y - x) ) ); }
	else
		{ return (NaN); }
}


com.log.factorial = function(x)	# log(factorial(x))
{
	if (is.vector(x) && length(x) > 1)
	{
		for (i in 1:length(x))
			x[i] = com.log.factorial(x[i]);
		return (x);
	}
	else if (is.numeric(x))
	{
		if (x == 0) { x = 1; }
		return (sum(log(seq(from = 1, to = x, by = 1))));
	}
	else { stop("x must be a vector or number."); }
}

Try the compoisson package in your browser

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

compoisson documentation built on May 29, 2017, 2:50 p.m.