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"""
189189function 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
214216end
215217
218220$(SIGNATURES)
219221
220222Update 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"""
224225function 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
245248end
246249
0 commit comments