Home > manopt > manifolds > rotations > randsym.m

# randsym

## PURPOSE

Generates random symmetric matrices with normal entries.

## SYNOPSIS

function S = randsym(n, N)

## DESCRIPTION

``` Generates random symmetric matrices with normal entries.

function S = randsym(n)
function S = randsym(n, N)

S is an n-by-n-by-N array where each slice S(:, :, i) for i = 1..N is a
random symmetric matrix with upper triangular entries distributed
independently following a normal distribution (Gaussian, zero mean, unit
variance).

By default, N = 1.

## CROSS-REFERENCE INFORMATION

This function calls:
• multitransp Transpose the matrix slices of an N-D array (no complex conjugate)
This function is called by:
• randherm Generates random Hermitian matrices with normal entries.
• randskewh Generates random skew Hermitian matrices with normal entries.

## SOURCE CODE

```0001 function S = randsym(n, N)
0002 % Generates random symmetric matrices with normal entries.
0003 %
0004 % function S = randsym(n)
0005 % function S = randsym(n, N)
0006 %
0007 % S is an n-by-n-by-N array where each slice S(:, :, i) for i = 1..N is a
0008 % random symmetric matrix with upper triangular entries distributed
0009 % independently following a normal distribution (Gaussian, zero mean, unit
0010 % variance).
0011 %
0012 % By default, N = 1.
0013 %
0014 % See also: randrot randskew randherm randskewh
0015
0016 % This file is part of Manopt: www.manopt.org.
0017 % Original author: Nicolas Boumal, Oct. 23, 2018.
0018 % Contributors:
0019 % Change log:
0020 %       Oct. 23, 2018 (NB):
0021 %           This is not technically necessary for the rotations factory,
0022 %           but it is counter-intuitive to have access to a function called
0023 %           randskew yet not have one for randsym.
0024 %       June 19, 2019 (NB):
0025 %           Now handles the case n = 1 properly.
0026
0027     if nargin < 2
0028         N = 1;
0029     end
0030
0031     if n == 1
0032         S = randn(1, 1, N);
0033         return;
0034     end
0035
0036     % Subindices of the (strictly) upper triangular entries of an n-by-n
0037     % matrix.
0038     [I, J] = find(triu(ones(n), 1));
0039
0040     K = repmat(1:N, n*(n-1)/2, 1);
0041
0042     % Indices of the strictly upper triangular entries of all N slices of
0043     % an n-by-n-by-N array.
0044     L = sub2ind([n n N], repmat(I, N, 1), repmat(J, N, 1), K(:));
0045
0046     % Allocate memory for N random symmetric matrices of size n-by-n and
0047     % populate each upper triangular entry with a random number following a
0048     % normal distribution and copy them on the corresponding lower
0049     % triangular side.
0050     S = zeros(n, n, N);
0051     S(L) = randn(size(L));
0052     S = S + multitransp(S);
0053
0054     % Now populate the diagonal entries:
0055
0056     % Subindices of the diagonal entries of an n-by-n matrix.
0057     [I, J] = find(eye(n));
0058
0059     K = repmat(1:N, n, 1);
0060
0061     % Indices of the diagonal entries of all N slices of an n-by-n-by-N
0062     % array.
0063     L = sub2ind([n n N], repmat(I, N, 1), repmat(J, N, 1), K(:));
0064
0065     S(L) = randn(size(L));
0066
0067 end```

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