From 09d3424635a8ce7682fee253f36169343d13e885 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 15 Apr 2026 09:09:14 +0200 Subject: [PATCH 1/3] Implement promote_with_map for term and polynomial --- src/promote.jl | 10 ++++++++ test/commutative/promote.jl | 48 +++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) diff --git a/src/promote.jl b/src/promote.jl index 7fdc56cf..9d3d7b9b 100644 --- a/src/promote.jl +++ b/src/promote.jl @@ -408,6 +408,16 @@ function promote_variables_with_maps(a, b) return (all_vars, _map(a, all_vars)), (all_vars, _map(b, all_vars)) end +function SA.promote_with_map(t::AbstractTerm, all_vars, map::MP.ExponentMap) + mono, _ = SA.promote_with_map(monomial(t), all_vars, exp_map) + return term(coefficient(t), mono), map +end + +function SA.promote_with_map(p::AbstractPolynomial, all_vars, map::MP.ExponentMap) + new_terms = [term(coefficient(t), first(SA.promote_with_map(monomial(t), all_vars, map))) for t in terms(p)] + return polynomial(new_terms, SortedUniqState()), map +end + function SA.promote_bases_with_maps(p::_APL, q::_APL) _p, _q = promote_variables_with_maps(variables(p), variables(q)) return SA.maybe_promote(p, _p...), SA.maybe_promote(q, _q...) diff --git a/test/commutative/promote.jl b/test/commutative/promote.jl index 6c5ad67b..cc1c291a 100644 --- a/test/commutative/promote.jl +++ b/test/commutative/promote.jl @@ -274,4 +274,52 @@ end @test MP.variables(rx) == MP.variables(ry) @test mx isa MP.ExponentMap @test my isa MP.ExponentMap + + # Term with Monomial + t = 3x^2 + m = y^2 + pt, pm = SA.promote_bases(t, m) + @test MP.variables(pt) == MP.variables(pm) + @test length(MP.variables(pt)) == 2 + @test MP.coefficient(pt) == 3 + @test MP.degree(pt) == 2 + @test MP.degree(pt, x) == 2 + @test MP.degree(pt, y) == 0 + @test MP.degree(pm, x) == 0 + @test MP.degree(pm, y) == 2 + + # Term with Term + t1 = 2x^2 + t2 = 5y^3 + pt1, pt2 = SA.promote_bases(t1, t2) + @test MP.variables(pt1) == MP.variables(pt2) + @test MP.coefficient(pt1) == 2 + @test MP.coefficient(pt2) == 5 + @test MP.degree(pt1, x) == 2 + @test MP.degree(pt1, y) == 0 + @test MP.degree(pt2, x) == 0 + @test MP.degree(pt2, y) == 3 + + # Term with same variables (no promotion needed) + t3 = 4x^2 + t4 = 7x^3 + pt3, pt4 = SA.promote_bases(t3, t4) + @test MP.coefficient(pt3) == 4 + @test MP.coefficient(pt4) == 7 + + # Polynomial with Monomial + p = 2x^2 + 3x + m = y^2 + pp, pm = SA.promote_bases(p, m) + @test MP.variables(pp) == MP.variables(pm) + @test length(MP.variables(pp)) == 2 + @test MP.degree(pm, y) == 2 + @test MP.degree(pm, x) == 0 + + # Polynomial with Polynomial (different variables) + p1 = x^2 + 2x + p2 = y^3 + 3y + pp1, pp2 = SA.promote_bases(p1, p2) + @test MP.variables(pp1) == MP.variables(pp2) + @test length(MP.variables(pp1)) == 2 end From 5699d57998de15aeea51ccb010e5061c20ae4dd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 15 Apr 2026 09:25:03 +0200 Subject: [PATCH 2/3] Fix --- src/promote.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/promote.jl b/src/promote.jl index 9d3d7b9b..85ffbd94 100644 --- a/src/promote.jl +++ b/src/promote.jl @@ -408,12 +408,12 @@ function promote_variables_with_maps(a, b) return (all_vars, _map(a, all_vars)), (all_vars, _map(b, all_vars)) end -function SA.promote_with_map(t::AbstractTerm, all_vars, map::MP.ExponentMap) - mono, _ = SA.promote_with_map(monomial(t), all_vars, exp_map) +function SA.promote_with_map(t::AbstractTerm, all_vars, map::ExponentMap) + mono, _ = SA.promote_with_map(monomial(t), all_vars, map) return term(coefficient(t), mono), map end -function SA.promote_with_map(p::AbstractPolynomial, all_vars, map::MP.ExponentMap) +function SA.promote_with_map(p::AbstractPolynomial, all_vars, map::ExponentMap) new_terms = [term(coefficient(t), first(SA.promote_with_map(monomial(t), all_vars, map))) for t in terms(p)] return polynomial(new_terms, SortedUniqState()), map end From 4ba329646078635df6174efb072aa26244e368af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 15 Apr 2026 09:27:05 +0200 Subject: [PATCH 3/3] Fix format --- src/promote.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/promote.jl b/src/promote.jl index 85ffbd94..1bf86907 100644 --- a/src/promote.jl +++ b/src/promote.jl @@ -414,7 +414,12 @@ function SA.promote_with_map(t::AbstractTerm, all_vars, map::ExponentMap) end function SA.promote_with_map(p::AbstractPolynomial, all_vars, map::ExponentMap) - new_terms = [term(coefficient(t), first(SA.promote_with_map(monomial(t), all_vars, map))) for t in terms(p)] + new_terms = [ + term( + coefficient(t), + first(SA.promote_with_map(monomial(t), all_vars, map)), + ) for t in terms(p) + ] return polynomial(new_terms, SortedUniqState()), map end