From: Michael Orlitzky Date: Sun, 3 Mar 2013 15:36:41 +0000 (-0500) Subject: Add tridiagonal() and its tests. X-Git-Url: https://gitweb.michael.orlitzky.com/?a=commitdiff_plain;h=b49076a1d3f7f9d7830a4da5d9958636b9e5d903;p=octave.git Add tridiagonal() and its tests. --- diff --git a/tests/tridiagonal_tests.m b/tests/tridiagonal_tests.m new file mode 100644 index 0000000..b93e36b --- /dev/null +++ b/tests/tridiagonal_tests.m @@ -0,0 +1,26 @@ +A = tridiagonal(0, -1, 2, -1); +expected = []; +unit_test_equals("poisson tridiagonal of size zero is correct", ... + expected, ... + A); + +A = tridiagonal(1, -1, 2, -1); +expected = [2]; +unit_test_equals("poisson tridiagonal of size one is correct", ... + expected, ... + A); + +A = tridiagonal(2, -1, 2, -1); +expected = [2, -1; ... + -1, 2]; +unit_test_equals("poisson tridiagonal of size two is correct", ... + expected, ... + A); + +A = tridiagonal(3, -1, 2, -1); +expected = [2, -1, 0; ... + -1, 2, -1; + 0, -1, 2]; +unit_test_equals("poisson tridiagonal of size three is correct", ... + expected, ... + A); diff --git a/tridiagonal.m b/tridiagonal.m new file mode 100644 index 0000000..ba99b63 --- /dev/null +++ b/tridiagonal.m @@ -0,0 +1,25 @@ +function A = tridiagonal(integerN, x, y, z) + % + % Construct the integerN x integerN (square) tridiagonal matrix + % diag(x,y,z). + % + % INPUT: + % + % - ``integerN`` -- The dimensions of the resulting matrix. + % + % - ``x`` -- The value on the subdiagonal. + % + % - ``y`` -- The value on the diagonal. + % + % - ``z`` -- The value on the superdiagonal. + % + if (integerN < 0) + A = NA; + return; + end + + sub_diag = repmat(x, 1, integerN-1); + on_diag = repmat(y, 1, integerN); + super_diag = repmat(z, 1, integerN-1); + A = diag(on_diag) + diag(super_diag, 1) + diag(sub_diag, -1); +end