diff --git a/NEWS.md b/NEWS.md index c59ce539d679c2..26d007889ee8f4 100644 --- a/NEWS.md +++ b/NEWS.md @@ -704,6 +704,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{T})` has been deprecated in favor of + `LinAlg.fillstored!(copy(A), one(T))` ([#TBD]). + * 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 66d210098c6643..559224254274a3 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1861,6 +1861,12 @@ end # PR #25030 @eval LinAlg @deprecate fillslots! fillstored! false +# PR #TBD +@eval SparseArrays @deprecate spones(A::SparseMatrixCSC{T}) where {T} fillstored!(copy(A), one(T)) +@eval SparseArrays @deprecate spones(A::SparseVector{T}) where {T} fillstored!(copy(A), one(T)) +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 ed3f5d2b34b499..79e00d38e79b77 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -1258,7 +1258,6 @@ export sparse, sparsevec, spdiagm, - spones, sprand, sprandn, spzeros, diff --git a/base/sparse/sparse.jl b/base/sparse/sparse.jl index 0b41b9e0731136..3de4101cda5257 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, fillstored!, sprand, sprandn, spzeros, nnz, permute include("abstractsparse.jl") diff --git a/base/sparse/sparsematrix.jl b/base/sparse/sparsematrix.jl index 0567b91758877c..b3980d87782a58 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!(S.nzval, x); S) """ spzeros([type,]m[,n]) diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index 4cb4f3ce8f3b73..1c2c1ccbfd9b57 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -99,8 +99,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{T}, y) where {T} = (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 87d1883dbc8dee..9a87815fc13c5c 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 c39546121a7c9f..08c97c71c4da99 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 9bb22f6626dfa0..04240da928e023 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -1737,8 +1737,8 @@ end end end -@testset "spones" begin - @test spones(sparse(2.0I, 5, 5)) == Matrix(I, 5, 5) +@testset "fillstored!" begin + @test SparseArrays.fillstored!(sparse(2.0I, 5, 5)) == Matrix(I, 5, 5) end @testset "factorization" begin @@ -2014,7 +2014,7 @@ end sprand(5, 5, 1/5) end A = max.(A, A') - A = spones(A) + SparseArrays.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 47fc747dba83b9..49dbaacf41fe53 100644 --- a/test/sparse/sparsevector.jl +++ b/test/sparse/sparsevector.jl @@ -119,10 +119,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 = SparseArrays.fillstored!(copy(x)) @test (x .!= 0) == (y .!= 0) @test y == SparseVector(8, [2, 3, 6], [1.0, 1.0, 1.0]) end