Skip to content

Commit bb8f554

Browse files
committed
at-deprecate spones(A) fillstored!(copy(A), 1) [ci skip]
1 parent bc2e1ab commit bb8f554

File tree

10 files changed

+18
-38
lines changed

10 files changed

+18
-38
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,9 @@ Deprecated or removed
716716
have been deprecated in favor of `spdiagm(d => x)` and `spdiagm(d[1] => x[1], d[2] => x[2], ...)`
717717
respectively. The new `spdiagm` implementation now always returns a square matrix ([#23757]).
718718

719+
* `spones(A::AbstractSparseArray)` has been deprecated in favor of
720+
`LinAlg.fillstored!(copy(A), 1)` ([#25037]).
721+
719722
* Constructors for `LibGit2.UserPasswordCredentials` and `LibGit2.SSHCredentials` which take a
720723
`prompt_if_incorrect` argument are deprecated. Instead, prompting behavior is controlled using
721724
the `allow_prompt` keyword in the `LibGit2.CredentialPayload` constructor ([#23690]).

base/deprecated.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1898,6 +1898,12 @@ end
18981898
# PR #25030
18991899
@eval LinAlg @deprecate fillslots! fillstored! false
19001900

1901+
# PR #TBD
1902+
@eval SparseArrays @deprecate spones(A::SparseMatrixCSC{T}) where {T} fillstored!(copy(A), one(T))
1903+
@eval SparseArrays @deprecate spones(A::SparseVector{T}) where {T} fillstored!(copy(A), one(T))
1904+
using .SparseArrays.spones
1905+
export spones
1906+
19011907
function diagm(v::BitVector)
19021908
depwarn(string("diagm(v::BitVector) is deprecated, use diagm(0 => v) or ",
19031909
"BitMatrix(Diagonal(v)) instead"), :diagm)

base/exports.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,6 @@ export
12391239
sparse,
12401240
sparsevec,
12411241
spdiagm,
1242-
spones,
12431242
sprand,
12441243
sprandn,
12451244
spzeros,

base/sparse/sparse.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module SparseArrays
77

88
using Base: ReshapedArray, promote_op, setindex_shape_check, to_shape, tail
99
using Base.Sort: Forward
10-
using Base.LinAlg: AbstractTriangular, PosDefException
10+
using Base.LinAlg: AbstractTriangular, PosDefException, fillstored!
1111

1212
import Base: +, -, *, \, /, &, |, xor, ==
1313
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,
3131

3232
export AbstractSparseArray, AbstractSparseMatrix, AbstractSparseVector,
3333
SparseMatrixCSC, SparseVector, blkdiag, droptol!, dropzeros!, dropzeros,
34-
issparse, nonzeros, nzrange, rowvals, sparse, sparsevec, spdiagm, spones,
34+
issparse, nonzeros, nzrange, rowvals, sparse, sparsevec, spdiagm,
3535
sprand, sprandn, spzeros, nnz, permute
3636

3737
include("abstractsparse.jl")

base/sparse/sparsematrix.jl

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1431,31 +1431,7 @@ julia> sprandn(rng, 2, 2, 0.75)
14311431
sprandn(r::AbstractRNG, m::Integer, n::Integer, density::AbstractFloat) = sprand(r,m,n,density,randn,Float64)
14321432
sprandn(m::Integer, n::Integer, density::AbstractFloat) = sprandn(GLOBAL_RNG,m,n,density)
14331433

1434-
"""
1435-
spones(S)
1436-
1437-
Create a sparse array with the same structure as that of `S`, but with every nonzero
1438-
element having the value `1.0`.
1439-
1440-
# Examples
1441-
```jldoctest
1442-
julia> A = sparse([1,2,3,4],[2,4,3,1],[5.,4.,3.,2.])
1443-
4×4 SparseMatrixCSC{Float64,Int64} with 4 stored entries:
1444-
[4, 1] = 2.0
1445-
[1, 2] = 5.0
1446-
[3, 3] = 3.0
1447-
[2, 4] = 4.0
1448-
1449-
julia> spones(A)
1450-
4×4 SparseMatrixCSC{Float64,Int64} with 4 stored entries:
1451-
[4, 1] = 1.0
1452-
[1, 2] = 1.0
1453-
[3, 3] = 1.0
1454-
[2, 4] = 1.0
1455-
```
1456-
"""
1457-
spones(S::SparseMatrixCSC{T}) where {T} =
1458-
SparseMatrixCSC(S.m, S.n, copy(S.colptr), copy(S.rowval), ones(T, S.colptr[end]-1))
1434+
LinAlg.fillstored!(S::SparseMatrixCSC, x) = (fill!(view(S.nzval, 1:(S.colptr[S.n + 1] - 1)), x); S)
14591435

14601436
"""
14611437
spzeros([type,]m[,n])

base/sparse/sparsevector.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ spzeros(len::Integer) = spzeros(Float64, len)
100100
spzeros(::Type{T}, len::Integer) where {T} = SparseVector(len, Int[], T[])
101101
spzeros(::Type{Tv}, ::Type{Ti}, len::Integer) where {Tv,Ti<:Integer} = SparseVector(len, Ti[], Tv[])
102102

103-
# Construction of same structure, but with all ones
104-
spones(x::SparseVector{T}) where {T} = SparseVector(x.n, copy(x.nzind), ones(T, length(x.nzval)))
103+
LinAlg.fillstored!(x::SparseVector, y) = (fill!(x.nzval, y); x)
105104

106105
### Construction from lists of indices and values
107106

doc/src/manual/arrays.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,6 @@ section of the standard library reference.
883883
| Sparse | Dense | Description |
884884
|:-------------------------- |:---------------------- |:--------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
885885
| [`spzeros(m,n)`](@ref) | [`zeros(m,n)`](@ref) | Creates a *m*-by-*n* matrix of zeros. ([`spzeros(m,n)`](@ref) is empty.) |
886-
| [`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*. |
887886
| [`sparse(I, n, n)`](@ref) | [`Matrix(I,n,n)`](@ref)| Creates a *n*-by-*n* identity matrix. |
888887
| [`Array(S)`](@ref) | [`sparse(A)`](@ref) | Interconverts between dense and sparse formats. |
889888
| [`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)``. |

doc/src/stdlib/arrays.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ Base.SparseArrays.sparsevec
198198
Base.SparseArrays.issparse
199199
Base.SparseArrays.nnz
200200
Base.SparseArrays.spzeros
201-
Base.SparseArrays.spones
202201
Base.SparseArrays.spdiagm
203202
Base.SparseArrays.sprand
204203
Base.SparseArrays.sprandn

test/sparse/sparse.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,8 +1739,8 @@ end
17391739
end
17401740
end
17411741

1742-
@testset "spones" begin
1743-
@test spones(sparse(2.0I, 5, 5)) == Matrix(I, 5, 5)
1742+
@testset "fillstored!" begin
1743+
@test LinAlg.fillstored!(sparse(2.0I, 5, 5), 1) == Matrix(I, 5, 5)
17441744
end
17451745

17461746
@testset "factorization" begin
@@ -2016,7 +2016,7 @@ end
20162016
sprand(5, 5, 1/5)
20172017
end
20182018
A = max.(A, A')
2019-
A = spones(A)
2019+
LinAlg.fillstored!(A, 1)
20202020
B = A[5:-1:1, 5:-1:1]
20212021
@test issymmetric(B)
20222022
end

test/sparse/sparsevector.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,9 @@ end
121121
@test exact_equal(sparsevec(d), SparseVector(3, [1, 2, 3], [0.0, 1.0, 2.0]))
122122
end
123123
end
124-
@testset "spones" begin
125-
# copies structure, but replaces nzvals with ones
124+
@testset "fillstored!" begin
126125
x = SparseVector(8, [2, 3, 6], [12.0, 18.0, 25.0])
127-
y = spones(x)
126+
y = LinAlg.fillstored!(copy(x), 1)
128127
@test (x .!= 0) == (y .!= 0)
129128
@test y == SparseVector(8, [2, 3, 6], [1.0, 1.0, 1.0])
130129
end

0 commit comments

Comments
 (0)