From 7073d71458e8d88c01908c951a17361c395e0985 Mon Sep 17 00:00:00 2001 From: Klaus Crusius Date: Mon, 19 Nov 2018 14:06:37 +0100 Subject: [PATCH] added sprandn methods with Type --- stdlib/SparseArrays/src/sparsematrix.jl | 4 +++- stdlib/SparseArrays/src/sparsevector.jl | 2 ++ stdlib/SparseArrays/test/sparse.jl | 7 +++++++ stdlib/SparseArrays/test/sparsevector.jl | 5 +++++ 4 files changed, 17 insertions(+), 1 deletion(-) diff --git a/stdlib/SparseArrays/src/sparsematrix.jl b/stdlib/SparseArrays/src/sparsematrix.jl index 02c975168c6ec..7fac079ac97d6 100644 --- a/stdlib/SparseArrays/src/sparsematrix.jl +++ b/stdlib/SparseArrays/src/sparsematrix.jl @@ -1471,7 +1471,7 @@ sprand(r::AbstractRNG, ::Type{Bool}, m::Integer, n::Integer, density::AbstractFl sprand(::Type{T}, m::Integer, n::Integer, density::AbstractFloat) where {T} = sprand(GLOBAL_RNG, T, m, n, density) """ - sprandn([rng], m[,n],p::AbstractFloat) + sprandn([rng],[,Type],m[,n],p::AbstractFloat) Create a random sparse vector of length `m` or sparse matrix of size `m` by `n` with the specified (independent) probability `p` of any entry being nonzero, @@ -1488,6 +1488,8 @@ julia> sprandn(2, 2, 0.75) """ sprandn(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,randn,Float64) sprandn(m::Integer, n::Integer, density::AbstractFloat) = sprandn(GLOBAL_RNG,m,n,density) +sprandn(r::AbstractRNG, ::Type{T}, m::Integer, n::Integer, density::AbstractFloat) where T = sprand(r,m,n,density,(r,i) -> randn(r,T,i), T) +sprandn(::Type{T}, m::Integer, n::Integer, density::AbstractFloat) where T = sprandn(GLOBAL_RNG,T,m,n,density) LinearAlgebra.fillstored!(S::SparseMatrixCSC, x) = (fill!(nzvalview(S), x); S) diff --git a/stdlib/SparseArrays/src/sparsevector.jl b/stdlib/SparseArrays/src/sparsevector.jl index e7f0e1a2db26a..ce9fbbc35e947 100644 --- a/stdlib/SparseArrays/src/sparsevector.jl +++ b/stdlib/SparseArrays/src/sparsevector.jl @@ -506,6 +506,8 @@ sprand(::Type{T}, n::Integer, p::AbstractFloat) where {T} = sprand(GLOBAL_RNG, T sprandn(n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, n, p, randn) sprandn(r::AbstractRNG, n::Integer, p::AbstractFloat) = sprand(r, n, p, randn) +sprandn(::Type{T}, n::Integer, p::AbstractFloat) where T = sprand(GLOBAL_RNG, n, p, (r, i) -> randn(r, T, i)) +sprandn(r::AbstractRNG, ::Type{T}, n::Integer, p::AbstractFloat) where T = sprand(r, n, p, (r, i) -> randn(r, T, i)) ## Indexing into Matrices can return SparseVectors diff --git a/stdlib/SparseArrays/test/sparse.jl b/stdlib/SparseArrays/test/sparse.jl index c8e5ebd8b5acb..9eb2069d3984c 100644 --- a/stdlib/SparseArrays/test/sparse.jl +++ b/stdlib/SparseArrays/test/sparse.jl @@ -2335,4 +2335,11 @@ end @test m2.module == SparseArrays end +@testset "sprandn with type $T" for T in (Float64, Float32, Float16, ComplexF64, ComplexF32, ComplexF16) + @test sprandn(T, 5, 5, 0.5) isa AbstractSparseMatrix{T} +end +@testset "sprandn with invalid type $T" for T in (AbstractFloat, BigFloat, Complex) + @test_throws MethodError sprandn(T, 5, 5, 0.5) +end + end # module diff --git a/stdlib/SparseArrays/test/sparsevector.jl b/stdlib/SparseArrays/test/sparsevector.jl index 6061b21a6721c..d8ada3963e2c5 100644 --- a/stdlib/SparseArrays/test/sparsevector.jl +++ b/stdlib/SparseArrays/test/sparsevector.jl @@ -154,6 +154,11 @@ end end end + let xr = sprandn(ComplexF64, 1000, 0.9) + @test isa(xr, SparseVector{ComplexF64,Int}) + @test length(xr) == 1000 + end + let xr = sprand(Bool, 1000, 0.9) @test isa(xr, SparseVector{Bool,Int}) @test length(xr) == 1000