Skip to content

Commit 0374a56

Browse files
committed
Fix some typos
1 parent 8ddb7ee commit 0374a56

File tree

2 files changed

+44
-43
lines changed

2 files changed

+44
-43
lines changed

src/systems/alias_elimination.jl

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ using SymbolicUtils: Rewriters
22

33
const KEEP = typemin(Int)
44

5-
function alias_eliminate(sys)
5+
function alias_elimination(sys)
66
sys = flatten(sys)
77
s = get_structure(sys)
88
if !(s isa SystemStructure)
@@ -30,7 +30,7 @@ function alias_eliminate(sys)
3030
dels = Int[]
3131
eqs = copy(equations(sys))
3232
for (ei, e) in enumerate(linear_equations)
33-
vs = 𝑠vertices(graph, e)
33+
vs = 𝑠neighbors(graph, e)
3434
if isempty(vs)
3535
push!(dels, e)
3636
else
@@ -64,16 +64,16 @@ function alias_eliminate(sys)
6464
return sys
6565
end
6666

67-
function alias_eliminate_graph(s::SystemStructure, is_linear_variables, eadj, cadj)
67+
function alias_eliminate_graph(s::SystemStructure, is_linear_equations, eadj, cadj)
6868
@unpack graph, varassoc = s
6969
invvarassoc = inverse_mapping(varassoc)
7070

7171
old_cadj = map(copy, cadj)
7272

