Skip to content

Commit

Permalink
Fixed sprand for dimensions of unusual integer type (#30516)
Browse files Browse the repository at this point in the history
Fixes #30502
  • Loading branch information
raghav9-97 authored and ViralBShah committed Dec 27, 2018
1 parent 573cf74 commit 2ab1405
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
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
5 changes: 5 additions & 0 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,11 @@ end
end
end

@testset "Issue #30502" begin
@test nnz(sprand(UInt8(16), UInt8(16), 1.0)) == 256
@test nnz(sprand(UInt8(16), UInt8(16), 1.0, ones)) == 256
end

@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

0 comments on commit 2ab1405

Please sign in to comment.