From c16933bbb2f10a20da89d539c91671842a48387b Mon Sep 17 00:00:00 2001 From: n0rbed Date: Fri, 30 Aug 2024 13:08:11 +0300 Subject: [PATCH] symbolic_solve([...], [x]) added --- ext/SymbolicsGroebnerExt.jl | 11 ++++++++++- test/solver.jl | 3 +++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/ext/SymbolicsGroebnerExt.jl b/ext/SymbolicsGroebnerExt.jl index 438ddf460..ebf5174a5 100644 --- a/ext/SymbolicsGroebnerExt.jl +++ b/ext/SymbolicsGroebnerExt.jl @@ -108,6 +108,7 @@ end # Given a GB in k[params][vars] produces a GB in k(params)[vars] function demote(gb, vars::Vector{Num}, params::Vector{Num}) isequal(gb, [1]) && return gb + gb = Symbolics.wrap.(SymbolicUtils.toterm.(gb)) Symbolics.check_polynomial.(gb) @@ -126,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 -> nemo_crude_evaluate(f, varmap), nemo_gb) + gb_demoted = map(f -> ring_demoted(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) @@ -176,6 +177,7 @@ function solve_zerodim(eqs::Vector, vars::Vector{Num}; dropmultiplicity=true, wa # Use a new variable to separate the input polynomials (Reference above) new_var = gen_separating_var(vars) old_len = length(vars) + old_vars = deepcopy(vars) vars = vcat(vars, new_var) new_eqs = [] @@ -204,6 +206,13 @@ function solve_zerodim(eqs::Vector, vars::Vector{Num}; dropmultiplicity=true, wa return [] end + for i in reverse(eachindex(new_eqs)) + all_present = Symbolics.get_variables(new_eqs[i]) + if length(intersect(all_present, vars)) < 1 + deleteat!(new_eqs, i) + end + end + new_eqs = demote(new_eqs, vars, params) new_eqs = map(Symbolics.unwrap, new_eqs) diff --git a/test/solver.jl b/test/solver.jl index 3dff04a47..def1507bf 100644 --- a/test/solver.jl +++ b/test/solver.jl @@ -281,6 +281,9 @@ end @variables t w u v sol = symbolic_solve([t*w - 1 ~ 4, u + v + w ~ 1], [t,w]) @test isequal(sol, [Dict(t => -5 / (-1 + u + v), w => 1 - u - v)]) + + sol = symbolic_solve([x-y, y-z], [x]) + @test isequal(sol, [Dict(x=>z)]) end @testset "Factorisation" begin