Skip to content
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

Add complex randn #21973

Merged
merged 5 commits into from
May 25, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1181,7 +1181,9 @@ const ziggurat_exp_r = 7.6971174701310497140446280481
Generate a normally-distributed random number of type `T` with mean 0 and standard deviation 1.
Optionally generate an array of normally-distributed random numbers.
The `Base` module currently provides an implementation for the types
`Float16`, `Float32`, and `Float64` (the default).
`Float16`, `Float32`, and `Float64` (the default), and their `Complex` counterparts.
When the type argument is complex, the values are drawn from the circularly symmetric
complex normal distribution.
"""
@inline function randn(rng::AbstractRNG=GLOBAL_RNG)
@inbounds begin
Expand All @@ -1194,6 +1196,9 @@ The `Base` module currently provides an implementation for the types
end
end

Base.@irrational SQRT_HALF 0.7071067811865475244008 sqrt(big(0.5))
randn{T}(rng::AbstractRNG, ::Type{Complex{T}}) = Complex{T}(SQRT_HALF * randn(rng, T), SQRT_HALF*randn(rng, T))
Copy link
Member

Choose a reason for hiding this comment

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

For new code, maybe use the where syntax. Also, don't you want to restrict T<:AbstractFloat?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah right I forgot about the new where syntax. Good call about restriction at the call site. I wasn't sure if should just let it blow up at the call the randn(rng, T), but it does makes the error message a little more cryptic doesn't it.

Copy link
Member

Choose a reason for hiding this comment

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

I think it would make an InexactError when calling the complex constructor on real (because of the multiplication by SQRT_HALF) values, e.g. with T==Int. Also, this distribution will make sense only when randn(T) produces a number in [0,1), i.e. T<:AbstractFloat.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I understand what you mean by this. Can you explain another way?


# this unlikely branch is put in a separate function for better efficiency
function randn_unlikely(rng, idx, rabs, x)
@inbounds if idx == 0
Expand Down
6 changes: 3 additions & 3 deletions base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,9 @@ include("combinatorics.jl")
# more hashing definitions
include("hashing2.jl")

# irrational mathematical constants
include("irrationals.jl")

# random number generation
include("dSFMT.jl")
include("random.jl")
Expand Down Expand Up @@ -334,9 +337,6 @@ const × = cross
# statistics
include("statistics.jl")

# irrational mathematical constants
include("irrationals.jl")

# signal processing
include("dft.jl")
importall .DFT
Expand Down
13 changes: 10 additions & 3 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ srand(0); rand(); x = rand(384)
@test typeof(rand(false:true)) === Bool
@test typeof(rand(Char)) === Char
@test length(randn(4, 5)) == 20
@test length(randn(Complex128, 4, 5)) == 20
@test length(bitrand(4, 5)) == 20

@test rand(MersenneTwister(0)) == 0.8236475079774124
Expand Down Expand Up @@ -65,6 +66,11 @@ randn!(MersenneTwister(42), A)
@test A == [-0.5560268761463861 0.027155338009193845;
-0.444383357109696 -0.29948409035891055]

B = zeros(Complex128, 2)
randn!(MersenneTwister(42), B)
@test B == [Complex128(-0.5560268761463861,-0.444383357109696),
Complex128(0.027155338009193845,-0.29948409035891055)] * 0.7071067811865475244008

for T in (Int8, UInt8, Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128, BigInt,
Float16, Float32, Float64, Rational{Int})
r = rand(convert(T, 97):convert(T, 122))
Expand Down Expand Up @@ -306,8 +312,9 @@ end

# test all rand APIs
for rng in ([], [MersenneTwister(0)], [RandomDevice()])
types = [Base.BitInteger_types..., Bool, Float16, Float32, Float64, Char]
ftypes = [Float16, Float32, Float64]
cftypes = [Complex32, Complex64, Complex128, ftypes...]
types = [Bool, Char, Base.BitInteger_types..., ftypes...]
b2 = big(2)
u3 = UInt(3)
for f in [rand, randn, randexp]
Expand All @@ -316,7 +323,7 @@ for rng in ([], [MersenneTwister(0)], [RandomDevice()])
f(rng..., 2, 3) ::Array{Float64, 2}
f(rng..., b2, u3) ::Array{Float64, 2}
f(rng..., (2, 3)) ::Array{Float64, 2}
for T in (f === rand ? types : ftypes)
for T in (f === rand ? types : f === randn ? cftypes : ftypes)
a0 = f(rng..., T) ::T
a1 = f(rng..., T, 5) ::Vector{T}
a2 = f(rng..., T, 2, 3) ::Array{T, 2}
Expand All @@ -330,7 +337,7 @@ for rng in ([], [MersenneTwister(0)], [RandomDevice()])
end
end
for f! in [rand!, randn!, randexp!]
for T in (f! === rand! ? types : ftypes)
for T in (f! === rand! ? types : f! === randn! ? cftypes : ftypes)
X = T == Bool ? T[0,1] : T[0,1,2]
for A in (Array{T}(5), Array{T}(2, 3))
f!(rng..., A) ::typeof(A)
Expand Down