diff --git a/src/elimination.jl b/src/elimination.jl index eeeeee89..0831b011 100644 --- a/src/elimination.jl +++ b/src/elimination.jl @@ -11,7 +11,7 @@ function det_minor_expansion_inner( if length(discarded[1]) == n return 1 end - if discarded in keys(cache) + if haskey(cache, discarded) return cache[discarded] end result = 0 @@ -25,7 +25,7 @@ function det_minor_expansion_inner( sign * m[row, col] * det_minor_expansion_inner(m, (dis_rows, dis_cols), cache) - sign = -1 * sign + sign = -sign end end if length(discarded[1]) > 1 diff --git a/src/wronskian.jl b/src/wronskian.jl index 1fc639a8..24407778 100644 --- a/src/wronskian.jl +++ b/src/wronskian.jl @@ -22,38 +22,37 @@ end function monomial_compress(io_equation, params::Array{<:MPolyRingElem, 1}) params_xs = isempty(params) ? empty(params) : gens(parent(first(params))) + # Pre-compute param string names for faster lookup + param_names = Set(var_to_str(p, xs = params_xs) for p in params) other_vars = [ - v for v in gens(parent(io_equation)) if - !(var_to_str(v) in map(p -> var_to_str(p, xs = params_xs), params)) + v for v in gens(parent(io_equation)) if !(var_to_str(v) in param_names) ] coeffdict = extract_coefficients(io_equation, other_vars) expvect = collect(keys(coeffdict)) coeffs = collect(values(coeffdict)) termlist = map(x -> prod(other_vars .^ x), expvect) - echelon_form = Array{Any, 1}() + # Use typed arrays instead of Array{Any, 1} + P = eltype(coeffs) + T = eltype(termlist) + echelon_form = Vector{Tuple{P, T}}() + sizehint!(echelon_form, length(coeffs)) for (c, p) in zip(coeffs, termlist) for i in 1:length(echelon_form) basis_c = echelon_form[i][1] - coef = coeff(c, leading_monomial(basis_c)) // leading_coefficient(basis_c) + lm = leading_monomial(basis_c) + coef = coeff(c, lm) // leading_coefficient(basis_c) if coef != 0 c = c - coef * basis_c - echelon_form[i][2] += coef * p + echelon_form[i] = (echelon_form[i][1], echelon_form[i][2] + coef * p) end end if c != 0 - push!(echelon_form, [c, p]) + push!(echelon_form, (c, p)) end end result = ([a[1] for a in echelon_form], [a[2] for a in echelon_form]) - #s = 0 - #for (a, b) in zip(result[1], result[2]) - # s += parent_ring_change(a, parent(io_equation)) * parent_ring_change(b, parent(io_equation)) - #end - #println("====================") - #println(s - io_equation) - return result end @@ -139,14 +138,14 @@ function massive_eval(polys, eval_dict) point = [get(eval_dict, v, zero(R)) for v in gens(parent(first(polys)))] n = length(point) - monomials = Set() + monomials = Set{Vector{Int}}() for p in polys for exp in exponent_vectors(p) push!(monomials, exp) end end - cache = Dict() + cache = Dict{Vector{Int}, typeof(one(R))}() cache[[0 for i in 1:n]] = one(R) cached_monoms = ExpVectTrie(n) push!(cached_monoms, [0 for _ in 1:n]) @@ -158,7 +157,7 @@ function massive_eval(polys, eval_dict) end for exp in sort!(collect(monomials), by = sum) - if !(exp in keys(cache)) + if !haskey(cache, exp) monom_val = one(R) computed = [0 for i in 1:n] while sum(exp) > 0