2222
2323function monomial_compress(io_equation, params:: Array{<:MPolyRingElem, 1} )
2424 params_xs = isempty(params) ? empty(params) : gens(parent(first(params)))
25+ # Pre-compute param string names for faster lookup
26+ param_names = Set(var_to_str(p, xs = params_xs) for p in params)
2527 other_vars = [
26- v for v in gens(parent(io_equation)) if
27- ! (var_to_str(v) in map(p -> var_to_str(p, xs = params_xs), params))
28+ v for v in gens(parent(io_equation)) if ! (var_to_str(v) in param_names)
2829 ]
2930 coeffdict = extract_coefficients(io_equation, other_vars)
3031 expvect = collect(keys(coeffdict))
3132 coeffs = collect(values(coeffdict))
3233 termlist = map(x -> prod(other_vars .^ x), expvect)
3334
34- echelon_form = Array{Any, 1 }()
35+ # Use typed arrays instead of Array{Any, 1}
36+ P = eltype(coeffs)
37+ T = eltype(termlist)
38+ echelon_form = Vector{Tuple{P, T}}()
39+ sizehint!(echelon_form, length(coeffs))
3540 for (c, p) in zip(coeffs, termlist)
3641 for i in 1 : length(echelon_form)
3742 basis_c = echelon_form[i][1 ]
38- coef = coeff(c, leading_monomial(basis_c)) // leading_coefficient(basis_c)
43+ lm = leading_monomial(basis_c)
44+ coef = coeff(c, lm) // leading_coefficient(basis_c)
3945 if coef != 0
4046 c = c - coef * basis_c
41- echelon_form[i][ 2 ] += coef * p
47+ echelon_form[i] = (echelon_form[i][ 1 ], echelon_form[i][ 2 ] + coef * p)
4248 end
4349 end
4450 if c != 0
45- push!(echelon_form, [ c, p] )
51+ push!(echelon_form, ( c, p) )
4652 end
4753 end
4854
4955 result = ([a[1 ] for a in echelon_form], [a[2 ] for a in echelon_form])
50- # s = 0
51- # for (a, b) in zip(result[1], result[2])
52- # s += parent_ring_change(a, parent(io_equation)) * parent_ring_change(b, parent(io_equation))
53- # end
54- # println("====================")
55- # println(s - io_equation)
56-
5756 return result
5857end
5958
@@ -139,14 +138,14 @@ function massive_eval(polys, eval_dict)
139138 point = [get(eval_dict, v, zero(R)) for v in gens(parent(first(polys)))]
140139 n = length(point)
141140
142- monomials = Set()
141+ monomials = Set{Vector{Int}} ()
143142 for p in polys
144143 for exp in exponent_vectors(p)
145144 push!(monomials, exp)
146145 end
147146 end
148147
149- cache = Dict()
148+ cache = Dict{Vector{Int}, typeof(one(R))} ()
150149 cache[[0 for i in 1 : n]] = one(R)
151150 cached_monoms = ExpVectTrie(n)
152151 push!(cached_monoms, [0 for _ in 1 : n])
@@ -158,7 +157,7 @@ function massive_eval(polys, eval_dict)
158157 end
159158
160159 for exp in sort!(collect(monomials), by = sum)
161- if ! (exp in keys( cache) )
160+ if ! haskey( cache, exp )
162161 monom_val = one(R)
163162 computed = [0 for i in 1 : n]
164163 while sum(exp) > 0
0 commit comments