Skip to content

Commit 7cfbfcd

Browse files
committed
Re-implement sparse setindex! as fill!
and simplify the call structure -- integers are just as capable as pseudo-vectors in these functions
1 parent a5a8fd6 commit 7cfbfcd

File tree

2 files changed

+22
-27
lines changed

2 files changed

+22
-27
lines changed

base/sysimg.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ if false
8989
# simple print definitions for debugging. enable these if something
9090
# goes wrong during bootstrap before printing code is available.
9191
# otherwise, they just just eventually get (noisily) overwritten later
92-
global show, print, println, string
92+
global show, print, println
9393
show(io::IO, x) = Core.show(io, x)
9494
print(io::IO, a...) = Core.print(io, a...)
9595
println(io::IO, x...) = Core.println(io, x...)

stdlib/SparseArrays/src/sparsematrix.jl

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2350,37 +2350,34 @@ function setindex!(A::SparseMatrixCSC{Tv,Ti}, v::Tv, i::Ti, j::Ti) where Tv wher
23502350
return A
23512351
end
23522352

2353-
setindex!(A::SparseMatrixCSC, v::AbstractArray, i::Integer, J::AbstractVector{<:Integer}) = setindex!(A, v, [i], J)
2354-
setindex!(A::SparseMatrixCSC, v::AbstractArray, I::AbstractVector{<:Integer}, j::Integer) = setindex!(A, v, I, [j])
2355-
23562353
# Colon translation
23572354
setindex!(A::SparseMatrixCSC, x::AbstractArray, ::Colon) = setindex!(A, x, 1:length(A))
23582355
setindex!(A::SparseMatrixCSC, x::AbstractArray, ::Colon, ::Colon) = setindex!(A, x, 1:size(A, 1), 1:size(A,2))
23592356
setindex!(A::SparseMatrixCSC, x::AbstractArray, ::Colon, j::Union{Integer, AbstractVector}) = setindex!(A, x, 1:size(A, 1), j)
23602357
setindex!(A::SparseMatrixCSC, x::AbstractArray, i::Union{Integer, AbstractVector}, ::Colon) = setindex!(A, x, i, 1:size(A, 2))
23612358

2362-
# TODO: Revamp this guy to use broadcast
2363-
# function setindex!(A::SparseMatrixCSC{Tv}, x::Number,
2364-
# I::AbstractVector{<:Integer}, J::AbstractVector{<:Integer}) where Tv
2365-
# if isempty(I) || isempty(J); return A; end
2366-
# # lt=≤ to check for strict sorting
2367-
# if !issorted(I, lt=≤); I = sort!(unique(I)); end
2368-
# if !issorted(J, lt=≤); J = sort!(unique(J)); end
2369-
# if (I[1] < 1 || I[end] > A.m) || (J[1] < 1 || J[end] > A.n)
2370-
# throw(BoundsError(A, (I, J)))
2371-
# end
2372-
# if x == 0
2373-
# _spsetz_setindex!(A, I, J)
2374-
# else
2375-
# _spsetnz_setindex!(A, convert(Tv, x), I, J)
2376-
# end
2377-
# end
2359+
function Base.fill!(V::SubArray{Tv, <:Any, <:SparseMatrixCSC, Tuple{Vararg{Union{Integer, AbstractVector{<:Integer}},2}}}, x) where Tv
2360+
A = V.parent
2361+
I, J = V.indices
2362+
if isempty(I) || isempty(J); return A; end
2363+
# lt=≤ to check for strict sorting
2364+
if !issorted(I, lt=); I = sort!(unique(I)); end
2365+
if !issorted(J, lt=); J = sort!(unique(J)); end
2366+
if (I[1] < 1 || I[end] > A.m) || (J[1] < 1 || J[end] > A.n)
2367+
throw(BoundsError(A, (I, J)))
2368+
end
2369+
if x == 0
2370+
_spsetz_setindex!(A, I, J)
2371+
else
2372+
_spsetnz_setindex!(A, convert(Tv, x), I, J)
2373+
end
2374+
end
23782375
"""
23792376
Helper method for immediately preceding setindex! method. For all (i,j) such that i in I and
23802377
j in J, assigns zero to A[i,j] if A[i,j] is a presently-stored entry, and otherwise does nothing.
23812378
"""
23822379
function _spsetz_setindex!(A::SparseMatrixCSC,
2383-
I::AbstractVector{<:Integer}, J::AbstractVector{<:Integer})
2380+
I::Union{Integer, AbstractVector{<:Integer}}, J::Union{Integer, AbstractVector{<:Integer}})
23842381
lengthI = length(I)
23852382
for j in J
23862383
coljAfirstk = A.colptr[j]
@@ -2416,7 +2413,7 @@ and j in J, assigns x to A[i,j] if A[i,j] is a presently-stored entry, and alloc
24162413
assigns x to A[i,j] if A[i,j] is not presently stored.
24172414
"""
24182415
function _spsetnz_setindex!(A::SparseMatrixCSC{Tv}, x::Tv,
2419-
I::AbstractVector{<:Integer}, J::AbstractVector{<:Integer}) where Tv
2416+
I::Union{Integer, AbstractVector{<:Integer}}, J::Union{Integer, AbstractVector{<:Integer}}) where Tv
24202417
m, n = size(A)
24212418
lenI = length(I)
24222419

@@ -2521,16 +2518,14 @@ function _spsetnz_setindex!(A::SparseMatrixCSC{Tv}, x::Tv,
25212518
return A
25222519
end
25232520

2524-
setindex!(A::SparseMatrixCSC{Tv,Ti}, S::Matrix, I::AbstractVector{T}, J::AbstractVector{T}) where {Tv,Ti,T<:Integer} =
2521+
setindex!(A::SparseMatrixCSC{Tv,Ti}, S::Matrix, I::Union{Integer, AbstractVector{T}}, J::Union{Integer, AbstractVector{T}}) where {Tv,Ti,T<:Integer} =
25252522
setindex!(A, convert(SparseMatrixCSC{Tv,Ti}, S), I, J)
25262523

2527-
setindex!(A::SparseMatrixCSC, v::AbstractVector, I::AbstractVector{<:Integer}, j::Integer) = setindex!(A, v, I, [j])
2528-
setindex!(A::SparseMatrixCSC, v::AbstractVector, i::Integer, J::AbstractVector{<:Integer}) = setindex!(A, v, [i], J)
2529-
setindex!(A::SparseMatrixCSC, v::AbstractVector, I::AbstractVector{T}, J::AbstractVector{T}) where {T<:Integer} =
2524+
setindex!(A::SparseMatrixCSC, v::AbstractVector, I::Union{Integer, AbstractVector{T}}, J::Union{Integer, AbstractVector{T}}) where {T<:Integer} =
25302525
setindex!(A, reshape(v, length(I), length(J)), I, J)
25312526

25322527
# A[I,J] = B
2533-
function setindex!(A::SparseMatrixCSC{Tv,Ti}, B::SparseMatrixCSC{Tv,Ti}, I::AbstractVector{T}, J::AbstractVector{T}) where {Tv,Ti,T<:Integer}
2528+
function setindex!(A::SparseMatrixCSC{Tv,Ti}, B::SparseMatrixCSC{Tv,Ti}, I::Union{Integer, AbstractVector{T}}, J::Union{Integer, AbstractVector{T}}) where {Tv,Ti,T<:Integer}
25342529
if size(B,1) != length(I) || size(B,2) != length(J)
25352530
throw(DimensionMismatch(""))
25362531
end

0 commit comments

Comments
 (0)