Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
14 changes: 11 additions & 3 deletions src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,7 @@ julia> sparse(Is, Js, Vs)
⋅ ⋅ 3
```
"""
function sparse(I::AbstractVector{Ti}, J::AbstractVector{Ti}, V::AbstractVector{Tv}, m::Integer, n::Integer, combine) where {Tv,Ti<:Integer}
function sparse(I::AbstractVector{Ti}, J::AbstractVector{Ti}, V::AbstractVector{Tv}, m::Integer, n::Integer, combine::F) where {Tv,Ti<:Integer,F}
require_one_based_indexing(I, J, V)
coolen = length(I)
if length(J) != coolen || length(V) != coolen
Expand Down Expand Up @@ -1110,7 +1110,7 @@ function sparse(I::AbstractVector{Ti}, J::AbstractVector{Ti}, V::AbstractVector{
end
end

sparse(I::AbstractVector, J::AbstractVector, V::AbstractVector, m::Integer, n::Integer, combine) =
sparse(I::AbstractVector, J::AbstractVector, V::AbstractVector, m::Integer, n::Integer, combine::F) where {F} =
sparse(AbstractVector{Int}(I), AbstractVector{Int}(J), V, m, n, combine)

"""
Expand Down Expand Up @@ -4238,14 +4238,22 @@ end
_nzvals(v::AbstractSparseVector) = nonzeros(v)
_nzvals(v::AbstractVector) = v

# Promoted element type of the diagonals, mirroring `Base.promote_eltypeof`
spdiagm_eltype(p::Pair) = eltype(p.second)
spdiagm_eltype(p::Pair, q::Pair, rest::Pair...) =
(@inline; promote_type(promote_type(eltype(p.second), eltype(q.second)),
spdiagm_eltype(rest...)))
spdiagm_eltype(p::Pair, q::Pair) = promote_type(eltype(p.second), eltype(q.second))
spdiagm_eltype(kv::Pair{<:Integer,<:AbstractVector{T}}...) where {T} = T

function spdiagm_internal(kv::Pair{<:Integer,<:AbstractVector}...)
ncoeffs = 0
for p in kv
ncoeffs += _nnz(p.second)
end
I = Vector{Int}(undef, ncoeffs)
J = Vector{Int}(undef, ncoeffs)
V = Vector{promote_type(map(x -> eltype(x.second), kv)...)}(undef, ncoeffs)
V = Vector{spdiagm_eltype(kv...)}(undef, ncoeffs)
i = 0
m = 0
n = 0
Expand Down
4 changes: 4 additions & 0 deletions test/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,10 @@ end
# promotion
@test spdiagm(0 => [1,2], 1 => [3.5], -1 => [4+5im]) == [1 3.5; 4+5im 2]

# sparse eltypes should infer well, even for a `Vararg` tail of unknown length
@test Base.infer_return_type(SparseArrays.spdiagm_eltype,
Tuple{Vararg{Pair{Int,Vector{Float64}}}}) === Core.Typeof(Float64)

# convenience constructor
@test spdiagm(x)::SparseMatrixCSC == diagm(x)
@test nnz(spdiagm(x)) == count(!iszero, x)
Expand Down
Loading