From a9171184455e1821b6597a695a5a84057d1b15bc Mon Sep 17 00:00:00 2001 From: raghav9-97 Date: Wed, 26 Dec 2018 15:01:12 +0530 Subject: [PATCH 1/4] Fixed sprand for integer type --- stdlib/SparseArrays/src/sparsematrix.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/stdlib/SparseArrays/src/sparsematrix.jl b/stdlib/SparseArrays/src/sparsematrix.jl index 6a4f7a36c2214..3322756d4e56d 100644 --- a/stdlib/SparseArrays/src/sparsematrix.jl +++ b/stdlib/SparseArrays/src/sparsematrix.jl @@ -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) From a343ea2d5bab481ee76e081f88a7db74b47720eb Mon Sep 17 00:00:00 2001 From: raghav9-97 Date: Wed, 26 Dec 2018 15:28:52 +0530 Subject: [PATCH 2/4] Added tests --- stdlib/SparseArrays/test/sparse.jl | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/stdlib/SparseArrays/test/sparse.jl b/stdlib/SparseArrays/test/sparse.jl index 3ef2cef966ae2..d889b4c0ad269 100644 --- a/stdlib/SparseArrays/test/sparse.jl +++ b/stdlib/SparseArrays/test/sparse.jl @@ -339,6 +339,11 @@ 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)) +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) From 547e336411177660a063630b34c9e1aa0ec33783 Mon Sep 17 00:00:00 2001 From: raghav9-97 Date: Wed, 26 Dec 2018 17:36:24 +0530 Subject: [PATCH 3/4] Modified sprand --- stdlib/SparseArrays/src/sparsematrix.jl | 1 + stdlib/SparseArrays/test/sparse.jl | 1 + 2 files changed, 2 insertions(+) diff --git a/stdlib/SparseArrays/src/sparsematrix.jl b/stdlib/SparseArrays/src/sparsematrix.jl index 3322756d4e56d..defcee1c7dfce 100644 --- a/stdlib/SparseArrays/src/sparsematrix.jl +++ b/stdlib/SparseArrays/src/sparsematrix.jl @@ -1454,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) diff --git a/stdlib/SparseArrays/test/sparse.jl b/stdlib/SparseArrays/test/sparse.jl index d889b4c0ad269..d81b2adc771e6 100644 --- a/stdlib/SparseArrays/test/sparse.jl +++ b/stdlib/SparseArrays/test/sparse.jl @@ -342,6 +342,7 @@ 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 @testset "kronecker product" begin From 7a79b2e1e7fac2a6e0b5ef02d7d0ed5c051b721f Mon Sep 17 00:00:00 2001 From: raghav9-97 Date: Wed, 26 Dec 2018 22:43:56 +0530 Subject: [PATCH 4/4] Modified tests --- stdlib/SparseArrays/test/sparse.jl | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/stdlib/SparseArrays/test/sparse.jl b/stdlib/SparseArrays/test/sparse.jl index d81b2adc771e6..0486984e6f3d5 100644 --- a/stdlib/SparseArrays/test/sparse.jl +++ b/stdlib/SparseArrays/test/sparse.jl @@ -340,9 +340,8 @@ 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)) + @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