Skip to content

Commit 28a3312

Browse files
vtjnashKlausC
andauthored
[SparseArrays] similar on sparse matrix returned uninitialized space (#40444)
Co-authored-by: Klaus Crusius <[email protected]>
1 parent 671bccb commit 28a3312

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

stdlib/SparseArrays/src/sparsematrix.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -469,17 +469,17 @@ function _sparsesimilar(S::AbstractSparseMatrixCSC, ::Type{TvNew}, ::Type{TiNew}
469469
newrowval = copyto!(similar(rowvals(S), TiNew), rowvals(S))
470470
return SparseMatrixCSC(size(S, 1), size(S, 2), newcolptr, newrowval, similar(nonzeros(S), TvNew))
471471
end
472-
# parent methods for similar that preserves only storage space (for when new and old dims differ)
472+
# parent methods for similar that preserves only storage space (for when new dims are 2-d)
473473
_sparsesimilar(S::AbstractSparseMatrixCSC, ::Type{TvNew}, ::Type{TiNew}, dims::Dims{2}) where {TvNew,TiNew} =
474-
SparseMatrixCSC(dims..., fill(one(TiNew), last(dims)+1), similar(rowvals(S), TiNew), similar(nonzeros(S), TvNew))
475-
# parent method for similar that allocates an empty sparse vector (when new dims are single)
474+
SparseMatrixCSC(dims..., fill(one(TiNew), last(dims)+1), similar(rowvals(S), TiNew, 0), similar(nonzeros(S), TvNew, 0))
475+
# parent method for similar that allocates an empty sparse vector (for when new dims are 1-d)
476476
_sparsesimilar(S::AbstractSparseMatrixCSC, ::Type{TvNew}, ::Type{TiNew}, dims::Dims{1}) where {TvNew,TiNew} =
477477
SparseVector(dims..., similar(rowvals(S), TiNew, 0), similar(nonzeros(S), TvNew, 0))
478-
#
478+
479479
# The following methods hook into the AbstractArray similar hierarchy. The first method
480480
# covers similar(A[, Tv]) calls, which preserve stored-entry structure, and the latter
481-
# methods cover similar(A[, Tv], shape...) calls, which preserve storage space when the shape
482-
# calls for a two-dimensional result.
481+
# methods cover similar(A[, Tv], shape...) calls, which partially preserve
482+
# storage space when the shape calls for a two-dimensional result.
483483
similar(S::AbstractSparseMatrixCSC{<:Any,Ti}, ::Type{TvNew}) where {Ti,TvNew} = _sparsesimilar(S, TvNew, Ti)
484484
similar(S::AbstractSparseMatrixCSC{<:Any,Ti}, ::Type{TvNew}, dims::Union{Dims{1},Dims{2}}) where {Ti,TvNew} =
485485
_sparsesimilar(S, TvNew, Ti, dims)

stdlib/SparseArrays/src/sparsevector.jl

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,24 @@ indtype(x::SparseVectorView) = indtype(parent(x))
8989
# parent method for similar that preserves stored-entry structure (for when new and old dims match)
9090
_sparsesimilar(S::SparseVector, ::Type{TvNew}, ::Type{TiNew}) where {TvNew,TiNew} =
9191
SparseVector(length(S), copyto!(similar(nonzeroinds(S), TiNew), nonzeroinds(S)), similar(nonzeros(S), TvNew))
92-
# parent method for similar that preserves nothing (for when old and new dims differ, and new is 1d)
92+
# parent method for similar that preserves nothing (for when new dims are 1-d)
9393
_sparsesimilar(S::SparseVector, ::Type{TvNew}, ::Type{TiNew}, dims::Dims{1}) where {TvNew,TiNew} =
9494
SparseVector(dims..., similar(nonzeroinds(S), TiNew, 0), similar(nonzeros(S), TvNew, 0))
95-
# parent method for similar that preserves storage space (for old and new dims differ, and new is 2d)
95+
# parent method for similar that preserves storage space (for when new dims are 2-d)
9696
_sparsesimilar(S::SparseVector, ::Type{TvNew}, ::Type{TiNew}, dims::Dims{2}) where {TvNew,TiNew} =
97-
SparseMatrixCSC(dims..., fill(one(TiNew), last(dims)+1), similar(nonzeroinds(S), TiNew), similar(nonzeros(S), TvNew))
97+
SparseMatrixCSC(dims..., fill(one(TiNew), last(dims)+1), similar(nonzeroinds(S), TiNew, 0), similar(nonzeros(S), TvNew, 0))
98+
9899
# The following methods hook into the AbstractArray similar hierarchy. The first method
99100
# covers similar(A[, Tv]) calls, which preserve stored-entry structure, and the latter
100101
# methods cover similar(A[, Tv], shape...) calls, which preserve nothing if the dims
101-
# specify a SparseVector result and storage space if the dims specify a SparseMatrixCSC result.
102+
# specify a SparseVector or a SparseMatrixCSC result.
102103
similar(S::SparseVector{<:Any,Ti}, ::Type{TvNew}) where {Ti,TvNew} =
103104
_sparsesimilar(S, TvNew, Ti)
104105
similar(S::SparseVector{<:Any,Ti}, ::Type{TvNew}, dims::Union{Dims{1},Dims{2}}) where {Ti,TvNew} =
105106
_sparsesimilar(S, TvNew, Ti, dims)
106107
# The following methods cover similar(A, Tv, Ti[, shape...]) calls, which specify the
107108
# result's index type in addition to its entry type, and aren't covered by the hooks above.
108-
# The calls without shape again preserve stored-entry structure, whereas those with
109-
# one-dimensional shape preserve nothing, and those with two-dimensional shape
110-
# preserve storage space.
109+
# The calls without shape again preserve stored-entry structure but no storage space.
111110
similar(S::SparseVector, ::Type{TvNew}, ::Type{TiNew}) where{TvNew,TiNew} =
112111
_sparsesimilar(S, TvNew, TiNew)
113112
similar(S::SparseVector, ::Type{TvNew}, ::Type{TiNew}, dims::Union{Dims{1},Dims{2}}) where {TvNew,TiNew} =

stdlib/SparseArrays/test/sparse.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2487,22 +2487,22 @@ end
24872487
@test typeof(simA) == typeof(A)
24882488
@test size(simA) == (6,6)
24892489
@test getcolptr(simA) == fill(1, 6+1)
2490-
@test length(rowvals(simA)) == length(rowvals(A))
2491-
@test length(nonzeros(simA)) == length(nonzeros(A))
2492-
# test similar with entry type and Dims{2} specification (preserves storage space only)
2490+
@test length(rowvals(simA)) == 0
2491+
@test length(nonzeros(simA)) == 0
2492+
# test similar with entry type and Dims{2} specification (empty storage space)
24932493
simA = similar(A, Float32, (6,6))
24942494
@test typeof(simA) == SparseMatrixCSC{Float32,eltype(getcolptr(A))}
24952495
@test size(simA) == (6,6)
24962496
@test getcolptr(simA) == fill(1, 6+1)
2497-
@test length(rowvals(simA)) == length(rowvals(A))
2498-
@test length(nonzeros(simA)) == length(nonzeros(A))
2497+
@test length(rowvals(simA)) == 0
2498+
@test length(nonzeros(simA)) == 0
24992499
# test similar with entry type, index type, and Dims{2} specification (preserves storage space only)
25002500
simA = similar(A, Float32, Int8, (6,6))
25012501
@test typeof(simA) == SparseMatrixCSC{Float32, Int8}
25022502
@test size(simA) == (6,6)
25032503
@test getcolptr(simA) == fill(1, 6+1)
2504-
@test length(rowvals(simA)) == length(rowvals(A))
2505-
@test length(nonzeros(simA)) == length(nonzeros(A))
2504+
@test length(rowvals(simA)) == 0
2505+
@test length(nonzeros(simA)) == 0
25062506
# test similar with Dims{1} specification (preserves nothing)
25072507
simA = similar(A, (6,))
25082508
@test typeof(simA) == SparseVector{eltype(nonzeros(A)),eltype(getcolptr(A))}

stdlib/SparseArrays/test/sparsevector.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1424,22 +1424,22 @@ end
14241424
@test typeof(simA) == SparseMatrixCSC{eltype(nonzeros(A)),eltype(nonzeroinds(A))}
14251425
@test size(simA) == (6,6)
14261426
@test getcolptr(simA) == fill(1, 6+1)
1427-
@test length(rowvals(simA)) == length(nonzeroinds(A))
1428-
@test length(nonzeros(simA)) == length(nonzeros(A))
1427+
@test length(rowvals(simA)) == 0
1428+
@test length(nonzeros(simA)) == 0
14291429
# test similar with entry type and Dims{2} specification (preserves storage space only)
14301430
simA = similar(A, Float32, (6,6))
14311431
@test typeof(simA) == SparseMatrixCSC{Float32,eltype(nonzeroinds(A))}
14321432
@test size(simA) == (6,6)
14331433
@test getcolptr(simA) == fill(1, 6+1)
1434-
@test length(rowvals(simA)) == length(nonzeroinds(A))
1435-
@test length(nonzeros(simA)) == length(nonzeros(A))
1434+
@test length(rowvals(simA)) == 0
1435+
@test length(nonzeros(simA)) == 0
14361436
# test similar with entry type, index type, and Dims{2} specification (preserves storage space only)
14371437
simA = similar(A, Float32, Int8, (6,6))
14381438
@test typeof(simA) == SparseMatrixCSC{Float32, Int8}
14391439
@test size(simA) == (6,6)
14401440
@test getcolptr(simA) == fill(1, 6+1)
1441-
@test length(rowvals(simA)) == length(nonzeroinds(A))
1442-
@test length(nonzeros(simA)) == length(nonzeros(A))
1441+
@test length(rowvals(simA)) == 0
1442+
@test length(nonzeros(simA)) == 0
14431443
end
14441444

14451445
@testset "Fast operations on full column views" begin

0 commit comments

Comments
 (0)