Skip to content

Commit

Permalink
add spzeros(::Tuple{<:Integer}) (#40515)
Browse files Browse the repository at this point in the history
  • Loading branch information
abraunst committed Apr 18, 2021
1 parent 55dd2f4 commit 36a048c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions stdlib/SparseArrays/src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,11 @@ Base.unaliascopy(S::SparseVector) = typeof(S)(length(S), unaliascopy(nonzeroinds
### Construct empty sparse vector

spzeros(len::Integer) = spzeros(Float64, len)
spzeros(dims::Tuple{<:Integer}) = spzeros(Float64, dims[1])
spzeros(::Type{T}, len::Integer) where {T} = SparseVector(len, Int[], T[])
spzeros(::Type{T}, dims::Tuple{<:Integer}) where {T} = spzeros(T, dims[1])
spzeros(::Type{Tv}, ::Type{Ti}, len::Integer) where {Tv,Ti<:Integer} = SparseVector(len, Ti[], Tv[])
spzeros(::Type{Tv}, ::Type{Ti}, dims::Tuple{<:Integer}) where {Tv,Ti<:Integer} = spzeros(Tv, Ti, dims[1])

LinearAlgebra.fillstored!(x::SparseVector, y) = (fill!(nonzeros(x), y); x)

Expand Down
6 changes: 5 additions & 1 deletion stdlib/SparseArrays/test/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ end
@testset "other constructors" begin
# construct empty sparse vector

@test exact_equal(spzeros(Float64, 8), SparseVector(8, Int[], Float64[]))
for dims in (8, (8,))
@test exact_equal(spzeros(dims), SparseVector(8, Int[], Float64[]))
@test exact_equal(spzeros(Float64, dims), SparseVector(8, Int[], Float64[]))
@test exact_equal(spzeros(Float64, Int16, dims), SparseVector(8, Int16[], Float64[]))
end

@testset "from list of indices and values" begin
@test exact_equal(
Expand Down

0 comments on commit 36a048c

Please sign in to comment.