dear julia experts—has anyone already written a random orthogonal matrix generator for square matrices?
Is the right way to do this generating a random normal matrix first and then running a Gram-Schmidt orthogonalization? (is this somewhere in Julia already, so that I won’t rewrite the wheel)?
the RandomMatrices installs in 0.6.2, but the brews for macOS abort somewhere, and there are deprecation warnings in the RandomMatrices code (e.g., use exp.() instead of exp() ). so the install and the code could use some tender care.
I am looking at the documentation. I am way over my head on the underlying math.
What I really want is K random directions with maximum distances in N dimensions (N<=K). So, if I have 2 directions and I want 4 directions, it would be a rotate of (1,0),(0,1),(1,1),(-1,1). If by any chance this is already in this package, please let me know. Otherwise, please ignore.
I am definitely ok with what I know and what I have. I can draw two matrices, and hope that they will not happen to be highly correlated. It’s a little less efficient, but it will do.
Note that this answer, which is currently marked as the accepted solution, does not produce uniform (Haar) distributed answers. For instance, it will always produce Q matrices with elements of the same sign in the first column. Simonbyrne’s answer is the correct one.
Another remark is that (Julia’s Householder-based) QR is more stable than Gram-Schmidt which OP was considering.
with m \geq n will produce a random m \times n Q with orthonormal columns taken from the same measure as in the square case. The QR algorithm works sequentially on the columns of A to build corresponding columns of Q and R. So the first n columns of Q and R will be the same as they would if A were extended to be square with A_{ij}, j \leq n held constant.
Thank you very much for your answer! Extrapolating implies that for increasing n once m\le n, the Q matrix does not change anymore? Hence, with focus on sampling Qm\times m-matrices, there is no reason to go beyond m=n?
I recently digged into methods of uniform sampling from the group of unit quaternions, and I stumbled upon this good reference, that suggests a method to ensure that the probability distribution of the generated orthogonal matrix is the Haar measure: Francesco Mezzadri: How to generate random matrices from the classical compact groups, Notices Amer. Math. Soc. 54(5), 592–604, 2007.