From 2cd3d1549b28a6d175e2e395e3e723bb7ca1d3ac Mon Sep 17 00:00:00 2001 From: Cody Tapscott Date: Thu, 17 Oct 2024 07:41:58 -0400 Subject: [PATCH] Avoid depending on `SymbolicsNemoExt` in `SymbolicsGroebnerExt` This is no longer supposed to be allowed in 1.11, but that change was unfortunately added in the 1.11.1 patch release instead of 1.11.0 like it was intended. --- ext/SymbolicsGroebnerExt.jl | 42 ++++++++++++++++++++++--------------- ext/SymbolicsNemoExt.jl | 12 +++++------ src/Symbolics.jl | 2 ++ 3 files changed, 33 insertions(+), 23 deletions(-) diff --git a/ext/SymbolicsGroebnerExt.jl b/ext/SymbolicsGroebnerExt.jl index 66d069060..1e9adc868 100644 --- a/ext/SymbolicsGroebnerExt.jl +++ b/ext/SymbolicsGroebnerExt.jl @@ -70,10 +70,10 @@ end # Map each variable of the given poly. # Can be used to transform Nemo polynomial to expression. -function nemo_crude_evaluate(poly::Nemo.MPolyRingElem, varmap) +function Symbolics.nemo_crude_evaluate(poly::Nemo.MPolyRingElem, varmap) new_poly = 0 for (i, term) in enumerate(Nemo.terms(poly)) - new_term = nemo_crude_evaluate(Nemo.coeff(poly, i), varmap) + new_term = Symbolics.nemo_crude_evaluate(Nemo.coeff(poly, i), varmap) for var in Nemo.vars(term) exp = Nemo.degree(term, var) exp == 0 && continue @@ -85,11 +85,11 @@ function nemo_crude_evaluate(poly::Nemo.MPolyRingElem, varmap) new_poly end -function nemo_crude_evaluate(poly::Nemo.FracElem, varmap) - nemo_crude_evaluate(numerator(poly), varmap) // nemo_crude_evaluate(denominator(poly), varmap) +function Symbolics.nemo_crude_evaluate(poly::Nemo.FracElem, varmap) + Symbolics.nemo_crude_evaluate(numerator(poly), varmap) // Symbolics.nemo_crude_evaluate(denominator(poly), varmap) end -function nemo_crude_evaluate(poly::Nemo.ZZRingElem, varmap) +function Symbolics.nemo_crude_evaluate(poly::Nemo.ZZRingElem, varmap) BigInt(poly) end @@ -127,7 +127,7 @@ function demote(gb, vars::Vector{Num}, params::Vector{Num}) ring_param, params_demoted = Nemo.polynomial_ring(Nemo.base_ring(ring_flat), map(string, nemo_params)) ring_demoted, vars_demoted = Nemo.polynomial_ring(Nemo.fraction_field(ring_param), map(string, nemo_vars), internal_ordering=:lex) varmap = Dict((nemo_vars .=> vars_demoted)..., (nemo_params .=> params_demoted)...) - gb_demoted = map(f -> ring_demoted(nemo_crude_evaluate(f, varmap)), nemo_gb) + gb_demoted = map(f -> ring_demoted(Symbolics.nemo_crude_evaluate(f, varmap)), nemo_gb) result = empty(gb_demoted) while true gb_demoted = map(f -> Nemo.map_coefficients(c -> c // Nemo.leading_coefficient(f), f), gb_demoted) @@ -156,7 +156,7 @@ function demote(gb, vars::Vector{Num}, params::Vector{Num}) poly = 0 for j in eachindex(monoms) - poly += nemo_crude_evaluate(coeffs[j], nemo_to_sym) * nemo_crude_evaluate(monoms[j], nemo_to_sym) + poly += Symbolics.nemo_crude_evaluate(coeffs[j], nemo_to_sym) * Symbolics.nemo_crude_evaluate(monoms[j], nemo_to_sym) end push!(final_result, poly) end @@ -317,15 +317,23 @@ function Symbolics.solve_multivar(eqs::Vector, vars::Vector{Num}; dropmultiplici sol end -# Helps with precompilation time -PrecompileTools.@setup_workload begin - @variables a b c x y z - simple_linear_equations = [x - y, y + 2z] - equations_intersect_sphere_line = [x^2 + y^2 + z^2 - 9, x - 2y + 3, y - z] - PrecompileTools.@compile_workload begin - symbolic_solve(simple_linear_equations, [x, y], warns=false) - symbolic_solve(equations_intersect_sphere_line, [x, y, z], warns=false) - end -end + +# XXX: on 1.11, SymbolicsNemoExt isn't actually loaded during the pre-compilation of SymbolicsGroebnerExt, +# so we can't run a workload using its definitions yet. (The new restriction was added in 1.11 because +# of "AB" issues where an extension A and B would mutually expect the other to load "first") +# +# This can be re-enabled when it is possible for an extension A to explicitly declare that it depends on +# an extension B. + +# # Helps with precompilation time +# PrecompileTools.@setup_workload begin + # @variables a b c x y z + # simple_linear_equations = [x - y, y + 2z] + # equations_intersect_sphere_line = [x^2 + y^2 + z^2 - 9, x - 2y + 3, y - z] + # PrecompileTools.@compile_workload begin + # symbolic_solve(simple_linear_equations, [x, y], warns=false) + # symbolic_solve(equations_intersect_sphere_line, [x, y, z], warns=false) + # end +# end end # module diff --git a/ext/SymbolicsNemoExt.jl b/ext/SymbolicsNemoExt.jl index 9c9f31db1..ee3be957b 100644 --- a/ext/SymbolicsNemoExt.jl +++ b/ext/SymbolicsNemoExt.jl @@ -12,10 +12,10 @@ end # Map each variable of the given poly. # Can be used to transform Nemo polynomial to expression. -function nemo_crude_evaluate(poly::Nemo.MPolyRingElem, varmap) +function Symbolics.nemo_crude_evaluate(poly::Nemo.MPolyRingElem, varmap) new_poly = 0 for (i, term) in enumerate(Nemo.terms(poly)) - new_term = nemo_crude_evaluate(Nemo.coeff(poly, i), varmap) + new_term = Symbolics.nemo_crude_evaluate(Nemo.coeff(poly, i), varmap) for var in Nemo.vars(term) exp = Nemo.degree(term, var) exp == 0 && continue @@ -27,11 +27,11 @@ function nemo_crude_evaluate(poly::Nemo.MPolyRingElem, varmap) new_poly end -function nemo_crude_evaluate(poly::Nemo.FracElem, varmap) - nemo_crude_evaluate(numerator(poly), varmap) // nemo_crude_evaluate(denominator(poly), varmap) +function Symbolics.nemo_crude_evaluate(poly::Nemo.FracElem, varmap) + Symbolics.nemo_crude_evaluate(numerator(poly), varmap) // Symbolics.nemo_crude_evaluate(denominator(poly), varmap) end -function nemo_crude_evaluate(poly::Nemo.ZZRingElem, varmap) +function Symbolics.nemo_crude_evaluate(poly::Nemo.ZZRingElem, varmap) Rational(poly) end @@ -48,7 +48,7 @@ function Symbolics.factor_use_nemo(poly::Num) nemo_unit = Nemo.unit(nemo_fac) nemo_factors = collect(keys(nemo_fac.fac)) sym_unit = Rational(Nemo.coeff(nemo_unit, 1)) - sym_factors = map(f -> Symbolics.wrap(nemo_crude_evaluate(f, nemo_to_sym)), nemo_factors) + sym_factors = map(f -> Symbolics.wrap(Symbolics.nemo_crude_evaluate(f, nemo_to_sym)), nemo_factors) for (i, fac) in enumerate(sym_factors) sym_factors[i] = fac^(collect(values(nemo_fac.fac))[i]) diff --git a/src/Symbolics.jl b/src/Symbolics.jl index 2b6a45912..62ca23bf2 100644 --- a/src/Symbolics.jl +++ b/src/Symbolics.jl @@ -214,6 +214,8 @@ export symbolic_solve function symbolics_to_sympy end export symbolics_to_sympy +function nemo_crude_evaluate end # for implementation in extensions + function __init__() Base.Experimental.register_error_hint(TypeError) do io, exc if exc.expected == Bool && exc.got isa Num