From: Michael Orlitzky Date: Fri, 1 Mar 2013 16:41:13 +0000 (-0500) Subject: Add is_positive_definite() and its tests. X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=b89ed48b92e5b38b7c8df70c12e4b8cc275628df;p=octave.git Add is_positive_definite() and its tests. --- diff --git a/is_positive_definite.m b/is_positive_definite.m new file mode 100644 index 0000000..61a250d --- /dev/null +++ b/is_positive_definite.m @@ -0,0 +1,13 @@ +function ipd = is_positive_definite(A) + % Returns true if A is positive-definite, and false otherwise. We + % perform a full Cholesky factorization, so this is not fast. + + ipd = false; + + [R,p] = chol(A); + + if (p == 0) + % The error flag is not set. + ipd = true; + end +end diff --git a/tests/is_positive_definite_tests.m b/tests/is_positive_definite_tests.m new file mode 100644 index 0000000..ebfc385 --- /dev/null +++ b/tests/is_positive_definite_tests.m @@ -0,0 +1,14 @@ +A = [1,0;0,1]; +unit_test_equals("The identity is positive definite", ... + true, ... + is_positive_definite(A)); + +A = [1,1;1,1]; +unit_test_equals("All-ones matrix is not positive definite", ... + false, ... + is_positive_definite(A)); + +A = [5,1,2;1,6,3;2,3,7]; +unit_test_equals("A diagonally-dominant matrix is positive definite", ... + true, ... + is_positive_definite(A));