Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

similar(::SparseMatrixCSC, dims) returns sparse matrix with empty space (#26560) #30435

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7073d71
added sprandn methods with Type
KlausC Nov 19, 2018
e43667e
Merge remote-tracking branch 'upstream/master'
KlausC Nov 19, 2018
4bef0c4
Merge remote-tracking branch 'upstream/master'
KlausC Nov 21, 2018
468723c
Merge remote-tracking branch 'upstream/master'
KlausC Nov 23, 2018
b2b63ec
Merge remote-tracking branch 'upstream/master'
KlausC Nov 24, 2018
ef2bc34
Merge remote-tracking branch 'upstream/master'
KlausC Nov 27, 2018
0b2dbeb
Merge remote-tracking branch 'upstream/master'
KlausC Dec 4, 2018
1192355
Merge remote-tracking branch 'upstream/master'
KlausC Dec 7, 2018
d7282cb
Merge remote-tracking branch 'upstream/master'
KlausC Dec 9, 2018
d423468
Merge remote-tracking branch 'upstream/master'
KlausC Dec 10, 2018
07f744f
Merge remote-tracking branch 'upstream/master'
KlausC Dec 11, 2018
957aa85
Merge remote-tracking branch 'upstream/master'
KlausC Dec 12, 2018
c8b61ca
Merge remote-tracking branch 'upstream/master'
KlausC Dec 18, 2018
014dc41
similar on sparse matrix returns empty space
KlausC Dec 18, 2018
f03ca85
Merge branch 'master' into krc/similarsparse
KlausC Dec 18, 2018
6a4f54d
Merge remote-tracking branch 'upstream/master'
KlausC Dec 19, 2018
eff900e
Merge remote-tracking branch 'upstream/master' into krc/similarsparse
KlausC Dec 19, 2018
f14e6fa
edit format errors
KlausC Dec 19, 2018
d9f86eb
similar of SparseVector producing SparseMatrixCSC
KlausC Dec 19, 2018
3e3882a
similar to preserve non-zero structure as far as possible
KlausC Dec 20, 2018
af3e470
Merge remote-tracking branch 'upstream/master'
KlausC Dec 20, 2018
dc5be96
Merge branch 'master' into krc/similarsparse
KlausC Dec 22, 2018
719196e
empty rowval and nzval
KlausC Dec 25, 2018
c356d73
changes in test and comments
KlausC Jan 1, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 4 additions & 6 deletions stdlib/SparseArrays/src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
KlausC marked this conversation as resolved.
Show resolved Hide resolved
_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} =
Expand Down
16 changes: 8 additions & 8 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down
12 changes: 6 additions & 6 deletions stdlib/SparseArrays/test/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down