]> gitweb.michael.orlitzky.com - octave.git/commitdiff
Add is_positive_definite() and its tests.
authorMichael Orlitzky <michael@orlitzky.com>
Fri, 1 Mar 2013 16:41:13 +0000 (11:41 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Fri, 1 Mar 2013 16:41:13 +0000 (11:41 -0500)
is_positive_definite.m [new file with mode: 0644]
tests/is_positive_definite_tests.m [new file with mode: 0644]

diff --git a/is_positive_definite.m b/is_positive_definite.m
new file mode 100644 (file)
index 0000000..61a250d
--- /dev/null
@@ -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 (file)
index 0000000..ebfc385
--- /dev/null
@@ -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));