From 8ef75faea7b516ad6e31e705e27a30f08bc8ba4f Mon Sep 17 00:00:00 2001 From: odow Date: Mon, 4 Sep 2023 10:07:26 +1200 Subject: [PATCH 1/2] Fix ambiguity arising in SumOfSquares and PolyJuMP --- src/default_term.jl | 8 ++++++++ 1 file changed, 8 insertions(+) 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) From 7b305543009137726102f3ff60055a7af7313a29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Legat?= Date: Wed, 6 Sep 2023 13:54:06 +0200 Subject: [PATCH 2/2] Add tests --- test/mutable_arithmetics.jl | 4 ++-- test/term.jl | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) 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