7373
is_not_potential_state = iszero.(varassoc)
7474
is_linear_variables = copy(is_not_potential_state)
75-
for i in 𝑠edges(graph); is_linear_equations[i] || continue
76-
for j in 𝑠vertices(graph, i)
75+
for i in 𝑠vertices(graph); is_linear_equations[i] || continue
76+
for j in 𝑠neighbors(graph, i)
7777
is_linear_variables[j] = false
7878
end
7979
end
@@ -83,11 +83,11 @@ function alias_eliminate_graph(s::SystemStructure, is_linear_variables, eadj, ca
8383

8484

8585
rank1 = bareiss!(
86-
(eadg, cadj),
86+
(eadj, cadj),
8787
old_cadj, linear_equations, is_linear_variables, 1
8888
)
8989

90-
v_solved = [eadg[i][1] for i in 1:rank1]
90+
v_solved = [eadj[i][1] for i in 1:rank1]
9191
v_eliminated = setdiff(solvable_variables, v_solved)
9292
n_null_vars = length(v_eliminated)
9393

@@ -97,41 +97,42 @@ function alias_eliminate_graph(s::SystemStructure, is_linear_variables, eadj, ca
9797
end
9898

9999
rank2 = bareiss!(
100-
(eadg, cadj),
100+
(eadj, cadj),
101101
old_cadj, linear_equations, is_not_potential_state, rank1+1
102102
)
103103

104104
rank3 = bareiss!(
105-
(eadg, cadj),
105+
(eadj, cadj),
106106
old_cadj, linear_equations, nothing, rank2+1
107107
)
108108

109109
# kind of like the backward substitution
110110
for ei in reverse(1:rank2)
111111
locally_structure_simplify!(
112-
(eadg[ei], cadj[ei]),
112+
(eadj[ei], cadj[ei]),
113113
invvarassoc, v_eliminated, v_types
114114
)
115115
end
116116

117117
reduced = false
118118
for ei in 1:rank2
119-
if length(cadj[ei]) > length(old_cadj[ei])
119+
if length(cadj[ei]) >= length(old_cadj[ei])
120120
cadj[ei] = old_cadj[ei]
121121
else
122-
cadj[ei] = eadg[linear_equations[ei]]
122+
# MEMORY ALIAS of a vector
123+
eadj[ei] = 𝑠neighbors(graph, linear_equations[ei])
123124
reduced |= locally_structure_simplify!(
124-
(eadg[ei], cadj[ei]),
125+
(eadj[ei], cadj[ei]),
125126
invvarassoc, v_eliminated, v_types
126127
)
127128
end
128129
end
129130

130131
while reduced
131132
for ei in 1:rank2
132-
if !isempty(eadg[ei])
133+
if !isempty(eadj[ei])
133134
reduced |= locally_structure_simplify!(
134-
(eadg[ei], cadj[ei]),
135+
(eadj[ei], cadj[ei]),
135136
invvarassoc, v_eliminated, v_types
136137
)
137138
reduced && break # go back to the begining of equations
@@ -140,11 +141,11 @@ function alias_eliminate_graph(s::SystemStructure, is_linear_variables, eadj, ca
140141
end
141142

142143
for ei in rank2+1:length(linear_equations)
143-
eadg[ei] = old_cadj[ei]
144+
eadj[ei] = old_cadj[ei]
144145
end
145146

146147
for (ei, e) in enumerate(linear_equations)
147-
graph.eadglist[e] = eadg[ei]
148+
graph.eadjlist[e] = eadj[ei]
148149
end
149150

150151
degenerate_equations = rank3 < length(linear_equations) ? linear_equations[rank3+1:end] : Int[]
@@ -161,7 +162,7 @@ function locally_structure_simplify!(
161162
(vars, coeffs),
162163
invvarassoc, v_eliminated, v_types
163164
)
164-
while length(vars) > 1 && any(!isequal(KEEP), (v_types[v] in @view vars[2:end]))
165+
while length(vars) > 1 && any(!isequal(KEEP), (v_types[v] for v in @view vars[2:end]))
165166
for vj in 2:length(vars)
166167
v = vars[vj]
167168
if isirreducible(v_types, v)
@@ -208,7 +209,7 @@ function locally_structure_simplify!(
208209

209210
v = first(vars)
210211
if invvarassoc[v] == 0
211-
if length(nvars) == 1
212+
if length(vars) == 1
212213
push!(v_eliminated, v)
213214
v_types[v] = 0
214215
empty!(vars); empty!(coeffs)
@@ -236,11 +237,11 @@ $(SIGNATURES)
236237
Use Bareiss algorithm to compute the nullspace of an integer matrix exactly.
237238
"""
238239
function bareiss!(
239-
(eadg, cadj),
240+
(eadj, cadj),
240241
old_cadj, linear_equations, is_linear_variables, offset
241242
)
242-
m = nsrcs(solvable_graph)
243-
# v = eadg[ei][vj]
243+
m = length(eadj)
244+
# v = eadj[ei][vj]
244245
v = ei = vj = 0
245246
pivot = last_pivot = 1
246247
tmp_incidence = Int[]
@@ -256,46 +257,46 @@ function bareiss!(
256257
###
257258
### Pivoting:
258259
###
259-
ei, vj = find_first_linear_variable(solvable_graph, k:m, is_linear_variables, isequal(1))
260+
ei, vj = find_first_linear_variable(eadj, k:m, is_linear_variables, isequal(1))
260261
if vj == 0
261-
ei, vj = find_first_linear_variable(solvable_graph, k:m, is_linear_variables, isequal(2))
262+
ei, vj = find_first_linear_variable(eadj, k:m, is_linear_variables, isequal(2))
262263
else
263-
ei, vj = find_first_linear_variable(solvable_graph, k:m, is_linear_variables, _->true)
264+
ei, vj = find_first_linear_variable(eadj, k:m, is_linear_variables, _->true)
264265
end
265266

266267
if vj > 0 # has a pivot
267268
pivot = cadj[ei][vj]
268269
deleteat!(cadj[ei] , vj)
269-
v = eadg[ei][vj]
270-
deleteat!(eadg[ei], vj)
270+
v = eadj[ei][vj]
271+
deleteat!(eadj[ei], vj)
271272
if ei != k
272273
swap!(cadj, ei, k)
273274
swap!(old_cadj, ei, k)
274-
swap!(eadg, ei, k)
275+
swap!(eadj, ei, k)
275276
swap!(linear_equations, ei, k)
276277
end
277278
else # rank deficient
278279
return k-1
279280
end
280281

281-
for ei in k+1
282+
for ei in k+1:m
282283
# elimate `v`
283284
coeff = 0
284-
vars = eadg[ei]
285+
vars = eadj[ei]
285286
vj = findfirst(isequal(v), vars)
286287
if vj === nothing # `v` is not in in `e`
287288
continue
288289
else # remove `v`
289290
coeff = cadj[ei][vj]
290291
deleteat!(cadj[ei], vj)
291-
deleteat!(eadg[ei], vj)
292+
deleteat!(eadj[ei], vj)
292293
end
293294

294295
# the pivot row
295-
kvars = eadg[k]
296+
kvars = eadj[k]
296297
kcoeffs = cadj[k]
297298
# the elimination target
298-
ivars = eadg[ei]
299+
ivars = eadj[ei]
299300
icoeffs = cadj[ei]
300301

301302
empty!(tmp_incidence)
@@ -313,19 +314,19 @@ function bareiss!(
313314
end
314315
end
315316

316-
eadg[ei], tmp_incidence = tmp_incidence, eadg[ei]
317+
eadj[ei], tmp_incidence = tmp_incidence, eadj[ei]
317318
cadj[ei], tmp_coeffs = tmp_coeffs, cadj[ei]
318319
end
319320
last_pivot = pivot
320321
# add `v` in the front of the `k`-th equation
321-
pushfirst!(eadg[k], v)
322+
pushfirst!(eadj[k], v)
322323
pushfirst!(cadj[k], pivot)
323324
end
324325

325326
return m # fully ranked
326327
end
327328

328-
swap!(v, i, j) = ((v[i], v[j] = v[j], v[i]); nothing)
329+
swap!(v, i, j) = v[i], v[j] = v[j], v[i]
329330

330331
function getcoeff(vars, coeffs, var)
331332
for (vj, v) in enumerate(vars)
@@ -337,17 +338,17 @@ end
337338
"""
338339
$(SIGNATURES)
339340
340-
Find the first linear variable such that `𝑠vertices(adj, i)[j]` is true given
341+
Find the first linear variable such that `𝑠neighbors(adj, i)[j]` is true given
341342
the `constraint`.
342343
"""
343344
@inline function find_first_linear_variable(
344-
solvable_graph,
345+
eadj,
345346
range,
346347
mask,
347348
constraint,
348349
)
349350
for i in range
350-
vertices = 𝑠vertices(solvable_graph, i)
351+
vertices = eadj[i]
351352
if constraint(length(vertices))
352353
for (j, v) in enumerate(vertices)
353354
(mask === nothing || mask[v]) && return i, j

src/systems/systemstructure.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ for v in 𝑣vertices(graph); active_𝑣vertices[v] || continue
3636
end
3737
=#
3838

39-
export SystemStructure, initialize_system_structure
39+
export SystemStructure, initialize_system_structure, find_linear_equations
4040
export diffvars_range, dervars_range, algvars_range
4141
export isdiffvar, isdervar, isalgvar, isdiffeq, isalgeq
4242
export DIFFERENTIAL_VARIABLE, ALGEBRAIC_VARIABLE, DERIVATIVE_VARIABLE
@@ -82,7 +82,7 @@ eqtype(s::SystemStructure, eq::Integer)::EquationType = isalgeq(s, eq) ? ALGEBRA
8282
function initialize_system_structure(sys)
8383
sys, dxvar_offset, fullvars, varassoc, algeqs, graph = init_graph(flatten(sys))
8484

85-
solvable_graph = BipartiteGraph(neqs, nvars)
85+
solvable_graph = BipartiteGraph(nsrcs(graph), ndsts(graph))
8686

8787
@set sys.structure = SystemStructure(
8888
dxvar_offset,
@@ -212,8 +212,8 @@ function find_linear_equations(sys)
212212
# where ``c_i`` ∈ ℤ and ``a_i`` denotes algebraic variables.
213213
if all_int_algvars && isequal(linear_term, term)
214214
is_linear_equations[i] = true
215-
push!(eadj, i)
216-
push!(cadj, copy(coeff))
215+
push!(eadj, copy(𝑠neighbors(graph, i)))
216+
push!(cadj, copy(coeffs))
217217
else
218218
is_linear_equations[i] = false
219219
end

0 commit comments

Comments
 (0)