]> gitweb.michael.orlitzky.com - octave.git/blobdiff - tridiagonal.m
Add tridiagonal() and its tests.
[octave.git] / tridiagonal.m
diff --git a/tridiagonal.m b/tridiagonal.m
new file mode 100644 (file)
index 0000000..ba99b63
--- /dev/null
@@ -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