From f6ab0c2d48dcf07c780aed58977bcc8050f281d2 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Thu, 28 Apr 2016 18:18:59 +0200 Subject: [PATCH 1/2] deprecate sprandbool and add new syntax for creating random sparse matrix --- base/deprecated.jl | 16 ++++++++++++++++ base/sparse/sparsematrix.jl | 33 +++++++++++++++------------------ base/sparse/sparsevector.jl | 9 ++++----- doc/stdlib/arrays.rst | 10 ++++++---- test/perf/sparse/getindex.jl | 4 ++-- test/sparsedir/sparse.jl | 23 ++++++++++++++++------- test/sparsedir/sparsevector.jl | 6 +++--- 7 files changed, 62 insertions(+), 39 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 1eb13ffcbdab2..bee35c18b4b48 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1078,6 +1078,22 @@ 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`. + +This function is deprecated. +""" +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, Bool, m, n, density) +@deprecate sprandbool(n::Integer, density::AbstractFloat) sprand(Bool, n, density) +@deprecate sprandbool(r::AbstractRNG, n::Integer, density::AbstractFloat) sprand(r, Bool, n, density) +@deprecate sprand{T}(n::Integer, density::AbstractFloat, ::Type{T}) sprand(T, n, density) +@deprecate sprand{T}(r::AbstractRNG, n::Integer, density::AbstractFloat, ::Type{T}) sprand(r, T, n, density) + # 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 a2d33ee3dbbea..9e3e9e7d0dda0 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -894,12 +894,12 @@ function sprand_IJ(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloa end """ - sprand([rng],m,[n],p::AbstractFloat,[rfn]) + sprand([rng],[type],m,[n],p::AbstractFloat,[rfn]) 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 +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 [Random Numbers](:ref:`Random Numbers `). """ @@ -923,30 +923,27 @@ 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(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,rand,Float64) +truebools(r::AbstractRNG, n::Integer) = ones(Bool, n) + 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) + +sprand(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,rand,Float64) +sprand{T}(r::AbstractRNG, ::Type{T}, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,(r, i) -> rand(r, T, i), T) +sprand(r::AbstractRNG, ::Type{Bool}, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density, truebools, Bool) +sprand{T}(::Type{T}, m::Integer, n::Integer, density::AbstractFloat) = sprand(GLOBAL_RNG, T, m, n, density) + """ - sprandn(m[,n],p::AbstractFloat) + sprandn([rng], 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, -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) - +where nonzero values are sampled from the normal distribution. The optional `rng` +argument specifies a random number generator, see [Random Numbers](:ref:`Random Numbers `). """ - sprandbool(m[,n],p) +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) -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) """ spones(S) diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index b8c2c87c04d22..4600f1a978aab 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -356,17 +356,16 @@ function sprand(r::AbstractRNG, n::Integer, p::AbstractFloat, rfn::Function) SparseVector(n, I, V) end -sprand{T}(n::Integer, p::AbstractFloat, ::Type{T}) = sprand(GLOBAL_RNG, n, p, rand, T) 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) = sprand(r, n, p, rand) +sprand{T}(r::AbstractRNG, ::Type{T}, n::Integer, p::AbstractFloat) = sprand(r, n, p, (r, i) -> rand(r, T, i)) +sprand(r::AbstractRNG, ::Type{Bool}, n::Integer, p::AbstractFloat) = sprand(r, n, p, truebools) +sprand{T}(::Type{T}, n::Integer, p::AbstractFloat) = sprand(GLOBAL_RNG, T, n, p) 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/doc/stdlib/arrays.rst b/doc/stdlib/arrays.rst index 65b4e4c33d7e6..4c2cfb219b28d 100644 --- a/doc/stdlib/arrays.rst +++ b/doc/stdlib/arrays.rst @@ -940,17 +940,17 @@ dense counterparts. The following functions are specific to sparse arrays. 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. -.. function:: sprand([rng],m,[n],p::AbstractFloat,[rfn]) +.. function:: sprand([rng],[type],m,[n],p::AbstractFloat,[rfn]) .. Docstring generated from Julia source - 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 `\ . + 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 `\ . -.. function:: sprandn(m[,n],p::AbstractFloat) +.. function:: sprandn([rng], m[,n],p::AbstractFloat) .. Docstring generated from Julia source - 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. + 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 `\ . .. function:: sprandbool(m[,n],p) @@ -958,6 +958,8 @@ dense counterparts. The following functions are specific to sparse arrays. 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``\ . + This function is deprecated. + .. function:: etree(A[, post]) .. Docstring generated from Julia source 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 9252c212e497d..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 @@ -1319,3 +1319,12 @@ let @test issparse(UpperTriangular(full(m))) == false @test issparse(LinAlg.UnitUpperTriangular(full(m))) == false end + +let + m = sprand(Float32, 10, 10, 0.1) + @test eltype(m) == Float32 + m = sprand(Float64, 10, 10, 0.1) + @test eltype(m) == Float64 + m = sprand(Int32, 10, 10, 0.1) + @test eltype(m) == Int32 +end diff --git a/test/sparsedir/sparsevector.jl b/test/sparsedir/sparsevector.jl index 07d00954372ad..358bfd463a38f 100644 --- a/test/sparsedir/sparsevector.jl +++ b/test/sparsedir/sparsevector.jl @@ -129,7 +129,7 @@ let xr = sprand(1000, 0.9) @test all(nonzeros(xr) .>= 0.0) end -let xr = sprand(1000, 0.9, Float32) +let xr = sprand(Float32, 1000, 0.9) @test isa(xr, SparseVector{Float32,Int}) @test length(xr) == 1000 @test all(nonzeros(xr) .>= 0.0) @@ -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, Bool, 100, .9, ) == sprand(r2, Bool, 100, .9) end ### Element access From 8044d1dd26315187a0e773e8a847cc1602cdd2d0 Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Fri, 29 Apr 2016 17:55:36 +0200 Subject: [PATCH 2/2] remove documentation for sprandbool --- base/deprecated.jl | 9 --------- base/exports.jl | 1 - base/sparse.jl | 2 +- contrib/BBEditTextWrangler-julia.plist | 1 - doc/manual/arrays.rst | 5 ----- doc/stdlib/arrays.rst | 8 -------- 6 files changed, 1 insertion(+), 25 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index bee35c18b4b48..61e8c57754fc5 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1078,15 +1078,6 @@ 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`. - -This function is deprecated. -""" -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, Bool, m, n, density) @deprecate sprandbool(n::Integer, density::AbstractFloat) sprand(Bool, n, density) diff --git a/base/exports.jl b/base/exports.jl index 1b9ae06611f93..9ffc4fb19b621 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1459,7 +1459,6 @@ export speye, spones, sprand, - sprandbool, sprandn, spzeros, symperm, diff --git a/base/sparse.jl b/base/sparse.jl index 4a16213bda971..9c7a4cc446558 100644 --- a/base/sparse.jl +++ b/base/sparse.jl @@ -31,7 +31,7 @@ import Base.Broadcast: eltype_plus, broadcast_shape export AbstractSparseArray, AbstractSparseMatrix, AbstractSparseVector, SparseMatrixCSC, SparseVector, blkdiag, dense, droptol!, dropzeros!, etree, issparse, nonzeros, nzrange, rowvals, sparse, sparsevec, spdiagm, speye, spones, - sprand, sprandbool, sprandn, spzeros, symperm, nnz + sprand, sprandn, spzeros, symperm, nnz include("sparse/abstractsparse.jl") include("sparse/sparsematrix.jl") diff --git a/contrib/BBEditTextWrangler-julia.plist b/contrib/BBEditTextWrangler-julia.plist index dce831b314675..7fc4c997a8fca 100644 --- a/contrib/BBEditTextWrangler-julia.plist +++ b/contrib/BBEditTextWrangler-julia.plist @@ -1070,7 +1070,6 @@ splitext spones sprand - sprandbool sprandn sprint spzeros diff --git a/doc/manual/arrays.rst b/doc/manual/arrays.rst index 59c79149b1f79..8acd638b6ecf9 100644 --- a/doc/manual/arrays.rst +++ b/doc/manual/arrays.rst @@ -800,8 +800,3 @@ reference. | | | distribution. (Requires the | | | | ``Distributions`` package.) | +----------------------------------------+----------------------------------+--------------------------------------------+ -| :func:`sprandbool(m,n,d) ` | :func:`rand(Bool,m,n) ` | Creates a *m*-by-*n* random matrix (of | -| | | density *d*) with non-zero ``Bool`` | -| | | elements with probability *d* (*d* =0.5 | -| | | for :func:`rand(Bool) `.) | -+----------------------------------------+----------------------------------+--------------------------------------------+ diff --git a/doc/stdlib/arrays.rst b/doc/stdlib/arrays.rst index 4c2cfb219b28d..436c88282f002 100644 --- a/doc/stdlib/arrays.rst +++ b/doc/stdlib/arrays.rst @@ -952,14 +952,6 @@ dense counterparts. The following functions are specific to sparse arrays. 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 `\ . -.. function:: sprandbool(m[,n],p) - - .. Docstring generated from Julia source - - 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``\ . - - This function is deprecated. - .. function:: etree(A[, post]) .. Docstring generated from Julia source