diff --git a/stdlib/SparseArrays/src/sparsematrix.jl b/stdlib/SparseArrays/src/sparsematrix.jl index 6a4f7a36c2214..31a18b2a5edd4 100644 --- a/stdlib/SparseArrays/src/sparsematrix.jl +++ b/stdlib/SparseArrays/src/sparsematrix.jl @@ -327,17 +327,19 @@ function _sparsesimilar(S::SparseMatrixCSC, ::Type{TvNew}, ::Type{TiNew}) where newrowval = copyto!(similar(S.rowval, TiNew), S.rowval) return SparseMatrixCSC(S.m, S.n, newcolptr, newrowval, similar(S.nzval, TvNew)) end -# parent methods for similar that preserves only storage space (for when new and old dims differ) +# parent methods for similar that allocates an empty matrix (for when new dims are +# two-dimensional). _sparsesimilar(S::SparseMatrixCSC, ::Type{TvNew}, ::Type{TiNew}, dims::Dims{2}) where {TvNew,TiNew} = - SparseMatrixCSC(dims..., fill(one(TiNew), last(dims)+1), similar(S.rowval, TiNew), similar(S.nzval, TvNew)) + SparseMatrixCSC(dims..., fill(one(TiNew), last(dims)+1), similar(S.rowval, TiNew, 0), similar(S.nzval, TvNew, 0)) + # parent method for similar that allocates an empty sparse vector (when new dims are single) _sparsesimilar(S::SparseMatrixCSC, ::Type{TvNew}, ::Type{TiNew}, dims::Dims{1}) where {TvNew,TiNew} = SparseVector(dims..., similar(S.rowval, TiNew, 0), similar(S.nzval, TvNew, 0)) # # The following methods hook into the AbstractArray similar hierarchy. The first method # covers similar(A[, Tv]) calls, which preserve stored-entry structure, and the latter -# methods cover similar(A[, Tv], shape...) calls, which preserve storage space when the shape -# calls for a two-dimensional result. +# methods cover similar(A[, Tv], shape...) calls, with partially preserved structure +# when the shape calls for a two-dimensional result. similar(S::SparseMatrixCSC{<:Any,Ti}, ::Type{TvNew}) where {Ti,TvNew} = _sparsesimilar(S, TvNew, Ti) similar(S::SparseMatrixCSC{<:Any,Ti}, ::Type{TvNew}, dims::Union{Dims{1},Dims{2}}) where {Ti,TvNew} = _sparsesimilar(S, TvNew, Ti, dims) diff --git a/stdlib/SparseArrays/src/sparsevector.jl b/stdlib/SparseArrays/src/sparsevector.jl index 5daaa8d42195c..39baa61ba2697 100644 --- a/stdlib/SparseArrays/src/sparsevector.jl +++ b/stdlib/SparseArrays/src/sparsevector.jl @@ -67,22 +67,20 @@ _sparsesimilar(S::SparseVector, ::Type{TvNew}, ::Type{TiNew}) where {TvNew,TiNew # parent method for similar that preserves nothing (for when old and new dims differ, and new is 1d) _sparsesimilar(S::SparseVector, ::Type{TvNew}, ::Type{TiNew}, dims::Dims{1}) where {TvNew,TiNew} = SparseVector(dims..., similar(S.nzind, TiNew, 0), similar(S.nzval, TvNew, 0)) -# parent method for similar that preserves storage space (for old and new dims differ, and new is 2d) +# parent method for similar with emtpy storage space (for old and new dims differ, and new is 2d) _sparsesimilar(S::SparseVector, ::Type{TvNew}, ::Type{TiNew}, dims::Dims{2}) where {TvNew,TiNew} = - SparseMatrixCSC(dims..., fill(one(TiNew), last(dims)+1), similar(S.nzind, TiNew), similar(S.nzval, TvNew)) + SparseMatrixCSC(dims..., fill(one(TiNew), last(dims)+1), similar(S.nzind, TiNew, 0), similar(S.nzval, TvNew, 0)) # The following methods hook into the AbstractArray similar hierarchy. The first method # covers similar(A[, Tv]) calls, which preserve stored-entry structure, and the latter # methods cover similar(A[, Tv], shape...) calls, which preserve nothing if the dims -# specify a SparseVector result and storage space if the dims specify a SparseMatrixCSC result. +# specify a SparseVector or a SparseMatrixCSC result. similar(S::SparseVector{<:Any,Ti}, ::Type{TvNew}) where {Ti,TvNew} = _sparsesimilar(S, TvNew, Ti) similar(S::SparseVector{<:Any,Ti}, ::Type{TvNew}, dims::Union{Dims{1},Dims{2}}) where {Ti,TvNew} = _sparsesimilar(S, TvNew, Ti, dims) # The following methods cover similar(A, Tv, Ti[, shape...]) calls, which specify the # result's index type in addition to its entry type, and aren't covered by the hooks above. -# The calls without shape again preserve stored-entry structure, whereas those with -# one-dimensional shape preserve nothing, and those with two-dimensional shape -# preserve storage space. +# The calls without shape again preserve stored-entry structure but no storage space. similar(S::SparseVector, ::Type{TvNew}, ::Type{TiNew}) where{TvNew,TiNew} = _sparsesimilar(S, TvNew, TiNew) similar(S::SparseVector, ::Type{TvNew}, ::Type{TiNew}, dims::Union{Dims{1},Dims{2}}) where {TvNew,TiNew} = diff --git a/stdlib/SparseArrays/test/sparse.jl b/stdlib/SparseArrays/test/sparse.jl index 3ef2cef966ae2..d02e5d75b1c5e 100644 --- a/stdlib/SparseArrays/test/sparse.jl +++ b/stdlib/SparseArrays/test/sparse.jl @@ -2159,27 +2159,27 @@ end @test simA.colptr == A.colptr @test simA.rowval == A.rowval @test length(simA.nzval) == length(A.nzval) - # test similar with Dims{2} specification (preserves storage space only, not stored-entry structure) + # test similar with Dims{2} specification (empty storage space, not stored-entry structure) simA = similar(A, (6,6)) @test typeof(simA) == typeof(A) @test size(simA) == (6,6) @test simA.colptr == fill(1, 6+1) - @test length(simA.rowval) == length(A.rowval) - @test length(simA.nzval) == length(A.nzval) - # test similar with entry type and Dims{2} specification (preserves storage space only) + @test length(simA.rowval) == 0 + @test length(simA.nzval) == 0 + # test similar with entry type and Dims{2} specification (empty storage space) simA = similar(A, Float32, (6,6)) @test typeof(simA) == SparseMatrixCSC{Float32,eltype(A.colptr)} @test size(simA) == (6,6) @test simA.colptr == fill(1, 6+1) - @test length(simA.rowval) == length(A.rowval) - @test length(simA.nzval) == length(A.nzval) + @test length(simA.rowval) == 0 + @test length(simA.nzval) == 0 # test similar with entry type, index type, and Dims{2} specification (preserves storage space only) simA = similar(A, Float32, Int8, (6,6)) @test typeof(simA) == SparseMatrixCSC{Float32, Int8} @test size(simA) == (6,6) @test simA.colptr == fill(1, 6+1) - @test length(simA.rowval) == length(A.rowval) - @test length(simA.nzval) == length(A.nzval) + @test length(simA.rowval) == 0 + @test length(simA.nzval) == 0 # test similar with Dims{1} specification (preserves nothing) simA = similar(A, (6,)) @test typeof(simA) == SparseVector{eltype(A.nzval),eltype(A.colptr)} diff --git a/stdlib/SparseArrays/test/sparsevector.jl b/stdlib/SparseArrays/test/sparsevector.jl index d8ada3963e2c5..a3e5d06efa4f4 100644 --- a/stdlib/SparseArrays/test/sparsevector.jl +++ b/stdlib/SparseArrays/test/sparsevector.jl @@ -1229,22 +1229,22 @@ end @test typeof(simA) == SparseMatrixCSC{eltype(A.nzval),eltype(A.nzind)} @test size(simA) == (6,6) @test simA.colptr == fill(1, 6+1) - @test length(simA.rowval) == length(A.nzind) - @test length(simA.nzval) == length(A.nzval) + @test length(simA.rowval) == 0 # length(A.nzind) + @test length(simA.nzval) == 0 # length(A.nzval) # test similar with entry type and Dims{2} specification (preserves storage space only) simA = similar(A, Float32, (6,6)) @test typeof(simA) == SparseMatrixCSC{Float32,eltype(A.nzind)} @test size(simA) == (6,6) @test simA.colptr == fill(1, 6+1) - @test length(simA.rowval) == length(A.nzind) - @test length(simA.nzval) == length(A.nzval) + @test length(simA.rowval) == 0 # length(A.nzind) + @test length(simA.nzval) == 0 # length(A.nzval) # test similar with entry type, index type, and Dims{2} specification (preserves storage space only) simA = similar(A, Float32, Int8, (6,6)) @test typeof(simA) == SparseMatrixCSC{Float32, Int8} @test size(simA) == (6,6) @test simA.colptr == fill(1, 6+1) - @test length(simA.rowval) == length(A.nzind) - @test length(simA.nzval) == length(A.nzval) + @test length(simA.rowval) == 0 # length(A.nzind) + @test length(simA.nzval) == 0 # length(A.nzval) end @testset "Fast operations on full column views" begin