Skip to content

Commit f4c1c33

Browse files
committed
Don't create a new entry of zero is inserted. Should solve issue #1
1 parent 724b2c6 commit f4c1c33

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

src/sparsematrixlnk.jl

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ end
184184
"""
185185
$(SIGNATURES)
186186
187-
Update value of existing entry, otherwise extend matrix.
187+
Update value of existing entry, otherwise extend matrix if _v is nonzero.
188188
"""
189189
function Base.setindex!(lnk::SparseMatrixLNK{Tv,Ti}, _v, _i::Integer, _j::Integer) where {Tv,Ti<:Integer}
190190
v = convert(Tv, _v)
@@ -196,7 +196,7 @@ function Base.setindex!(lnk::SparseMatrixLNK{Tv,Ti}, _v, _i::Integer, _j::Intege
196196
end
197197

198198
# Set the first column entry if it was not yet set.
199-
if lnk.rowval[j]==0
199+
if lnk.rowval[j]==0 && !iszero(v)
200200
lnk.rowval[j]=i
201201
lnk.nzval[j]=v
202202
lnk.nnz+=1
@@ -208,8 +208,10 @@ function Base.setindex!(lnk::SparseMatrixLNK{Tv,Ti}, _v, _i::Integer, _j::Intege
208208
lnk.nzval[k]=v
209209
return lnk
210210
end
211-
k=addentry!(lnk,i,j,k,k0)
212-
lnk.nzval[k]=v
211+
if !iszero(v)
212+
k=addentry!(lnk,i,j,k,k0)
213+
lnk.nzval[k]=v
214+
end
213215
return lnk
214216
end
215217

@@ -218,8 +220,7 @@ end
218220
$(SIGNATURES)
219221
220222
Update element of the matrix with operation `op`.
221-
This can replace the following code and save one index
222-
search during acces:
223+
It assumes that `op(0,0)==0`
223224
"""
224225
function updateindex!(lnk::SparseMatrixLNK{Tv,Ti},op, _v, _i::Integer, _j::Integer) where {Tv,Ti<:Integer}
225226
v = convert(Tv, _v)
@@ -228,7 +229,7 @@ function updateindex!(lnk::SparseMatrixLNK{Tv,Ti},op, _v, _i::Integer, _j::Integ
228229

229230

230231
# Set the first column entry if it was not yet set.
231-
if lnk.rowval[j]==0
232+
if lnk.rowval[j]==0 && !iszero(v)
232233
lnk.rowval[j]=i
233234
lnk.nzval[j]=op(lnk.nzval[j],v)
234235
lnk.nnz+=1
@@ -239,8 +240,10 @@ function updateindex!(lnk::SparseMatrixLNK{Tv,Ti},op, _v, _i::Integer, _j::Integ
239240
lnk.nzval[k]=op(lnk.nzval[k],v)
240241
return lnk
241242
end
242-
k=addentry!(lnk,i,j,k,k0)
243-
lnk.nzval[k]=op(zero(Tv),v)
243+
if !iszero(v)
244+
k=addentry!(lnk,i,j,k,k0)
245+
lnk.nzval[k]=op(zero(Tv),v)
246+
end
244247
lnk
245248
end
246249

0 commit comments

Comments
 (0)