8
$\begingroup$

Is there a way to generate real random orthogonal matrices in mathematica? If Q is an orthogonal matrix, it's properties are

dd

$\endgroup$
1

2 Answers 2

15
$\begingroup$

RandomVariate[CircularRealMatrixDistribution[n]] should do the trick for $n \times n$ matrices. Very obscure name IMHO.

$\endgroup$
7
  • $\begingroup$ Can I fix the random matrix once produced $\endgroup$
    – Jasmine
    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 use A from then on. If you want to have reproducible pseudorandom matrices, you can use SeedRandom to set a seed for the random number generator. $\endgroup$ Apr 9, 2021 at 9:22
  • $\begingroup$ What I meant is seedrandom. Do I just add seed before random $\endgroup$
    – Jasmine
    Apr 9, 2021 at 9:38
  • 1
    $\begingroup$ "Do I just add seed before random " Yes. See also the documentation. $\endgroup$ 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$
    – Erosannin
    Nov 17, 2022 at 9:37
0
$\begingroup$

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 *)

$\endgroup$
6
  • $\begingroup$ Are you sure you're sampling from a uniform distribution in orthogonal-matrix-space here? $\endgroup$
    – Roman
    Apr 9, 2021 at 9:11
  • $\begingroup$ What is the objection? $\endgroup$ Apr 9, 2021 at 9:13
  • 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$
    – Roman
    Apr 9, 2021 at 9:17
  • $\begingroup$ Interestingly that is true. Do you have an explanation? $\endgroup$ 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 of RandomReal[{-1, 1}, {2, n}]. (2) I don't think RotationMatrix does the right thing here. $\endgroup$
    – Roman
    Apr 9, 2021 at 13:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.