Add advection matrix_sparse() and a test for it.
authorMichael Orlitzky <michael@orlitzky.com>
Thu, 7 Feb 2013 00:38:31 +0000 (19:38 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Thu, 7 Feb 2013 00:38:31 +0000 (19:38 -0500)
advection_matrix_sparse.m [new file with mode: 0644]
tests/advection_matrix_sparse_tests.m [new file with mode: 0644]

diff --git a/advection_matrix_sparse.m b/advection_matrix_sparse.m
new file mode 100644 (file)
index 0000000..ce45e15
--- /dev/null
@@ -0,0 +1,29 @@
+function S = advection_matrix_sparse(integerN)
+  ##
+  ## Sparse version of the advection_matrix function. See
+  ## advection_matrix.m for details.
+  ##
+
+  if (integerN < 2)
+    S = NA;
+    return
+  end
+
+  ## The ones directly above the diagonal.
+  top = [ [zeros(integerN-1, 1), speye(integerN-1)]; ...
+          zeros(1, integerN)];
+
+  ## The ones directly below the diagonal.
+  bottom = [ [zeros(1, integerN-1); ...
+            -speye(integerN-1)    ], zeros(integerN, 1)];
+
+  ## Combine the top and bottom.
+  S = top + bottom;
+
+  ## Fill in the entries in the corner.
+  S(1, integerN) = -1;
+  S(integerN, 1) = 1;
+
+  ## And divide the whole thing by 2.
+  S = (1/2)*S;
+end
diff --git a/tests/advection_matrix_sparse_tests.m b/tests/advection_matrix_sparse_tests.m
new file mode 100644 (file)
index 0000000..1880dee
--- /dev/null
@@ -0,0 +1,16 @@
+result = true;
+
+for n = [3:100]
+  ## Check the sparse matrix against the full one.
+  expected = advection_matrix(n,0,1);
+  actual = advection_matrix_sparse(n);
+
+  if (expected != actual)
+     result = false;
+  end
+end
+
+
+unit_test_equals("Sparse advection matrices agree with full ones", ...
+                true, ...
+                result);