Skip to content

Commit

Permalink
Avoid depending on SymbolicsNemoExt in SymbolicsGroebnerExt
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
topolarity committed Oct 17, 2024
1 parent 4fa78cc commit 2cd3d15
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 23 deletions.
42 changes: 25 additions & 17 deletions ext/SymbolicsGroebnerExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
12 changes: 6 additions & 6 deletions ext/SymbolicsNemoExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand All @@ -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])
Expand Down
2 changes: 2 additions & 0 deletions src/Symbolics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2cd3d15

Please sign in to comment.