Home > manopt > manifolds > rotations > randrot.m

# randrot

## PURPOSE Generates uniformly random rotation matrices.

## SYNOPSIS function R = randrot(n, N)

## DESCRIPTION ``` Generates uniformly random rotation matrices.

function R = randrot(n, N)

R is a n-by-n-by-N matrix such that each slice R(:, :, i) is an
orthogonal matrix of size n of determinant +1 (i.e., a matrix in SO(n)).
By default, N = 1.
Complexity: N times O(n^3).
Theory in Diaconis and Shahshahani 1987 for the uniformity on O(n);
With details in Mezzadri 2007,
"How to generate random matrices from the classical compact groups."
To ensure matrices in SO(n), we permute the two first columns when
the determinant is -1.

## CROSS-REFERENCE INFORMATION This function calls:
This function is called by:
• essential_svd Sample solution of an optimization problem on the essential manifold.
• generalized_procrustes Rotationally align clouds of points (generalized Procrustes problem)
• essentialfactory Manifold structure to optimize over the space of essential matrices.
• rotationsfactory Returns a manifold structure to optimize over rotation matrices.

## SOURCE CODE ```0001 function R = randrot(n, N)
0002 % Generates uniformly random rotation matrices.
0003 %
0004 % function R = randrot(n, N)
0005 %
0006 % R is a n-by-n-by-N matrix such that each slice R(:, :, i) is an
0007 % orthogonal matrix of size n of determinant +1 (i.e., a matrix in SO(n)).
0008 % By default, N = 1.
0009 % Complexity: N times O(n^3).
0010 % Theory in Diaconis and Shahshahani 1987 for the uniformity on O(n);
0011 % With details in Mezzadri 2007,
0012 % "How to generate random matrices from the classical compact groups."
0013 % To ensure matrices in SO(n), we permute the two first columns when
0014 % the determinant is -1.
0015 %
0017
0018 % This file is part of Manopt: www.manopt.org.
0019 % Original author: Nicolas Boumal, Sept. 25, 2012.
0020 % Contributors:
0021 % Change log:
0022
0023     if nargin < 2
0024         N = 1;
0025     end
0026
0027     if n == 1
0028         R = ones(1, 1, N);
0029         return;
0030     end
0031
0032     R = zeros(n, n, N);
0033
0034     for i = 1 : N
0035
0036         % Generated as such, Q is uniformly distributed over O(n), the set
0037         % of orthogonal matrices.
0038         A = randn(n);
0039         [Q, RR] = qr(A);
0040         Q = Q * diag(sign(diag(RR))); %% Mezzadri 2007
0041
0042         % If Q is in O(n) but not in SO(n), we permute the two first
0043         % columns of Q such that det(new Q) = -det(Q), hence the new Q will
0044         % be in SO(n), uniformly distributed.
0045         if det(Q) < 0
0046             Q(:, [1 2]) = Q(:, [2 1]);
0047         end
0048
0049         R(:, :, i) = Q;
0050
0051     end
0052
0053 end```

Generated on Mon 10-Sep-2018 11:48:06 by m2html © 2005