Skip to content

Add support for local RNG seed in Haar measure matrix generation and reproducibility tests #92

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/Ginibre.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export rand, Ginibre
import Base.rand
import Random: AbstractRNG, GLOBAL_RNG

"""
Ginibre(β::Int, N::Int) <: ContinuousMatrixDistribution
Expand Down Expand Up @@ -30,30 +31,38 @@
end

"""
rand(W::Ginibre)
rand(rng::AbstractRNG, W::Ginibre)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rand(rng::AbstractRNG, W::Ginibre)
rand([rng::AbstractRNG,] W::Ginibre)

see https://docs.julialang.org/en/v1/manual/documentation/.


Samples a matrix from the Ginibre ensemble.

For `β = 1,2,4`, generates matrices randomly sampled from the real, complex, and quaternion
Ginibre ensemble, respectively.
"""
function rand(W::Ginibre)
function rand(rng::AbstractRNG, W::Ginibre)
beta, n = W.beta, W.N
if beta==1
randn(n,n)
randn(rng,n,n)
elseif beta==2
randn(n,n)+im*randn(n,n)
randn(rng,n,n)+im*randn(rng,n,n)

Check warning on line 46 in src/Ginibre.jl

View check run for this annotation

Codecov / codecov/patch

src/Ginibre.jl#L46

Added line #L46 was not covered by tests
elseif beta==4
Q0=randn(n,n)
Q1=randn(n,n)
Q2=randn(n,n)
Q3=randn(n,n)
Q0=randn(rng,n,n)
Q1=randn(rng,n,n)
Q2=randn(rng,n,n)
Q3=randn(rng,n,n)

Check warning on line 51 in src/Ginibre.jl

View check run for this annotation

Codecov / codecov/patch

src/Ginibre.jl#L48-L51

Added lines #L48 - L51 were not covered by tests
[Q0+im*Q1 Q2+im*Q3;-Q2+im*Q3 Q0-im*Q1]
else
error(string("beta = ", beta, " not implemented"))
end
end


rand(W::Ginibre)=rand(GLOBAL_RNG, W)

Check warning on line 59 in src/Ginibre.jl

View check run for this annotation

Codecov / codecov/patch

src/Ginibre.jl#L59

Added line #L59 was not covered by tests

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These extra lines don't seem necessary






function jpdf(Z::AbstractMatrix{z}) where {z<:Complex}
pi^(size(Z,1)^2)*exp(-trace(Z'*Z))
end
Expand Down
16 changes: 10 additions & 6 deletions src/HaarMeasure.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#TODO implement O(n^2) method
"""
rand(W::Haar, n::Int)
rand(rng::AbstractRNG, W::Haar, n::Int)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
rand(rng::AbstractRNG, W::Haar, n::Int)
rand([rng::AbstractRNG,] W::Haar, n::Int)

same here.


Computes samples of real or complex Haar matrices of size `n`×`n`.

Expand All @@ -24,19 +24,19 @@
- Edelman and Rao, 2005
- Mezzadri, 2006, math-ph/0609050
"""
function rand(W::Haar, n::Int, doCorrection::Int=1)
function rand(rng::AbstractRNG, W::Haar, n::Int, doCorrection::Int=1)
beta = W.beta
M=rand(Ginibre(beta,n))
M=rand(rng,Ginibre(beta,n))
q,r=qr(M)
if doCorrection==0
q
elseif doCorrection==1
if beta==1
L = sign.(rand(n).-0.5)
L = sign.(rand(rng,n).-0.5)
elseif beta==2
L = exp.(im*rand(n)*2pi)
L = exp.(im*rand(rng,n)*2pi)

Check warning on line 37 in src/HaarMeasure.jl

View check run for this annotation

Codecov / codecov/patch

src/HaarMeasure.jl#L37

Added line #L37 was not covered by tests
elseif beta==4
L = exp.(im*rand(2n)*2pi)
L = exp.(im*rand(rng,2n)*2pi)

Check warning on line 39 in src/HaarMeasure.jl

View check run for this annotation

Codecov / codecov/patch

src/HaarMeasure.jl#L39

Added line #L39 was not covered by tests
else
error(string("beta = ",beta, " not implemented."))
end
Expand All @@ -54,6 +54,10 @@
end
end

rand(W::Haar, n::Int, doCorrection::Int=1)= rand(GLOBAL_RNG,W,n,doCorrection)


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These extra lines don't seem necessary


#A utility method to check if the piecewise correction is needed
#This checks the R part of the QR factorization; if correctly done,
#the diagonals are all chi variables so are non-negative
Expand Down
20 changes: 20 additions & 0 deletions test/Haar.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using RandomMatrices
using LinearAlgebra: I, tr, Diagonal, QRPackedQ
using Test
using Random

@testset "Haar" begin

Expand Down Expand Up @@ -29,4 +30,23 @@ for T in (Float64, ComplexF64)
@test C*A' ≈ C*A2'
end


@testset "Local RNG reproducibility" begin
rng1 = MersenneTwister(1234)
rng2 = MersenneTwister(1234)

A = rand(rng1, Haar(1), N)
B = rand(rng2, Haar(1), N)

@test A ≈ B

# Confirm that rand still works without passing an RNG
C = rand(Haar(1), N)
@test size(C) == (N, N)
end


end # testset


Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These extra lines below don't seem necessary.


Loading