Skip to content

Commit b07d8c4

Browse files
committed
Add Stewart's fast orthogonal/unitary matrix generation algorithm
1 parent bf959ee commit b07d8c4

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/HaarMeasure.jl

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,44 @@ end
7070
#Zyczkowski and Kus, Random unitary matrices, J. Phys. A: Math. Gen. 27,
7171
#4235–4245 (1994).
7272

73+
### Stewarts algorithm for n^2 orthogonal random matrix
74+
using Base.LinAlg: BlasInt
75+
for (s, elty) in (("dlarfg_", Float64),
76+
("zlarfg_", Complex128))
77+
@eval begin
78+
function larfg!(n::Int, α::Ptr{$elty}, x::Ptr{$elty}, incx::Int, τ::Ptr{$elty})
79+
ccall(($(Base.blasfunc(s)), Base.liblapack_name), Void,
80+
(Ptr{BlasInt}, Ptr{$elty}, Ptr{$elty}, Ptr{BlasInt}, Ptr{$elty}),
81+
&n, α, x, &incx, τ)
82+
end
83+
end
84+
end
85+
86+
function Stewart(::Type{Float64}, n)
87+
τ = Array(Float64, n)
88+
H = randn(n, n)
89+
90+
= pointer(τ)
91+
= pointer(H)
92+
pH = pointer(H, 2)
93+
94+
for i = 0:n-2
95+
larfg!(n - i, pβ + (n + 1)*8i, pH + (n + 1)*8i, 1, pτ + 8i)
96+
end
97+
LinAlg.QRPackedQ(H,τ)
98+
end
99+
function Stewart(::Type{Complex128}, n)
100+
τ = Array(Complex128, n)
101+
H = complex(randn(n, n), randn(n,n))
102+
103+
= pointer(τ)
104+
= pointer(H)
105+
pH = pointer(H, 2)
106+
107+
for i = 0:n-2
108+
larfg!(n - i, pβ + (n + 1)*16i, pH + (n + 1)*16i, 1, pτ + 16i)
109+
end
110+
LinAlg.QRPackedQ(H,τ)
111+
end
112+
113+

test/Haar.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ Q=UniformHaar(2, N)
1212
println("Case 3")
1313
println("E(A*Q*B*Q'*A*Q*B*Q') = ", eval(expectation(N, :Q, :(A*Q*B*Q'*A*Q*B*Q'))))
1414

15+
for elty in (Float64, Complex128)
16+
A = Stewart(elty, N)
17+
@test_approx_eq A'A eye(N)
18+
end
19+
1520
end #_HAVE_GSL

0 commit comments

Comments
 (0)