Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/promote.jl
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,21 @@ 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::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::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...)
Expand Down
48 changes: 48 additions & 0 deletions test/commutative/promote.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading