Is there a way to generate real random orthogonal matrices in mathematica? If Q is an orthogonal matrix, it's properties are
2 Answers
$\begingroup$
$\endgroup$
7
RandomVariate[CircularRealMatrixDistribution[n]]
should do the trick for $n \times n$ matrices. Very obscure name IMHO.
-
$\begingroup$ Can I fix the random matrix once produced $\endgroup$– JasmineCommented Apr 9, 2021 at 9:18
-
$\begingroup$ Not sure what you mean. Of course, you can store the result in a variable:
A = RandomVariate[CircularRealMatrixDistribution[n]]
and useA
from then on. If you want to have reproducible pseudorandom matrices, you can useSeedRandom
to set a seed for the random number generator. $\endgroup$ Commented Apr 9, 2021 at 9:22 -
$\begingroup$ What I meant is seedrandom. Do I just add seed before random $\endgroup$– JasmineCommented Apr 9, 2021 at 9:38
-
1$\begingroup$ "Do I just add seed before random " Yes. See also the documentation. $\endgroup$ Commented Apr 9, 2021 at 9:39
-
$\begingroup$ I would like to know if there is any way to rescale all the elements of the resulting matrix so that they like in a given interval, say $[-r,r]$ as specified by the user. I realised that the
CircularRealMatrixDistribution
ensures that the elements are in the interval $[-1,1]$ but I wanted the same for a general interval. $\endgroup$ Commented Nov 17, 2022 at 9:37
$\begingroup$
$\endgroup$
6
Here is a pedestrian way of getting a real orthogonal random matrix:
The eigenvalues of an orthogonal matrix are +1 or -1. Therefore, we may create a diagonal matrix with +1 or -1 on the diagonal and the rotate this matrix by a random rotation:
n = 3;
mat0 = DiagonalMatrix[RandomChoice[{-1, 1}, n]];
rot = RotationMatrix[RandomReal[{-1, 1}, {2, n}]];
mat1 = Transpose[rot].mat0.rot;
OrthogonalMatrixQ[mat1]
(* True *)
-
$\begingroup$ Are you sure you're sampling from a uniform distribution in orthogonal-matrix-space here? $\endgroup$– RomanCommented Apr 9, 2021 at 9:11
-
-
3$\begingroup$ My objection is that if I execute your recipe many times and plot the spatial distribution of the eigenvectors of the resulting matrices, I do not get a uniform distribution. $\endgroup$– RomanCommented Apr 9, 2021 at 9:17
-
$\begingroup$ Interestingly that is true. Do you have an explanation? $\endgroup$ Commented Apr 9, 2021 at 9:44
-
$\begingroup$ (1) you're pulling the 3D coordinates from a cube instead of a sphere; should be
RandomVariate[NormalDistribution[], {2, n}]
instead ofRandomReal[{-1, 1}, {2, n}]
. (2) I don't thinkRotationMatrix
does the right thing here. $\endgroup$– RomanCommented Apr 9, 2021 at 13:46
Orthogonalize
. $\endgroup$