Home > manopt > manifolds > rotations > randunitary.m

# randunitary

## PURPOSE Generates uniformly random unitary matrices.

## SYNOPSIS function U = randunitary(n, N)

## DESCRIPTION ``` 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."

## CROSS-REFERENCE INFORMATION This function calls:
• qr_unique Thin QR factorization ensuring diagonal of R is real, positive if possible.
This function is called by:
• unitaryfactory Returns a manifold structure to optimize over unitary matrices.

## SOURCE CODE ```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 %
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```

Generated on Fri 30-Sep-2022 13:18:25 by m2html © 2005