view general/numerical/matrix/logabsdet.m @ 16:db7f4afd27c5

Rearranging numerical toolbox.
author samer
date Thu, 17 Jan 2013 13:20:44 +0000
parents general/numerical/logabsdet.m@e44f49929e56
children
line wrap: on
line source
function y = logabsdet(A)
% logabsdet - logabsdet(X)=log(abs(det(X))) 
%
% logabsdet :: [[N,N]] -> nonneg.
%
% This is faster and more stable than using log(det(A)).
% (Except when A is not positive definite, in which case
% we fall back to computing det(A) the usual way.)

%  From Tom Minka's lightspeed toolbox
%  Samer: added clause incase A not pos def.

% this only works for HERMITIAN POS DEF matrix.
% y = 2*sum(log(diag(chol(A))));

[R,p]=chol(A*A');
if p>0,
	fprintf('*** logabsdet warning: matrix is singular');
	y=-inf;
else
	y = sum(log(full(diag(R))));
end