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

Fixed sprand for dimensions of unusual integer type #30516

Merged
merged 4 commits into from
Dec 27, 2018
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1443,6 +1443,7 @@ julia> sprand(Float64, 3, 0.75)
"""
function sprand(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat,
rfn::Function, ::Type{T}=eltype(rfn(r,1))) where T
m,n = Int(m), Int(n)
N = m*n
N == 0 && return spzeros(T,m,n)
N == 1 && return rand(r) <= density ? sparse([1], [1], rfn(r,1)) : spzeros(T,1,1)
Expand All @@ -1453,6 +1454,7 @@ end

function sprand(m::Integer, n::Integer, density::AbstractFloat,
rfn::Function, ::Type{T}=eltype(rfn(1))) where T
m,n = Int(m), Int(n)
N = m*n
N == 0 && return spzeros(T,m,n)
N == 1 && return rand() <= density ? sparse([1], [1], rfn(1)) : spzeros(T,1,1)
Expand Down
6 changes: 6 additions & 0 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,12 @@ end
end
end

@testset "Issue #30502" begin
@test sprand(UInt8(16), UInt8(16), 1.0) != spzeros(UInt8(16), UInt8(16))
@test sprand(Int8(16), Int8(16), 1.0) != spzeros(Int8(16), Int8(16))
@test sprand(UInt8(16), UInt8(16), 1.0, ones) != spzeros(UInt8(16), UInt8(16))
end
Copy link
Member

@ViralBShah ViralBShah Dec 26, 2018

Choose a reason for hiding this comment

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

Can we have a test that checks correctness of the intended answer with ==? Also, only one test for each of the changed functions would be enough here.


@testset "kronecker product" begin
for (m,n) in ((5,10), (13,8), (14,10))
a = sprand(m, 5, 0.4); a_d = Matrix(a)
Expand Down