# Generating random group matrices

**URL:** <https://discourse.julialang.org/t/generating-random-group-matrices/6634>\
**Category:** Numerics\
**Created:** [October 23, 2017, 7:52pm UTC](https://discourse.julialang.org/t/generating-random-group-matrices/6634 "2017-10-23T19:52:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [October 23, 2017, 7:52pm UTC](https://discourse.julialang.org/t/generating-random-group-matrices/6634/1 "2017-10-23T19:52:11Z")

</div>

I was wondering if there was already a package out there for generating random matrices from SU(N) and SO(N). This mostly boils down to generating random points on n-spheres and is not difficult to implement for particular cases, but I was wondering if someone had already done this as it would be nice to have a more efficient and properly parallelized implementation than I am likely to write.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [October 23, 2017, 7:59pm UTC](https://discourse.julialang.org/t/generating-random-group-matrices/6634/2 "2017-10-23T19:59:18Z")

</div>

RandomMatrices.jl lets you sample from Haar measure.

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [October 23, 2017, 8:44pm UTC](https://discourse.julialang.org/t/generating-random-group-matrices/6634/3 "2017-10-23T20:44:10Z")

</div>

This seems to have quite a lot of deprecation warnings at the moment. That aside, my understanding of this is that this can generate me random U(N) matrices… the only way I can think off the top of my head of producing SU(N) matrices from these (essentially dividing out the determinant) seems rather inefficient to me (not to mention the fact that I’m then essentially generating an extra S^{1} variable only to divide it out again in a reall complicated way!)… Obviously this isn’t really my area of expertise, so perhaps I’m missing something stupidly obvious.

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [October 23, 2017, 8:54pm UTC](https://discourse.julialang.org/t/generating-random-group-matrices/6634/4 "2017-10-23T20:54:11Z")

</div>

I suppose `Q^2` probably has the right distribution, though it requires a dense matrix multiplication, as far as I know.

@alanedelman might have a better suggestion.

I’ll fix the deprecation warnings soon (I thought I already had…).

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [October 23, 2017, 9:13pm UTC](https://discourse.julialang.org/t/generating-random-group-matrices/6634/5 "2017-10-23T21:13:00Z")

</div>

Hmm, having looked at the code HaarMeasure.jl the default version just calls QR on a Ginibre (random Gaussian) matrix. If there is a `qrfact` call that uses Given’s instead of Householder, then it should give you the equivalent for `SO(N)`.

There is also an `O(N^2)` implementation of Stewart’s algorithm. Perhaps that can be modified?

---

<div class="post-metadata">

**Author:** ![rveltz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rveltz/32/2707_2.png) [@rveltz](https://discourse.julialang.org/u/rveltz)\
**Post date:** [October 24, 2017, 12:25pm UTC](https://discourse.julialang.org/t/generating-random-group-matrices/6634/6 "2017-10-24T12:25:07Z")

</div>

Maybe you can pullback your sampling distribution to its Lie Algebra which is quite simple (? pun intended).

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [October 24, 2017, 2:00pm UTC](https://discourse.julialang.org/t/generating-random-group-matrices/6634/7 "2017-10-24T14:00:15Z")

</div>

Actually, I just realized something that makes me incredibly happy (though, bear in mind, I don’t know how efficient this is, probably not very). I can just use `expm` of the generators. Why does this make me very happy? Because look how beautifully this can be written in Julia. For example, for SU(2), after defining the Pauli matrices and a random unit vector `n` on \mathbb{R}^{N^{2}-1} and a random scalar in [0, 2\pi)

```julia
σ⃗ = [σ₁, σ₂, σ₃]
U = expm(im*φ*n'*σ⃗)

```

How cool is that? Unfortunately this probably involves dense matrix multiplication, so despite looking beautiful it’s probably incredibly inefficient, so alternatively I can do  
 \cos(\phi) + i(n\_{a}T\_{a})\sin(\phi)  
where T\_{a} are the generators. This should involve only scalar multiplication and dense matrix addition. These are both essentially what @rveltz suggested.
