From 3195d41d45a08e4001ceb933bff7593f710db0e4 Mon Sep 17 00:00:00 2001 From: Oscar Dowson Date: Thu, 7 Sep 2023 01:54:29 +1200 Subject: [PATCH] Fix ambiguity arising in SumOfSquares and PolyJuMP (#273) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix ambiguity arising in SumOfSquares and PolyJuMP * Add tests --------- Co-authored-by: BenoƮt Legat --- src/default_term.jl | 8 ++++++++ test/mutable_arithmetics.jl | 4 ++-- test/term.jl | 27 +++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/default_term.jl b/src/default_term.jl index 92c9664a..35fd35fe 100644 --- a/src/default_term.jl +++ b/src/default_term.jl @@ -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) diff --git a/test/mutable_arithmetics.jl b/test/mutable_arithmetics.jl index 9a93c691..77120c85 100644 --- a/test/mutable_arithmetics.jl +++ b/test/mutable_arithmetics.jl @@ -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) diff --git a/test/term.jl b/test/term.jl index 92bfc6d1..6da09258 100644 --- a/test/term.jl +++ b/test/term.jl @@ -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 @@ -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