From 462ee646b190a11828d5a5955c4794d5b8557103 Mon Sep 17 00:00:00 2001 From: Fredrik Ekre Date: Fri, 15 Dec 2017 08:40:52 +0100 Subject: [PATCH] at-deprecate spones(A) fillstored!(copy(A), 1) --- NEWS.md | 3 +++ base/deprecated.jl | 6 ++++++ base/exports.jl | 1 - base/sparse/sparse.jl | 4 ++-- base/sparse/sparsematrix.jl | 26 +------------------------- base/sparse/sparsevector.jl | 3 +-- doc/src/manual/arrays.md | 1 - doc/src/stdlib/arrays.md | 1 - test/sparse/sparse.jl | 6 +++--- test/sparse/sparsevector.jl | 5 ++--- 10 files changed, 18 insertions(+), 38 deletions(-) diff --git a/NEWS.md b/NEWS.md index 0f7da3162e200..d8263eae946b3 100644 --- a/NEWS.md +++ b/NEWS.md @@ -716,6 +716,9 @@ Deprecated or removed have been deprecated in favor of `spdiagm(d => x)` and `spdiagm(d[1] => x[1], d[2] => x[2], ...)` respectively. The new `spdiagm` implementation now always returns a square matrix ([#23757]). + * `spones(A::AbstractSparseArray)` has been deprecated in favor of + `LinAlg.fillstored!(copy(A), 1)` ([#25037]). + * Constructors for `LibGit2.UserPasswordCredentials` and `LibGit2.SSHCredentials` which take a `prompt_if_incorrect` argument are deprecated. Instead, prompting behavior is controlled using the `allow_prompt` keyword in the `LibGit2.CredentialPayload` constructor ([#23690]). diff --git a/base/deprecated.jl b/base/deprecated.jl index 860f839acce0a..c884c4588c768 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1898,6 +1898,12 @@ end # PR #25030 @eval LinAlg @deprecate fillslots! fillstored! false +# PR #25037 +@eval SparseArrays @deprecate spones(A::SparseMatrixCSC) fillstored!(copy(A), 1) +@eval SparseArrays @deprecate spones(A::SparseVector) fillstored!(copy(A), 1) +using .SparseArrays.spones +export spones + function diagm(v::BitVector) depwarn(string("diagm(v::BitVector) is deprecated, use diagm(0 => v) or ", "BitMatrix(Diagonal(v)) instead"), :diagm) diff --git a/base/exports.jl b/base/exports.jl index 3d2c355994b1d..8220c3ae1b2f9 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1239,7 +1239,6 @@ export sparse, sparsevec, spdiagm, - spones, sprand, sprandn, spzeros, diff --git a/base/sparse/sparse.jl b/base/sparse/sparse.jl index dcc9665c20fd6..e65f9cb4006f5 100644 --- a/base/sparse/sparse.jl +++ b/base/sparse/sparse.jl @@ -7,7 +7,7 @@ module SparseArrays using Base: ReshapedArray, promote_op, setindex_shape_check, to_shape, tail using Base.Sort: Forward -using Base.LinAlg: AbstractTriangular, PosDefException +using Base.LinAlg: AbstractTriangular, PosDefException, fillstored! import Base: +, -, *, \, /, &, |, xor, == import Base: A_mul_B!, Ac_mul_B, Ac_mul_B!, At_mul_B, At_mul_B! @@ -31,7 +31,7 @@ import Base: @get!, acos, acosd, acot, acotd, acsch, asech, asin, asind, asinh, export AbstractSparseArray, AbstractSparseMatrix, AbstractSparseVector, SparseMatrixCSC, SparseVector, blkdiag, droptol!, dropzeros!, dropzeros, - issparse, nonzeros, nzrange, rowvals, sparse, sparsevec, spdiagm, spones, + issparse, nonzeros, nzrange, rowvals, sparse, sparsevec, spdiagm, sprand, sprandn, spzeros, nnz, permute include("abstractsparse.jl") diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index f0a80e8d9da62..4d726ab4bf581 100644 --- a/base/sparse/sparsematrix.jl +++ b/base/sparse/sparsematrix.jl @@ -1431,31 +1431,7 @@ julia> sprandn(rng, 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) -""" - spones(S) - -Create a sparse array with the same structure as that of `S`, but with every nonzero -element having the value `1.0`. - -# Examples -```jldoctest -julia> A = sparse([1,2,3,4],[2,4,3,1],[5.,4.,3.,2.]) -4×4 SparseMatrixCSC{Float64,Int64} with 4 stored entries: - [4, 1] = 2.0 - [1, 2] = 5.0 - [3, 3] = 3.0 - [2, 4] = 4.0 - -julia> spones(A) -4×4 SparseMatrixCSC{Float64,Int64} with 4 stored entries: - [4, 1] = 1.0 - [1, 2] = 1.0 - [3, 3] = 1.0 - [2, 4] = 1.0 -``` -""" -spones(S::SparseMatrixCSC{T}) where {T} = - SparseMatrixCSC(S.m, S.n, copy(S.colptr), copy(S.rowval), ones(T, S.colptr[end]-1)) +LinAlg.fillstored!(S::SparseMatrixCSC, x) = (fill!(view(S.nzval, 1:(S.colptr[S.n + 1] - 1)), x); S) """ spzeros([type,]m[,n]) diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index 293a9d5925954..110a63c991292 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -100,8 +100,7 @@ spzeros(len::Integer) = spzeros(Float64, len) spzeros(::Type{T}, len::Integer) where {T} = SparseVector(len, Int[], T[]) spzeros(::Type{Tv}, ::Type{Ti}, len::Integer) where {Tv,Ti<:Integer} = SparseVector(len, Ti[], Tv[]) -# Construction of same structure, but with all ones -spones(x::SparseVector{T}) where {T} = SparseVector(x.n, copy(x.nzind), ones(T, length(x.nzval))) +LinAlg.fillstored!(x::SparseVector, y) = (fill!(x.nzval, y); x) ### Construction from lists of indices and values diff --git a/doc/src/manual/arrays.md b/doc/src/manual/arrays.md index 8bbef72d07df6..2cc353e724c64 100644 --- a/doc/src/manual/arrays.md +++ b/doc/src/manual/arrays.md @@ -883,7 +883,6 @@ section of the standard library reference. | Sparse | Dense | Description | |:-------------------------- |:---------------------- |:--------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | [`spzeros(m,n)`](@ref) | [`zeros(m,n)`](@ref) | Creates a *m*-by-*n* matrix of zeros. ([`spzeros(m,n)`](@ref) is empty.) | -| [`spones(S)`](@ref) | [`ones(m,n)`](@ref) | Creates a matrix filled with ones. Unlike the dense version, [`spones`](@ref) has the same sparsity pattern as *S*. | | [`sparse(I, n, n)`](@ref) | [`Matrix(I,n,n)`](@ref)| Creates a *n*-by-*n* identity matrix. | | [`Array(S)`](@ref) | [`sparse(A)`](@ref) | Interconverts between dense and sparse formats. | | [`sprand(m,n,d)`](@ref) | [`rand(m,n)`](@ref) | Creates a *m*-by-*n* random matrix (of density *d*) with iid non-zero elements distributed uniformly on the half-open interval ``[0, 1)``. | diff --git a/doc/src/stdlib/arrays.md b/doc/src/stdlib/arrays.md index 81d4d7d62e9f9..7f8f45ed80bfe 100644 --- a/doc/src/stdlib/arrays.md +++ b/doc/src/stdlib/arrays.md @@ -198,7 +198,6 @@ Base.SparseArrays.sparsevec Base.SparseArrays.issparse Base.SparseArrays.nnz Base.SparseArrays.spzeros -Base.SparseArrays.spones Base.SparseArrays.spdiagm Base.SparseArrays.sprand Base.SparseArrays.sprandn diff --git a/test/sparse/sparse.jl b/test/sparse/sparse.jl index c3b32de0c8bc5..3e8e5d14fd085 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -1739,8 +1739,8 @@ end end end -@testset "spones" begin - @test spones(sparse(2.0I, 5, 5)) == Matrix(I, 5, 5) +@testset "fillstored!" begin + @test LinAlg.fillstored!(sparse(2.0I, 5, 5), 1) == Matrix(I, 5, 5) end @testset "factorization" begin @@ -2016,7 +2016,7 @@ end sprand(5, 5, 1/5) end A = max.(A, A') - A = spones(A) + LinAlg.fillstored!(A, 1) B = A[5:-1:1, 5:-1:1] @test issymmetric(B) end diff --git a/test/sparse/sparsevector.jl b/test/sparse/sparsevector.jl index be73dd551d482..0c6691b08aeb5 100644 --- a/test/sparse/sparsevector.jl +++ b/test/sparse/sparsevector.jl @@ -121,10 +121,9 @@ end @test exact_equal(sparsevec(d), SparseVector(3, [1, 2, 3], [0.0, 1.0, 2.0])) end end - @testset "spones" begin - # copies structure, but replaces nzvals with ones + @testset "fillstored!" begin x = SparseVector(8, [2, 3, 6], [12.0, 18.0, 25.0]) - y = spones(x) + y = LinAlg.fillstored!(copy(x), 1) @test (x .!= 0) == (y .!= 0) @test y == SparseVector(8, [2, 3, 6], [1.0, 1.0, 1.0]) end