From: Michael Orlitzky Date: Mon, 25 Feb 2013 15:09:03 +0000 (-0500) Subject: Add envelope() and its tests. X-Git-Url: http://gitweb.michael.orlitzky.com/?p=octave.git;a=commitdiff_plain;h=8ca30867e869231f85848dc14e15f11e6d9c732b Add envelope() and its tests. --- diff --git a/envelope.m b/envelope.m new file mode 100644 index 0000000..e54f818 --- /dev/null +++ b/envelope.m @@ -0,0 +1,38 @@ +function envelope = envelope(A) + ## Compute the envelope of the matrix ``A``. The envelope of a matrix + ## is defined as the set of indices, + ## + ## E = { (i,j) : i < j, A(k,j) != 0 for some k <= i } + ## + if (!issymmetric(A) && !is_upper_triangular(A)) + ## The envelope of a matrix is only defined for U-T or symmetric + ## matrices. + envelope = {NA}; + return; + end + + ## Start with an empty result, and append to it as we find + ## satisfactory indices. + envelope = {}; + + for j = [ 1 : columns(A) ] + ## Everything below the first non-zero element in a column will be + ## part of the envelope. Since we're moving from top to bottom, we + ## can simply set a flag indicating that we've found the first + ## non-zero element. Thereafter, everything we encounter should be + ## added to the envelope. + found_nonzero = false; + + for i = [ 1 : j-1 ] + if (A(i,j) != 0) + found_nonzero = true; + end + + if (found_nonzero) + envelope{end+1} = [i,j]; + end + end + + end + +end diff --git a/tests/envelope_tests.m b/tests/envelope_tests.m new file mode 100644 index 0000000..88f5b4f --- /dev/null +++ b/tests/envelope_tests.m @@ -0,0 +1,11 @@ +A = [2, 1, 1, 0; ... + 1, 2, 0, 1; ... + 1, 0, 2, 0; ... + 0, 1, 0, 1]; + +expected = { [1,2], [1,3], [2,3], [2,4], [3,4] }; +actual = envelope(A); + +unit_test_equals("envelope works on an example", ... + true, ... + isequal(actual, expected));