Skip to content

Commit bbc8e60

Browse files
KristofferCtkelman
authored andcommitted
deprecate sprandbool + add new syntax for creating random sparse matrix (#16098)
* deprecate sprandbool and add new syntax for creating random sparse matrix * remove documentation for sprandbool
1 parent f3bce71 commit bbc8e60

File tree

11 files changed

+52
-53
lines changed

11 files changed

+52
-53
lines changed

base/deprecated.jl

+7
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,13 @@ end
10751075
@deprecate copy(x::AbstractString) identity(x)
10761076
@deprecate copy(x::Tuple) identity(x)
10771077

1078+
@deprecate sprandbool(m::Integer, n::Integer, density::AbstractFloat) sprand(Bool, m, n, density)
1079+
@deprecate sprandbool(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) sprand(r, Bool, m, n, density)
1080+
@deprecate sprandbool(n::Integer, density::AbstractFloat) sprand(Bool, n, density)
1081+
@deprecate sprandbool(r::AbstractRNG, n::Integer, density::AbstractFloat) sprand(r, Bool, n, density)
1082+
@deprecate sprand{T}(n::Integer, density::AbstractFloat, ::Type{T}) sprand(T, n, density)
1083+
@deprecate sprand{T}(r::AbstractRNG, n::Integer, density::AbstractFloat, ::Type{T}) sprand(r, T, n, density)
1084+
10781085
# During the 0.5 development cycle, do not add any deprecations below this line
10791086
# To be deprecated in 0.6
10801087

base/exports.jl

-1
Original file line numberDiff line numberDiff line change
@@ -1459,7 +1459,6 @@ export
14591459
speye,
14601460
spones,
14611461
sprand,
1462-
sprandbool,
14631462
sprandn,
14641463
spzeros,
14651464
symperm,

base/sparse.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import Base.Broadcast: eltype_plus, broadcast_shape
3131
export AbstractSparseArray, AbstractSparseMatrix, AbstractSparseVector,
3232
SparseMatrixCSC, SparseVector, blkdiag, dense, droptol!, dropzeros!, etree,
3333
issparse, nonzeros, nzrange, rowvals, sparse, sparsevec, spdiagm, speye, spones,
34-
sprand, sprandbool, sprandn, spzeros, symperm, nnz
34+
sprand, sprandn, spzeros, symperm, nnz
3535

3636
include("sparse/abstractsparse.jl")
3737
include("sparse/sparsematrix.jl")

base/sparse/sparsematrix.jl

+15-18
Original file line numberDiff line numberDiff line change
@@ -894,12 +894,12 @@ function sprand_IJ(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloa
894894
end
895895

896896
"""
897-
sprand([rng],m,[n],p::AbstractFloat,[rfn])
897+
sprand([rng],[type],m,[n],p::AbstractFloat,[rfn])
898898
899899
Create a random length `m` sparse vector or `m` by `n` sparse matrix, in
900900
which the probability of any element being nonzero is independently given by
901901
`p` (and hence the mean density of nonzeros is also exactly `p`). Nonzero
902-
values are sampled from the distribution specified by `rfn`. The uniform
902+
values are sampled from the distribution specified by `rfn` and have the type `type`. The uniform
903903
distribution is used in case `rfn` is not specified. The optional `rng`
904904
argument specifies a random number generator, see [Random Numbers](:ref:`Random Numbers <random-numbers>`).
905905
"""
@@ -923,30 +923,27 @@ function sprand{T}(m::Integer, n::Integer, density::AbstractFloat,
923923
sparse_IJ_sorted!(I, J, rfn(length(I)), m, n, +) # it will never need to combine
924924
end
925925

926-
sprand(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,rand,Float64)
926+
truebools(r::AbstractRNG, n::Integer) = ones(Bool, n)
927+
927928
sprand(m::Integer, n::Integer, density::AbstractFloat) = sprand(GLOBAL_RNG,m,n,density)
928-
sprandn(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,randn,Float64)
929+
930+
sprand(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,rand,Float64)
931+
sprand{T}(r::AbstractRNG, ::Type{T}, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,(r, i) -> rand(r, T, i), T)
932+
sprand(r::AbstractRNG, ::Type{Bool}, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density, truebools, Bool)
933+
sprand{T}(::Type{T}, m::Integer, n::Integer, density::AbstractFloat) = sprand(GLOBAL_RNG, T, m, n, density)
934+
929935

930936
"""
931-
sprandn(m[,n],p::AbstractFloat)
937+
sprandn([rng], m[,n],p::AbstractFloat)
932938
933939
Create a random sparse vector of length `m` or sparse matrix of size `m` by `n`
934940
with the specified (independent) probability `p` of any entry being nonzero,
935-
where nonzero values are sampled from the normal distribution.
936-
"""
937-
sprandn( m::Integer, n::Integer, density::AbstractFloat) = sprandn(GLOBAL_RNG,m,n,density)
938-
939-
truebools(r::AbstractRNG, n::Integer) = ones(Bool, n)
940-
sprandbool(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,truebools,Bool)
941-
941+
where nonzero values are sampled from the normal distribution. The optional `rng`
942+
argument specifies a random number generator, see [Random Numbers](:ref:`Random Numbers <random-numbers>`).
942943
"""
943-
sprandbool(m[,n],p)
944+
sprandn(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,randn,Float64)
945+
sprandn(m::Integer, n::Integer, density::AbstractFloat) = sprandn(GLOBAL_RNG,m,n,density)
944946

945-
Create a random `m` by `n` sparse boolean matrix or length `m` sparse boolean
946-
vector with the specified (independent) probability `p` of any entry being
947-
`true`.
948-
"""
949-
sprandbool(m::Integer, n::Integer, density::AbstractFloat) = sprandbool(GLOBAL_RNG,m,n,density)
950947

951948
"""
952949
spones(S)

base/sparse/sparsevector.jl

+4-5
Original file line numberDiff line numberDiff line change
@@ -356,17 +356,16 @@ function sprand(r::AbstractRNG, n::Integer, p::AbstractFloat, rfn::Function)
356356
SparseVector(n, I, V)
357357
end
358358

359-
sprand{T}(n::Integer, p::AbstractFloat, ::Type{T}) = sprand(GLOBAL_RNG, n, p, rand, T)
360359
sprand(n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, n, p, rand)
361-
sprand{T}(r::AbstractRNG, n::Integer, p::AbstractFloat, ::Type{T}) = sprand(r, n, p, rand, T)
360+
362361
sprand(r::AbstractRNG, n::Integer, p::AbstractFloat) = sprand(r, n, p, rand)
362+
sprand{T}(r::AbstractRNG, ::Type{T}, n::Integer, p::AbstractFloat) = sprand(r, n, p, (r, i) -> rand(r, T, i))
363+
sprand(r::AbstractRNG, ::Type{Bool}, n::Integer, p::AbstractFloat) = sprand(r, n, p, truebools)
364+
sprand{T}(::Type{T}, n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, T, n, p)
363365

364366
sprandn(n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, n, p, randn)
365367
sprandn(r::AbstractRNG, n::Integer, p::AbstractFloat) = sprand(r, n, p, randn)
366368

367-
sprandbool(n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, n, p, truebools)
368-
sprandbool(r::AbstractRNG, n::Integer, p::AbstractFloat) = sprand(r, n, p, truebools)
369-
370369
## Indexing into Matrices can return SparseVectors
371370

372371
# Column slices

contrib/BBEditTextWrangler-julia.plist

-1
Original file line numberDiff line numberDiff line change
@@ -1070,7 +1070,6 @@
10701070
<string>splitext</string>
10711071
<string>spones</string>
10721072
<string>sprand</string>
1073-
<string>sprandbool</string>
10741073
<string>sprandn</string>
10751074
<string>sprint</string>
10761075
<string>spzeros</string>

doc/manual/arrays.rst

-5
Original file line numberDiff line numberDiff line change
@@ -800,8 +800,3 @@ reference.
800800
| | | distribution. (Requires the |
801801
| | | ``Distributions`` package.) |
802802
+----------------------------------------+----------------------------------+--------------------------------------------+
803-
| :func:`sprandbool(m,n,d) <sprandbool>` | :func:`rand(Bool,m,n) <rand>` | Creates a *m*-by-*n* random matrix (of |
804-
| | | density *d*) with non-zero ``Bool`` |
805-
| | | elements with probability *d* (*d* =0.5 |
806-
| | | for :func:`rand(Bool) <rand>`.) |
807-
+----------------------------------------+----------------------------------+--------------------------------------------+

doc/stdlib/arrays.rst

+4-10
Original file line numberDiff line numberDiff line change
@@ -940,23 +940,17 @@ dense counterparts. The following functions are specific to sparse arrays.
940940
941941
Construct a sparse diagonal matrix. ``B`` is a tuple of vectors containing the diagonals and ``d`` is a tuple containing the positions of the diagonals. In the case the input contains only one diagonal, ``B`` can be a vector (instead of a tuple) and ``d`` can be the diagonal position (instead of a tuple), defaulting to 0 (diagonal). Optionally, ``m`` and ``n`` specify the size of the resulting sparse matrix.
942942

943-
.. function:: sprand([rng],m,[n],p::AbstractFloat,[rfn])
943+
.. function:: sprand([rng],[type],m,[n],p::AbstractFloat,[rfn])
944944

945945
.. Docstring generated from Julia source
946946
947-
Create a random length ``m`` sparse vector or ``m`` by ``n`` sparse matrix, in which the probability of any element being nonzero is independently given by ``p`` (and hence the mean density of nonzeros is also exactly ``p``\ ). Nonzero values are sampled from the distribution specified by ``rfn``\ . The uniform distribution is used in case ``rfn`` is not specified. The optional ``rng`` argument specifies a random number generator, see :ref:`Random Numbers <random-numbers>`\ .
947+
Create a random length ``m`` sparse vector or ``m`` by ``n`` sparse matrix, in which the probability of any element being nonzero is independently given by ``p`` (and hence the mean density of nonzeros is also exactly ``p``\ ). Nonzero values are sampled from the distribution specified by ``rfn`` and have the type ``type``\ . The uniform distribution is used in case ``rfn`` is not specified. The optional ``rng`` argument specifies a random number generator, see :ref:`Random Numbers <random-numbers>`\ .
948948

949-
.. function:: sprandn(m[,n],p::AbstractFloat)
949+
.. function:: sprandn([rng], m[,n],p::AbstractFloat)
950950

951951
.. Docstring generated from Julia source
952952
953-
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.
954-
955-
.. function:: sprandbool(m[,n],p)
956-
957-
.. Docstring generated from Julia source
958-
959-
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``\ .
953+
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. The optional ``rng`` argument specifies a random number generator, see :ref:`Random Numbers <random-numbers>`\ .
960954

961955
.. function:: etree(A[, post])
962956

test/perf/sparse/getindex.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ function sparse_getindex_perf()
9595
c = counters[sz]
9696
if indstr=="logical array"
9797
# make a logical array of the right size
98-
ind = sprandbool(size(m,1)..., 1e-5)
98+
ind = sprand(Bool, size(m,1)..., 1e-5)
9999
end
100100
if nargs==2
101101
@timeit fun(m, ind) "sparse_getindex_$s1$c" "Sparse matrix with $ms, $funstr with $indstr"
@@ -120,7 +120,7 @@ function sparse_getindex_perf()
120120
continue # logical indexing with medium size sparse matrix takes too long
121121
end
122122
# make a logical array of the right size
123-
ind = sprandbool(size(m)..., 1e-5)
123+
ind = sprand(Bool, size(m)..., 1e-5)
124124
c = counters[sz]
125125
@timeit one_arg_indexing(m, ind) "sparse_getindex_$s1$c" "$s2 with $ms, linear indexing with $indstr"
126126
counters[sz] += 1

test/sparsedir/sparse.jl

+16-7
Original file line numberDiff line numberDiff line change
@@ -389,11 +389,11 @@ A = speye(Bool, 5)
389389
@test sprand(4,5,0.5).^0 == sparse(ones(4,5))
390390

391391
# issue #5985
392-
@test sprandbool(4, 5, 0.0) == sparse(zeros(Bool, 4, 5))
393-
@test sprandbool(4, 5, 1.00) == sparse(ones(Bool, 4, 5))
392+
@test sprand(Bool, 4, 5, 0.0) == sparse(zeros(Bool, 4, 5))
393+
@test sprand(Bool, 4, 5, 1.00) == sparse(ones(Bool, 4, 5))
394394
sprb45nnzs = zeros(5)
395395
for i=1:5
396-
sprb45 = sprandbool(4, 5, 0.5)
396+
sprb45 = sprand(Bool, 4, 5, 0.5)
397397
@test length(sprb45) == 20
398398
sprb45nnzs[i] = sum(sprb45)[1]
399399
end
@@ -589,7 +589,7 @@ let A = speye(Int, 5), I=1:10, X=reshape([trues(10); falses(15)],5,5)
589589
@test A[I] == A[X] == collect(1:10)
590590
end
591591

592-
let S = sprand(50, 30, 0.5, x->round(Int,rand(x)*100)), I = sprandbool(50, 30, 0.2)
592+
let S = sprand(50, 30, 0.5, x->round(Int,rand(x)*100)), I = sprand(Bool, 50, 30, 0.2)
593593
FS = full(S)
594594
FI = full(I)
595595
@test sparse(FS[FI]) == S[I] == S[FI]
@@ -973,7 +973,7 @@ end
973973
@test spzeros(1,2) .* spzeros(0,1) == zeros(0,2)
974974

975975
# test throws
976-
A = sprandbool(5,5,0.2)
976+
A = sprand(Bool, 5,5,0.2)
977977
@test_throws ArgumentError reinterpret(Complex128,A)
978978
@test_throws ArgumentError reinterpret(Complex128,A,(5,5))
979979
@test_throws DimensionMismatch reinterpret(Int8,A,(20,))
@@ -990,9 +990,9 @@ A = speye(5)
990990
@test convert(Matrix,A) == full(A)
991991

992992
# test float
993-
A = sprandbool(5,5,0.0)
993+
A = sprand(Bool, 5,5,0.0)
994994
@test eltype(float(A)) == Float64 # issue #11658
995-
A = sprandbool(5,5,0.2)
995+
A = sprand(Bool, 5,5,0.2)
996996
@test float(A) == float(full(A))
997997

998998
# test sparsevec
@@ -1319,3 +1319,12 @@ let
13191319
@test issparse(UpperTriangular(full(m))) == false
13201320
@test issparse(LinAlg.UnitUpperTriangular(full(m))) == false
13211321
end
1322+
1323+
let
1324+
m = sprand(Float32, 10, 10, 0.1)
1325+
@test eltype(m) == Float32
1326+
m = sprand(Float64, 10, 10, 0.1)
1327+
@test eltype(m) == Float64
1328+
m = sprand(Int32, 10, 10, 0.1)
1329+
@test eltype(m) == Int32
1330+
end

test/sparsedir/sparsevector.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ let xr = sprand(1000, 0.9)
129129
@test all(nonzeros(xr) .>= 0.0)
130130
end
131131

132-
let xr = sprand(1000, 0.9, Float32)
132+
let xr = sprand(Float32, 1000, 0.9)
133133
@test isa(xr, SparseVector{Float32,Int})
134134
@test length(xr) == 1000
135135
@test all(nonzeros(xr) .>= 0.0)
@@ -143,7 +143,7 @@ let xr = sprandn(1000, 0.9)
143143
end
144144
end
145145

146-
let xr = sprandbool(1000, 0.9)
146+
let xr = sprand(Bool, 1000, 0.9)
147147
@test isa(xr, SparseVector{Bool,Int})
148148
@test length(xr) == 1000
149149
@test all(nonzeros(xr))
@@ -152,7 +152,7 @@ end
152152
let r1 = MersenneTwister(), r2 = MersenneTwister()
153153
@test sprand(r1, 100, .9) == sprand(r2, 100, .9)
154154
@test sprandn(r1, 100, .9) == sprandn(r2, 100, .9)
155-
@test sprandbool(r1, 100, .9) == sprandbool(r2, 100, .9)
155+
@test sprand(r1, Bool, 100, .9, ) == sprand(r2, Bool, 100, .9)
156156
end
157157

158158
### Element access

0 commit comments

Comments
 (0)