@@ -104,11 +104,20 @@ function computed_highest_diff_variables(structure, ag::Union{AliasGraph, Nothin
104
104
varwhitelist = falses (nvars)
105
105
for var in 1 : nvars
106
106
if var_to_diff[var] === nothing && ! varwhitelist[var]
107
+ # This variable is structurally highest-differentiated, but may not actually appear in the
108
+ # system (complication 1 above). Ascend the differentiation graph to find the highest
109
+ # differentiated variable that does appear in the system or the alias graph).
107
110
while isempty (𝑑neighbors (graph, var)) && (ag === nothing || ! haskey (ag, var))
108
111
var′ = invview (var_to_diff)[var]
109
112
var′ === nothing && break
110
113
var = var′
111
114
end
115
+ # If we don't have an alias graph, we are done. If we do have an alias graph, we may
116
+ # have to keep going along the stem, for as long as our differentiation path
117
+ # matches that of the stem (see complication 2 above). Note that we may end up
118
+ # whitelisting multiple differentiation levels of the stem here from different
119
+ # starting points that all map to the same stem. We clean that up in a post-processing
120
+ # pass below.
112
121
if ag != = nothing && haskey (ag, var)
113
122
(_, stem) = ag[var]
114
123
stem == 0 && continue
@@ -134,7 +143,7 @@ function computed_highest_diff_variables(structure, ag::Union{AliasGraph, Nothin
134
143
end
135
144
136
145
# Remove any variables from the varwhitelist for whom a higher-differentiated
137
- # var is already on the whitelist
146
+ # var is already on the whitelist.
138
147
for var in 1 : nvars
139
148
varwhitelist[var] || continue
140
149
var′ = var
0 commit comments