-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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...)) | ||
$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...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, you shouldn't use The reason is that you still want There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Leaving off this change will still break the infinite-loop, but the error message is less clear. Thoughts? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 My reason is that, to add If the price for that is slightly more obscure method errors, that's fine. In fact, There was a problem hiding this comment. Choose a reason for hiding this commentThe 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...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here, revert this part of the patch ... the patch to |
||
end | ||
end | ||
end | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add a second loop for concision:
(and, why not, a loop over the dimensions: |
||
end | ||
|
||
function hist(X,n) | ||
|
There was a problem hiding this comment.
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).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Thanks!