]> gitweb.michael.orlitzky.com - octave.git/blobdiff - random_orthogonal_matrix.m
Add the random_orthogonal_matrix() function and its tests.
[octave.git] / random_orthogonal_matrix.m
diff --git a/random_orthogonal_matrix.m b/random_orthogonal_matrix.m
new file mode 100644 (file)
index 0000000..8c7cea5
--- /dev/null
@@ -0,0 +1,25 @@
+function U = random_orthogonal_matrix(integerN)
+  %
+  % Generate a random orthogonal matrix.
+  %
+  % INPUT:
+  %
+  %   - ``integerN`` -- The dimension of the resulting square matrix.
+  %
+  % OUTPUT:
+  %
+  %   - ``U`` -- An orthogonal matrix of dimension integerN.
+  %
+  % REFERENCES:
+  %
+  %   1. G.W. Stewart, Efficient Generation of Random Orthogonal Matrices,
+  %      SIAM J. Numer. Analysis, 1980, pp. 403--409, Section 3.
+  %
+
+  % We begin by computing a random matrix A.
+  A = rand(integerN);
+
+  % The Q-R decomposition of A will give us an orthogonal factor of A.
+  % See the reference for why this doesn't suck.
+  [U, R] = qr(A);
+end