Generates uniformly random unitary matrices. function U = randunitary(n, N) U is a n-by-n-by-N array such that each slice U(:, :, i) is a random unitary matrix of size n (i.e., a matrix in the unitary group U(n)), sampled from the Haar measure (uniform distribution). By default, N = 1. Complexity: N times O(n^3). For details on the algorithm, see Mezzadri 2007, "How to generate random matrices from the classical compact groups." See also: randrot qr_unique
0001 function U = randunitary(n, N) 0002 % Generates uniformly random unitary matrices. 0003 % 0004 % function U = randunitary(n, N) 0005 % 0006 % U is a n-by-n-by-N array such that each slice U(:, :, i) is a random 0007 % unitary matrix of size n (i.e., a matrix in the unitary group U(n)), 0008 % sampled from the Haar measure (uniform distribution). 0009 % 0010 % By default, N = 1. 0011 % 0012 % Complexity: N times O(n^3). 0013 % For details on the algorithm, see Mezzadri 2007, 0014 % "How to generate random matrices from the classical compact groups." 0015 % 0016 % See also: randrot qr_unique 0017 0018 % This file is part of Manopt: www.manopt.org. 0019 % Original author: Nicolas Boumal, June 18, 2019. 0020 % Contributors: 0021 % Change log: 0022 0023 if nargin < 2 0024 N = 1; 0025 end 0026 0027 if n == 1 0028 U = sign(randn(1, 1, N) + 1i*randn(1, 1, N)); 0029 return; 0030 end 0031 0032 % Generated as such, the slides of U are uniformly distributed over 0033 % U(n), the set of unitary matrices: see Mezzadri 2007, p597. 0034 U = qr_unique(randn(n, n, N) + 1i*randn(n, n, N)); 0035 0036 end