Skip to content

Commit 3195d41

Browse files
odowblegat
andauthored
Fix ambiguity arising in SumOfSquares and PolyJuMP (#273)
* Fix ambiguity arising in SumOfSquares and PolyJuMP * Add tests --------- Co-authored-by: Benoît Legat <[email protected]>
1 parent 90ad74e commit 3195d41

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

src/default_term.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,14 @@ function MA.operate!(::typeof(*), t1::Term, t2::AbstractTermLike)
110110
return t1
111111
end
112112

113+
# Needed to resolve ambiguity with
114+
# MA.operate!(::typeof(*), ::AbstractMonomial, ::AbstractMonomialLike)
115+
function MA.operate!(::typeof(*), t1::Term, t2::AbstractMonomialLike)
116+
MA.operate!(*, t1.coefficient, coefficient(t2))
117+
MA.operate!(*, t1.monomial, monomial(t2))
118+
return t1
119+
end
120+
113121
function MA.operate!(::typeof(one), t::Term)
114122
MA.operate!(one, t.coefficient)
115123
MA.operate!(constant_monomial, t.monomial)

test/mutable_arithmetics.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import MutableArithmetics
2-
const MA = MutableArithmetics
1+
using Test
2+
import MutableArithmetics as MA
33

44
function all_tests(a, b, c, d, e, f, g)
55
a_copy = deepcopy(a)

test/term.jl

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
1+
import MutableArithmetics as MA
2+
import MultivariatePolynomials as MP
3+
14
struct CoefNotComparable end
25
Base.iszero(::CoefNotComparable) = false
36

7+
struct Term2{T,M} <: MP.AbstractTermLike{T}
8+
monomial::M
9+
end
10+
MP.coefficient(t::Term2{T}) where {T} = 2one(T)
11+
MP.monomial(t) = t.monomial
12+
MP.term_type(::Type{Term2{T,M}}) where {T,M} = MP.Term{T,M}
13+
414
@testset "Term" begin
515
Mod.@polyvar x
616
@test convert(Any, 1x) == 1x
@@ -95,4 +105,21 @@ Base.iszero(::CoefNotComparable) = false
95105
@test !(t1 < t2)
96106
@test t1 <= t2
97107
end
108+
109+
@testset "MA $T" for T in [Int, BigInt]
110+
M = typeof(x^2)
111+
t = one(T) * x
112+
s = MA.operate!!(*, t, x)
113+
@test monomial(s) == x^2
114+
if T == BigInt && MA.mutability(M) isa MA.IsMutable
115+
@test monomial(t) == x^2
116+
end
117+
u = MA.operate!!(*, s, Term2{T,M}(x^3))
118+
@test monomial(u) == x^5
119+
@test coefficient(u) == 2
120+
if T == BigInt && MA.mutability(M) isa MA.IsMutable
121+
@test monomial(t) == x^5
122+
@test coefficient(t) == 2
123+
end
124+
end
98125
end

0 commit comments

Comments
 (0)