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

RFC: Restricts parameter type for randn & randexp to Float types for #17608. #17627

Merged
merged 3 commits into from
Jul 29, 2016
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
16 changes: 8 additions & 8 deletions base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1213,14 +1213,14 @@ let Floats = Union{Float16,Float32,Float64}
$randfun!(A::AbstractArray) = $randfun!(GLOBAL_RNG, A)

# generating arrays
$randfun{T}(rng::AbstractRNG, ::Type{T}, dims::Dims) = $randfun!(rng, Array{T}(dims))
$randfun{T}(rng::AbstractRNG, ::Type{T}, dims::Integer...) = $randfun!(rng, Array{T}(dims...))
$randfun{T}( ::Type{T}, dims::Dims) = $randfun(GLOBAL_RNG, T, dims)
$randfun{T}( ::Type{T}, dims::Integer...) = $randfun(GLOBAL_RNG, T, dims...)
$randfun( rng::AbstractRNG, dims::Dims) = $randfun(rng, Float64, dims)
$randfun( rng::AbstractRNG, dims::Integer...) = $randfun(rng, Float64, dims...)
$randfun( dims::Dims) = $randfun(GLOBAL_RNG, Float64, dims)
$randfun( dims::Integer...) = $randfun(GLOBAL_RNG, Float64, dims...)
$randfun{T}(rng::AbstractRNG, ::Type{T}, dims::Dims ) = $randfun!(rng, Array{T}(dims))
$randfun{T}(rng::AbstractRNG, ::Type{T}, dim0::Integer, dims::Integer...) = $randfun!(rng, Array{T}(dim0, dims...))
Copy link
Member

Choose a reason for hiding this comment

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

Probably dim1 would be more Julian (1-based).

Copy link
Member Author

Choose a reason for hiding this comment

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

Done. Thanks!

$randfun{T}( ::Type{T}, dims::Dims ) = $randfun(GLOBAL_RNG, T, dims)
$randfun{T}( ::Type{T}, dim0::Integer, dims::Integer...) = $randfun(GLOBAL_RNG, T, dim0, dims...)
Copy link
Member

@stevengj stevengj Jul 28, 2016

Choose a reason for hiding this comment

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

No, you shouldn't use dim0, dims... here, you should just juse dims...; dim0 should only be in the lowest-level function that takes an rng argument.

The reason is that you still want randn(T) to call randn(GLOBAL_RNG, T), even if there are no dimension arguments.

Copy link
Member 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 agree; there is already the definition at line 1203 which covers the case of empty dims. So randn(Float32) will still call randn(GLOBAL_RNG, T).

Leaving off this change will still break the infinite-loop, but the error message is less clear. randn(Int) will successfully match the definition at line 1219, and then produce the error message: MethodError: no method matching randn(::MersenneTwister, ::Type{Int64}), which is less clear than the current error message, MethodError: no method matching randn(::Type{Int64}), which is what the user wrote.

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I missed the definition on line 1203. I would prefer to just delete that definition. (If there is some performance reason to define the one-argument case separately ... splatting penalty? ... we should still define it generically for all types, not just T<:$Floats.)

My reason is that, to add randn for a new type T, you should just have to define one method: randn(rng::AbstractRNG, ::Type{T}). You shouldn't have to define other methods that we could get just by loosening the type signature.

If the price for that is slightly more obscure method errors, that's fine. In fact, no method matching randn(::MersenneTwister, ::Type{Int64}) is actually more helpful, because it tells you exactly what method you need to implement to make everything work.

Copy link
Member Author

Choose a reason for hiding this comment

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

That is a great reason; I'm sold. Thanks.

So, for this PR, I'm gonna change it to work for all types, as you suggested. Maybe it makes sense to just delete that line, but I'll leave that up to future PRs?

$randfun( rng::AbstractRNG, dims::Dims ) = $randfun(rng, Float64, dims)
$randfun( rng::AbstractRNG, dim0::Integer, dims::Integer...) = $randfun(rng, Float64, dim0, dims...)
$randfun( dims::Dims ) = $randfun(GLOBAL_RNG, Float64, dims)
$randfun( dim0::Integer, dims::Integer...) = $randfun(GLOBAL_RNG, Float64, dim0, dims...)
Copy link
Member

Choose a reason for hiding this comment

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

Same here, revert this part of the patch ... the patch to base/random.jl should need to change only one line.

end
end
end
Expand Down
17 changes: 17 additions & 0 deletions test/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,23 @@ for rng in ([], [MersenneTwister()], [RandomDevice()])
bitrand(rng..., 2, 3) ::BitArray{2}
rand!(rng..., BitArray(5)) ::BitArray{1}
rand!(rng..., BitArray(2, 3)) ::BitArray{2}

# Test that you cannot call randn or randexp with non-Float types.
for r in [randn, randexp, randn!, randexp!]
@test_throws MethodError r(Int)
@test_throws MethodError r(Int32)
@test_throws MethodError r(Bool)
@test_throws MethodError r(String)
@test_throws MethodError r(AbstractFloat)
# TODO(#17627): Consider adding support for randn(BigFloat) and removing this test.
@test_throws MethodError r(BigFloat)

@test_throws MethodError r(Int64, (2,3))
@test_throws MethodError r(String, 1)

@test_throws MethodError r(rng..., Number, (2,3))
@test_throws MethodError r(rng..., Any, 1)
end
Copy link
Member

Choose a reason for hiding this comment

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

I would add a second loop for concision:

for T in [Int, Int32, String, AbstractFloat, BigFloat, Number, Any]
    @test_throws MethodError r(rng..., T)
    @test_throws MethodError r(rng..., T, 1)
    @test_throws MethodError r(rng..., T, (2, 3))
end

(and, why not, a loop over the dimensions: for d in ([], [1], [(2, 3)])).

end

function hist(X,n)
Expand Down