]> gitweb.michael.orlitzky.com - octave.git/commitdiff
Add permutation_matrices() and its tests.
authorMichael Orlitzky <michael@orlitzky.com>
Mon, 25 Feb 2013 15:24:40 +0000 (10:24 -0500)
committerMichael Orlitzky <michael@orlitzky.com>
Mon, 25 Feb 2013 15:24:40 +0000 (10:24 -0500)
permutation_matrices.m [new file with mode: 0644]
tests/permutation_matrices_tests.m [new file with mode: 0644]

diff --git a/permutation_matrices.m b/permutation_matrices.m
new file mode 100644 (file)
index 0000000..9032d2e
--- /dev/null
@@ -0,0 +1,31 @@
+function PMs = permutation_matrices(integerN)
+  ## Generate all permutation matrices of size ``integerN``.
+  ##
+  ## INPUT:
+  ##
+  ##   - ``integerN`` -- The dimension of the resulting matrices.
+  ##
+  ## OUTPUT:
+  ##
+  ##   - ``PMs`` -- A cell array of permutation matrices.
+  ##
+
+  if (integerN < 1)
+    PMs = NA;
+    return;
+  end
+
+  ## Append to this as we generate them.
+  PMs = {};
+
+  ## Generate all permutations of [1,2,...,integerN].
+  permutations = perms([1:integerN]);
+
+  for idx = [ 1 : factorial(integerN) ]
+    sigma = permutations(idx,:);
+    ## Create a permutation matrix from the permutation, sigma.
+    P = eye(integerN) (sigma,:);
+    PMs{end+1} = P;
+  end
+
+end
diff --git a/tests/permutation_matrices_tests.m b/tests/permutation_matrices_tests.m
new file mode 100644 (file)
index 0000000..d65049e
--- /dev/null
@@ -0,0 +1,6 @@
+expected = { [1] };
+actual = permutation_matrices(1);
+
+unit_test_equals("permutation_matrices(1) works", ...
+                true, ...
+                isequal(actual,expected));