Skip to content

Commit 5ea69be

Browse files
stev47KristofferC
authored andcommitted
stdlib/SparseArrays: fix scalar setindex! for vector eltype (#29331)
fix #29034 (cherry picked from commit 99efc91)
1 parent 6cb86ad commit 5ea69be

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

stdlib/SparseArrays/src/sparsematrix.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,11 @@ getindex(A::SparseMatrixCSC, I::AbstractVector{<:Integer}, J::AbstractVector{Boo
23742374
getindex(A::SparseMatrixCSC, I::AbstractVector{Bool}, J::AbstractVector{<:Integer}) = A[findall(I),J]
23752375

23762376
## setindex!
2377-
function setindex!(A::SparseMatrixCSC{Tv,Ti}, _v, _i::Integer, _j::Integer) where {Tv,Ti<:Integer}
2377+
2378+
# dispatch helper for #29034
2379+
setindex!(A::SparseMatrixCSC, _v, _i::Integer, _j::Integer) = _setindex_scalar!(A, _v, _i, _j)
2380+
2381+
function _setindex_scalar!(A::SparseMatrixCSC{Tv,Ti}, _v, _i::Integer, _j::Integer) where {Tv,Ti<:Integer}
23782382
v = convert(Tv, _v)
23792383
i = convert(Ti, _i)
23802384
j = convert(Ti, _j)
@@ -2569,7 +2573,8 @@ end
25692573
_to_same_csc(::SparseMatrixCSC{Tv, Ti}, V::AbstractMatrix, I...) where {Tv,Ti} = convert(SparseMatrixCSC{Tv,Ti}, V)
25702574
_to_same_csc(::SparseMatrixCSC{Tv, Ti}, V::AbstractVector, I...) where {Tv,Ti} = convert(SparseMatrixCSC{Tv,Ti}, reshape(V, map(length, I)))
25712575

2572-
setindex!(A::SparseMatrixCSC{Tv}, B::AbstractVecOrMat, I::Integer, J::Integer) where {Tv} = setindex!(A, convert(Tv, B), I, J)
2576+
setindex!(A::SparseMatrixCSC{Tv}, B::AbstractVecOrMat, I::Integer, J::Integer) where {Tv} = _setindex_scalar!(A, B, I, J)
2577+
25732578
function setindex!(A::SparseMatrixCSC{Tv,Ti}, V::AbstractVecOrMat, Ix::Union{Integer, AbstractVector{<:Integer}, Colon}, Jx::Union{Integer, AbstractVector{<:Integer}, Colon}) where {Tv,Ti<:Integer}
25742579
@assert !has_offset_axes(A, V, Ix, Jx)
25752580
(I, J) = Base.ensure_indexable(to_indices(A, (Ix, Jx)))

stdlib/SparseArrays/test/sparse.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2002,6 +2002,12 @@ end
20022002
@test nnz(A) == 1
20032003
end
20042004

2005+
@testset "setindex with vector eltype (#29034)" begin
2006+
A = sparse([1], [1], [Vector{Float64}(undef, 3)], 3, 3)
2007+
A[1,1] = [1.0, 2.0, 3.0]
2008+
@test A[1,1] == [1.0, 2.0, 3.0]
2009+
end
2010+
20052011
@testset "show" begin
20062012
io = IOBuffer()
20072013
show(io, MIME"text/plain"(), sparse(Int64[1], Int64[1], [1.0]))

0 commit comments

Comments
 (0)