Skip to content

Commit

Permalink
deprecate sprandbool
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Apr 28, 2016
1 parent 7454cfb commit 0b5e0c7
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
12 changes: 12 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 6 additions & 14 deletions base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions base/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/perf/sparse/getindex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down
14 changes: 7 additions & 7 deletions test/sparsedir/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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,))
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/sparsedir/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down

0 comments on commit 0b5e0c7

Please sign in to comment.