From 0b5e0c7864dcb1526387dc724e521d9d6e5be4ce Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Thu, 28 Apr 2016 23:53:14 +0200 Subject: [PATCH] deprecate sprandbool --- base/deprecated.jl | 12 ++++++++++++ base/sparse/sparsematrix.jl | 20 ++++++-------------- base/sparse/sparsevector.jl | 6 +++--- test/perf/sparse/getindex.jl | 4 ++-- test/sparsedir/sparse.jl | 14 +++++++------- test/sparsedir/sparsevector.jl | 4 ++-- 6 files changed, 32 insertions(+), 28 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 1eb13ffcbdab2..d6d93c91758a5 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1078,6 +1078,18 @@ end @deprecate copy(x::AbstractString) identity(x) @deprecate copy(x::Tuple) identity(x) +""" + sprandbool(m[,n],p) +Create a random `m` by `n` sparse boolean matrix or length `m` sparse boolean +vector with the specified (independent) probability `p` of any entry being +`true`. +""" +function sprandbool end +@deprecate sprandbool(m::Integer, n::Integer, density::AbstractFloat) sprand(Bool, m, n, density) +@deprecate sprandbool(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) sprand(r, m, n, density, (r, i) -> ones(Bool, i)) +@deprecate sprandbool(n::Integer, density::AbstractFloat) sprand(Bool, n, density) +@deprecate sprandbool(r::AbstractRNG, n::Integer, density::AbstractFloat) sprand(r, n, density, Bool) + # During the 0.5 development cycle, do not add any deprecations below this line # To be deprecated in 0.6 diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 20c233b4bf8e6..301b2a0d1f2a7 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -923,7 +923,11 @@ function sprand{T}(m::Integer, n::Integer, density::AbstractFloat, sparse_IJ_sorted!(I, J, rfn(length(I)), m, n, +) # it will never need to combine end -sprand{T}(::Type{T}, m::Integer, n::Integer, density::AbstractFloat) = sprand(m, n, density, (i) -> rand(T, i)) +truebools(r::AbstractRNG, n::Integer) = ones(Bool, n) + +sprand{T}(::Type{T}, m::Integer, n::Integer, density::AbstractFloat) = sprand(GLOBAL_RNG, m, n, density, (r, i) -> rand(r, T, i), T) +sprand(::Type{Bool}, m::Integer, n::Integer, density::AbstractFloat) = sprand(GLOBAL_RNG, m, n, density, truebools, Bool) + sprand(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,rand,Float64) sprand(m::Integer, n::Integer, density::AbstractFloat) = sprand(GLOBAL_RNG,m,n,density) sprandn(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,randn,Float64) @@ -935,19 +939,7 @@ 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, where nonzero values are sampled from the normal distribution. """ -sprandn( m::Integer, n::Integer, density::AbstractFloat) = sprandn(GLOBAL_RNG,m,n,density) - -truebools(r::AbstractRNG, n::Integer) = ones(Bool, n) -sprandbool(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,truebools,Bool) - -""" - sprandbool(m[,n],p) - -Create a random `m` by `n` sparse boolean matrix or length `m` sparse boolean -vector with the specified (independent) probability `p` of any entry being -`true`. -""" -sprandbool(m::Integer, n::Integer, density::AbstractFloat) = sprandbool(GLOBAL_RNG,m,n,density) +sprandn(m::Integer, n::Integer, density::AbstractFloat) = sprandn(GLOBAL_RNG,m,n,density) """ spones(S) diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index b8c2c87c04d22..a9b30a43a9f6e 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -356,17 +356,17 @@ function sprand(r::AbstractRNG, n::Integer, p::AbstractFloat, rfn::Function) SparseVector(n, I, V) end +sprand{T}(::Type{T}, n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, n, p, rand, T) sprand{T}(n::Integer, p::AbstractFloat, ::Type{T}) = sprand(GLOBAL_RNG, n, p, rand, T) +sprand(::Type{Bool}, n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, n, p, truebools) sprand(n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, n, p, rand) sprand{T}(r::AbstractRNG, n::Integer, p::AbstractFloat, ::Type{T}) = sprand(r, n, p, rand, T) +sprand(r::AbstractRNG, n::Integer, p::AbstractFloat, ::Type{Bool}) = sprand(r, n, p, truebools) sprand(r::AbstractRNG, n::Integer, p::AbstractFloat) = sprand(r, n, p, rand) sprandn(n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, n, p, randn) sprandn(r::AbstractRNG, n::Integer, p::AbstractFloat) = sprand(r, n, p, randn) -sprandbool(n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, n, p, truebools) -sprandbool(r::AbstractRNG, n::Integer, p::AbstractFloat) = sprand(r, n, p, truebools) - ## Indexing into Matrices can return SparseVectors # Column slices diff --git a/test/perf/sparse/getindex.jl b/test/perf/sparse/getindex.jl index 7e5c6551b596b..af3254e628d2a 100644 --- a/test/perf/sparse/getindex.jl +++ b/test/perf/sparse/getindex.jl @@ -95,7 +95,7 @@ function sparse_getindex_perf() c = counters[sz] if indstr=="logical array" # make a logical array of the right size - ind = sprandbool(size(m,1)..., 1e-5) + ind = sprand(Bool, size(m,1)..., 1e-5) end if nargs==2 @timeit fun(m, ind) "sparse_getindex_$s1$c" "Sparse matrix with $ms, $funstr with $indstr" @@ -120,7 +120,7 @@ function sparse_getindex_perf() continue # logical indexing with medium size sparse matrix takes too long end # make a logical array of the right size - ind = sprandbool(size(m)..., 1e-5) + ind = sprand(Bool, size(m)..., 1e-5) c = counters[sz] @timeit one_arg_indexing(m, ind) "sparse_getindex_$s1$c" "$s2 with $ms, linear indexing with $indstr" counters[sz] += 1 diff --git a/test/sparsedir/sparse.jl b/test/sparsedir/sparse.jl index f8c5122b231b3..9aa0cff6d2faa 100644 --- a/test/sparsedir/sparse.jl +++ b/test/sparsedir/sparse.jl @@ -389,11 +389,11 @@ A = speye(Bool, 5) @test sprand(4,5,0.5).^0 == sparse(ones(4,5)) # issue #5985 -@test sprandbool(4, 5, 0.0) == sparse(zeros(Bool, 4, 5)) -@test sprandbool(4, 5, 1.00) == sparse(ones(Bool, 4, 5)) +@test sprand(Bool, 4, 5, 0.0) == sparse(zeros(Bool, 4, 5)) +@test sprand(Bool, 4, 5, 1.00) == sparse(ones(Bool, 4, 5)) sprb45nnzs = zeros(5) for i=1:5 - sprb45 = sprandbool(4, 5, 0.5) + sprb45 = sprand(Bool, 4, 5, 0.5) @test length(sprb45) == 20 sprb45nnzs[i] = sum(sprb45)[1] end @@ -589,7 +589,7 @@ let A = speye(Int, 5), I=1:10, X=reshape([trues(10); falses(15)],5,5) @test A[I] == A[X] == collect(1:10) end -let S = sprand(50, 30, 0.5, x->round(Int,rand(x)*100)), I = sprandbool(50, 30, 0.2) +let S = sprand(50, 30, 0.5, x->round(Int,rand(x)*100)), I = sprand(Bool, 50, 30, 0.2) FS = full(S) FI = full(I) @test sparse(FS[FI]) == S[I] == S[FI] @@ -973,7 +973,7 @@ end @test spzeros(1,2) .* spzeros(0,1) == zeros(0,2) # test throws -A = sprandbool(5,5,0.2) +A = sprand(Bool, 5,5,0.2) @test_throws ArgumentError reinterpret(Complex128,A) @test_throws ArgumentError reinterpret(Complex128,A,(5,5)) @test_throws DimensionMismatch reinterpret(Int8,A,(20,)) @@ -990,9 +990,9 @@ A = speye(5) @test convert(Matrix,A) == full(A) # test float -A = sprandbool(5,5,0.0) +A = sprand(Bool, 5,5,0.0) @test eltype(float(A)) == Float64 # issue #11658 -A = sprandbool(5,5,0.2) +A = sprand(Bool, 5,5,0.2) @test float(A) == float(full(A)) # test sparsevec diff --git a/test/sparsedir/sparsevector.jl b/test/sparsedir/sparsevector.jl index 07d00954372ad..6b33e2c02d094 100644 --- a/test/sparsedir/sparsevector.jl +++ b/test/sparsedir/sparsevector.jl @@ -143,7 +143,7 @@ let xr = sprandn(1000, 0.9) end end -let xr = sprandbool(1000, 0.9) +let xr = sprand(Bool, 1000, 0.9) @test isa(xr, SparseVector{Bool,Int}) @test length(xr) == 1000 @test all(nonzeros(xr)) @@ -152,7 +152,7 @@ end let r1 = MersenneTwister(), r2 = MersenneTwister() @test sprand(r1, 100, .9) == sprand(r2, 100, .9) @test sprandn(r1, 100, .9) == sprandn(r2, 100, .9) - @test sprandbool(r1, 100, .9) == sprandbool(r2, 100, .9) + @test sprand(r1, 100, .9, Bool) == sprand(r2, 100, .9, Bool) end ### Element access