Skip to content

Commit a51e5a4

Browse files
committed
Simplify changes: keep only cleaner code improvements
Per reviewer feedback, reverted complex performance-focused changes that add complexity without making the code cleaner. Kept only changes that genuinely improve code quality: Kept: - haskey() instead of `in keys()` (standard Julia idiom) - sign = -sign (cleaner) - Typed arrays Vector{Tuple{P,T}} instead of Array{Any,1} - Pre-computed param_names Set (avoids repeated map) - Typed Set{Vector{Int}} and Dict in massive_eval - Removed commented-out dead code Reverted: - Complex pre-allocated loops in det_minor_expansion_inner - Explicit loops replacing map() in extract_coefficients - Pre-allocated working arrays and @inbounds loops in massive_eval Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 07ac4cd commit a51e5a4

3 files changed

Lines changed: 23 additions & 59 deletions

File tree

src/elimination.jl

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,12 @@ function det_minor_expansion_inner(
1515
return cache[discarded]
1616
end
1717
result = 0
18-
# Use pre-allocated Set for better performance
19-
discarded_rows_set = Set(discarded[1])
20-
row = 0
21-
@inbounds for i in 1:n
22-
if !(i in discarded_rows_set)
23-
row = i
24-
break
25-
end
26-
end
27-
dis_rows = Tuple(sort!([discarded[1]..., row]))
18+
row = minimum(setdiff(Set(1:n), Set(discarded[1])))
19+
dis_rows = Tuple(sort([[i for i in discarded[1]]; row]))
2820
sign = 1
29-
discarded_cols_set = Set(discarded[2])
3021
for col in 1:n
31-
if !(col in discarded_cols_set)
32-
dis_cols = Tuple(sort!([discarded[2]..., col]))
22+
if !(col in discarded[2])
23+
dis_cols = Tuple(sort([[i for i in discarded[2]]; col]))
3324
result +=
3425
sign *
3526
m[row, col] *

src/util.jl

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -192,12 +192,7 @@ Output:
192192
function extract_coefficients(poly::P, variables::Array{P, 1}) where {P <: MPolyRingElem}
193193
xs = gens(parent(poly))
194194
@assert all(in(xs), variables)
195-
# Use a type-stable version by converting to Int explicitly
196-
cut_indices = Vector{Int}(undef, length(variables))
197-
for (j, v) in enumerate(variables)
198-
idx = findfirst(x -> x == v, xs)
199-
cut_indices[j] = idx::Int # Assert non-nothing for type stability
200-
end
195+
cut_indices = map(v -> findfirst(x -> x == v, xs), variables)
201196
coeff_indices = setdiff(collect(1:length(xs)), cut_indices)
202197
coeff_vars = xs[coeff_indices]
203198

@@ -207,25 +202,20 @@ function extract_coefficients(poly::P, variables::Array{P, 1}) where {P <: MPoly
207202

208203
result = Dict{Vector{Int}, Tuple{Vector{Vector{Int}}, Vector{FieldType}}}()
209204

210-
n_cut = length(cut_indices)
211-
n_coeff = length(coeff_indices)
212205
@inbounds for i in 1:length(poly)
213206
coef = coeff(poly, i)
214207
evec = exponent_vector(poly, i)
215-
var_slice = Vector{Int}(undef, n_cut)
216-
for j in 1:n_cut
217-
var_slice[j] = evec[cut_indices[j]]
218-
end
208+
var_slice = [evec[i] for i in cut_indices]
219209
if !haskey(result, var_slice)
220210
monom_vect, coef_vect = Vector{Vector{Int}}(), Vector{FieldType}()
221211
sizehint!(monom_vect, 8)
222212
sizehint!(coef_vect, 8)
223213
result[var_slice] = (monom_vect, coef_vect)
224214
end
225215
monom_vect, coef_vect = result[var_slice]
226-
new_monom = Vector{Int}(undef, n_coeff)
227-
for j in 1:n_coeff
228-
new_monom[j] = evec[coeff_indices[j]]
216+
new_monom = Vector{Int}(undef, length(coeff_vars))
217+
for i in 1:length(new_monom)
218+
new_monom[i] = evec[coeff_indices[i]]
229219
end
230220
push!(monom_vect, new_monom)
231221
push!(coef_vect, coef)

src/wronskian.jl

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ function monomial_compress(io_equation, params::Array{<:MPolyRingElem, 1})
4444
coef = coeff(c, lm) // leading_coefficient(basis_c)
4545
if coef != 0
4646
c = c - coef * basis_c
47-
# Update in place by creating new tuple
4847
echelon_form[i] = (echelon_form[i][1], echelon_form[i][2] + coef * p)
4948
end
5049
end
@@ -136,65 +135,49 @@ of lower degree are cached and used to compute the values of the monomials of hi
136135
"""
137136
function massive_eval(polys, eval_dict)
138137
R = parent(first(values(eval_dict)))
139-
poly_ring = parent(first(polys))
140-
poly_gens = gens(poly_ring)
141-
point = [get(eval_dict, v, zero(R)) for v in poly_gens]
138+
point = [get(eval_dict, v, zero(R)) for v in gens(parent(first(polys)))]
142139
n = length(point)
143140

144-
# Use typed Set for better performance
145141
monomials = Set{Vector{Int}}()
146142
for p in polys
147143
for exp in exponent_vectors(p)
148144
push!(monomials, exp)
149145
end
150146
end
151147

152-
# Pre-allocate the zero vector once
153-
zero_vec = zeros(Int, n)
154148
cache = Dict{Vector{Int}, typeof(one(R))}()
155-
cache[zero_vec] = one(R)
149+
cache[[0 for i in 1:n]] = one(R)
156150
cached_monoms = ExpVectTrie(n)
157-
push!(cached_monoms, zero_vec)
151+
push!(cached_monoms, [0 for _ in 1:n])
158152

159-
# Cache unit vectors
160153
for i in 1:n
161-
var_exp = zeros(Int, n)
162-
var_exp[i] = 1
154+
var_exp = [(i != j) ? 0 : 1 for j in 1:n]
163155
cache[var_exp] = point[i]
164156
push!(cached_monoms, var_exp)
165157
end
166158

167-
# Pre-allocate working arrays
168-
computed = zeros(Int, n)
169-
exp_work = zeros(Int, n)
170159
for exp in sort!(collect(monomials), by = sum)
171160
if !haskey(cache, exp)
172161
monom_val = one(R)
173-
# Use in-place operations on working arrays
174-
fill!(computed, 0)
175-
copyto!(exp_work, exp)
176-
while sum(exp_work) > 0
177-
_, below = get_max_below(cached_monoms, exp_work)
162+
computed = [0 for i in 1:n]
163+
while sum(exp) > 0
164+
_, below = get_max_below(cached_monoms, exp)
178165
monom_val = monom_val * cache[below]
179-
@inbounds for k in 1:n
180-
exp_work[k] -= below[k]
181-
computed[k] += below[k]
182-
end
183-
computed_copy = copy(computed)
184-
cache[computed_copy] = monom_val
185-
push!(cached_monoms, computed_copy)
166+
exp = exp .- below
167+
computed = computed .+ below
168+
cache[computed] = monom_val
169+
push!(cached_monoms, computed)
186170
end
187171
end
188172
end
189173

190-
# Pre-size results array with correct type
191-
results = Vector{typeof(zero(R))}(undef, length(polys))
192-
for (pidx, p) in enumerate(polys)
174+
results = []
175+
for p in polys
193176
res = zero(R)
194177
for (exp, coef) in zip(exponent_vectors(p), coefficients(p))
195178
res += coef * cache[exp]
196179
end
197-
results[pidx] = res
180+
push!(results, res)
198181
end
199182
return results
200183
end

0 commit comments

Comments
 (0)