diff general/numerical/logabsdet.m @ 4:e44f49929e56

Adding reorganised general toolbox, now in several subdirectories.
author samer
date Sat, 12 Jan 2013 19:21:22 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/general/numerical/logabsdet.m	Sat Jan 12 19:21:22 2013 +0000
@@ -0,0 +1,22 @@
+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