@@ -120,7 +120,7 @@ function _classify_default!(result)
120120 classify_solutions! (result, is_physical, " physical" )
121121 classify_solutions! (result, is_stable, " stable" )
122122 classify_solutions! (result, is_Hopf_unstable, " Hopf" )
123- order_branches! (result, [" physical" , " stable" , " Hopf " ]) # shuffle the branches to have relevant ones first
123+ order_branches! (result, [" physical" , " stable" ]) # shuffle the branches to have relevant ones first
124124 classify_binaries! (result) # assign binaries to solutions depending on which branches are stable
125125end
126126
@@ -160,37 +160,38 @@ function compile_matrix(matrix, variables, fixed_parameters)
160160end
161161
162162
163- " Order the solution branches in `res` such that close classified positively by `class` are first."
164- function order_branches! (res:: Result , class:: String )
165- indices = findall ( x-> x== 1 , any .(classify_branch (res, class)))
166- order = cat (indices, setdiff (1 : length (res. solutions[1 ]), indices), dims= 1 ) # permutation of indices which puts stable branches first
167- reorder_solutions! (res, order)
163+ " Find a branch order according `classification`. Place branches where true occurs earlier first."
164+ function find_branch_order (classification:: Vector{BitVector} )
165+ branches = [getindex .(classification, k) for k in 1 : length (classification[1 ])] # array of branches
166+ indices = replace (findfirst .(branches), nothing => Inf )
167+ negative = findall (x -> x == Inf , indices) # branches not true anywhere - leave out
168+ order = setdiff (sortperm (indices), negative)
168169end
169170
170- " Order the solution branches in `res` such that close classified positively by `classes` are first.
171- The order of classes has descending precedence."
172- function order_branches! (s:: Result , classes:: Vector{String} )
173- for class in reverse (classes)
174- order_branches! (s, class)
175- end
171+ find_branch_order (classification:: Array ) = collect (1 : length (classification[1 ])) # no ordering for >1D
172+
173+ " Order the solution branches in `res` such that close classified positively by `classes` are first."
174+ function order_branches! (res:: Result , classes:: Vector{String} )
175+ for class in classes
176+ order_branches! (res, find_branch_order (res. classes[class]))
177+ end
176178end
177179
180+ order_branches! (res:: Result , class:: String ) = order_branches! (res, [class])
181+
178182" Reorder the solutions in `res` to match the index permutation `order`."
179- function reorder_solutions ! (res:: Result , order:: Vector{Int64} )
180- res. solutions = reorder_array (res. solutions, order)
183+ function order_branches ! (res:: Result , order:: Vector{Int64} )
184+ res. solutions = _reorder_nested (res. solutions, order)
181185 for key in keys (res. classes)
182- res. classes[key] = reorder_array (res. classes[key], order)
186+ res. classes[key] = _reorder_nested (res. classes[key], order)
183187 end
184188end
185189
186- " Reorder EACH ELEMENT of `a` to match the index permutation `order`."
187- function reorder_array (a:: Array , order:: Vector{Int64} )
188- a[1 ] isa Array || return a
189- new_array = similar (a)
190- for (i,el) in enumerate (a)
191- new_array[i] = el[order]
192- end
193- return new_array
190+ " Reorder EACH ELEMENT of `a` to match the index permutation `order`. If length(order) < length(array), the remanining positions are kept."
191+ function _reorder_nested (a:: Array , order:: Vector{Int64} )
192+ a[1 ] isa Union{Array, BitVector} || return a
193+ order = length (order) == length (a) ? order : vcat (order, setdiff (1 : length (a[1 ]), order)) # pad if needed
194+ new_array = [el[order] for el in a]
194195end
195196
196197
0 commit comments