Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ambiguity arising in SumOfSquares and PolyJuMP #273

Merged
merged 2 commits into from
Sep 6, 2023
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
8 changes: 8 additions & 0 deletions src/default_term.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,14 @@ function MA.operate!(::typeof(*), t1::Term, t2::AbstractTermLike)
return t1
end

# Needed to resolve ambiguity with
# MA.operate!(::typeof(*), ::AbstractMonomial, ::AbstractMonomialLike)
function MA.operate!(::typeof(*), t1::Term, t2::AbstractMonomialLike)
MA.operate!(*, t1.coefficient, coefficient(t2))
MA.operate!(*, t1.monomial, monomial(t2))
return t1
end

function MA.operate!(::typeof(one), t::Term)
MA.operate!(one, t.coefficient)
MA.operate!(constant_monomial, t.monomial)
Expand Down
4 changes: 2 additions & 2 deletions test/mutable_arithmetics.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import MutableArithmetics
const MA = MutableArithmetics
using Test
import MutableArithmetics as MA

function all_tests(a, b, c, d, e, f, g)
a_copy = deepcopy(a)
Expand Down
27 changes: 27 additions & 0 deletions test/term.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
import MutableArithmetics as MA
import MultivariatePolynomials as MP

struct CoefNotComparable end
Base.iszero(::CoefNotComparable) = false

struct Term2{T,M} <: MP.AbstractTermLike{T}
monomial::M
end
MP.coefficient(t::Term2{T}) where {T} = 2one(T)
MP.monomial(t) = t.monomial
MP.term_type(::Type{Term2{T,M}}) where {T,M} = MP.Term{T,M}

@testset "Term" begin
Mod.@polyvar x
@test convert(Any, 1x) == 1x
Expand Down Expand Up @@ -95,4 +105,21 @@ Base.iszero(::CoefNotComparable) = false
@test !(t1 < t2)
@test t1 <= t2
end

@testset "MA $T" for T in [Int, BigInt]
M = typeof(x^2)
t = one(T) * x
s = MA.operate!!(*, t, x)
@test monomial(s) == x^2
if T == BigInt && MA.mutability(M) isa MA.IsMutable
@test monomial(t) == x^2
end
u = MA.operate!!(*, s, Term2{T,M}(x^3))
@test monomial(u) == x^5
@test coefficient(u) == 2
if T == BigInt && MA.mutability(M) isa MA.IsMutable
@test monomial(t) == x^5
@test coefficient(t) == 2
end
end
end
Loading