@@ -241,7 +241,7 @@ function Base.setindex!(ag::AliasGraph, v::Integer, i::Integer)
241
241
return 0 => 0
242
242
end
243
243
244
- function Base. setindex! (ag:: AliasGraph , p:: Pair{Int, Int} , i:: Integer )
244
+ function Base. setindex! (ag:: AliasGraph , p:: Union{ Pair{Int, Int}, Tuple{Int, Int} } , i:: Integer )
245
245
(c, v) = p
246
246
if c == 0 || v == 0
247
247
ag[i] = 0
@@ -271,49 +271,9 @@ function Base.in(i::Int, agk::AliasGraphKeySet)
271
271
end
272
272
273
273
function reduce! (mm:: SparseMatrixCLIL , ag:: AliasGraph )
274
- dels = Int[]
275
- for (i, rs) in enumerate (mm. row_cols)
276
- rvals = mm. row_vals[i]
277
- j = 1
278
- while j <= length (rs)
279
- c = rs[j]
280
- _alias = get (ag, c, nothing )
281
- if _alias != = nothing
282
- coeff, alias = _alias
283
- if alias == c
284
- i = searchsortedfirst (rs, alias)
285
- rvals[i] *= coeff
286
- else
287
- push! (dels, j)
288
- iszero (coeff) && (j += 1 ; continue )
289
- inc = coeff * rvals[j]
290
- i = searchsortedfirst (rs, alias)
291
- if i > length (rs) || rs[i] != alias
292
- # if we add a variable to what we already visited, make sure
293
- # to bump the cursor.
294
- j += i <= j
295
- for (i, e) in enumerate (dels)
296
- e >= i && (dels[i] += 1 )
297
- end
298
- insert! (rs, i, alias)
299
- insert! (rvals, i, inc)
300
- else
301
- rvals[i] += inc
302
- end
303
- end
304
- end
305
- j += 1
306
- end
307
- unique! (sort! (dels))
308
- deleteat! (rs, dels)
309
- deleteat! (rvals, dels)
310
- empty! (dels)
311
- for (j, v) in enumerate (rvals)
312
- iszero (v) && push! (dels, j)
313
- end
314
- deleteat! (rs, dels)
315
- deleteat! (rvals, dels)
316
- empty! (dels)
274
+ for i in 1 : size (mm, 1 )
275
+ adj_row = @view mm[i, :]
276
+ locally_structure_simplify! (adj_row, nothing , ag)
317
277
end
318
278
mm
319
279
end
@@ -896,8 +856,12 @@ function exactdiv(a::Integer, b)
896
856
end
897
857
898
858
function locally_structure_simplify! (adj_row, pivot_var, ag)
899
- pivot_val = adj_row[pivot_var]
900
- iszero (pivot_val) && return false
859
+ if pivot_var === nothing
860
+ pivot_val = nothing
861
+ else
862
+ pivot_val = adj_row[pivot_var]
863
+ iszero (pivot_val) && return false
864
+ end
901
865
902
866
nirreducible = 0
903
867
# When this row only as the pivot element, the pivot is zero by homogeneity
@@ -940,10 +904,21 @@ function locally_structure_simplify!(adj_row, pivot_var, ag)
940
904
end
941
905
end
942
906
907
+ if pivot_var === nothing
908
+ if iszero (nirreducible)
909
+ zero! (adj_row)
910
+ else
911
+ dropzeros! (adj_row)
912
+ end
913
+ return true
914
+ end
943
915
# If there were only one or two terms left in the equation (including the
944
916
# pivot variable). We can eliminate the pivot variable. Note that when
945
917
# `nirreducible <= 1`, `alias_candidate` is uniquely determined.
946
- nirreducible <= 1 || return false
918
+ if nirreducible > 1
919
+ dropzeros! (adj_row)
920
+ return false
921
+ end
947
922
948
923
if alias_candidate isa Pair
949
924
alias_val, alias_var = alias_candidate
0 commit comments