Skip to content

Commit

Permalink
Fix ambiguity arising in SumOfSquares and PolyJuMP (#273)
Browse files Browse the repository at this point in the history
* Fix ambiguity arising in SumOfSquares and PolyJuMP

* Add tests

---------

Co-authored-by: Benoît Legat <[email protected]>
  • Loading branch information
odow and blegat authored Sep 6, 2023
1 parent 90ad74e commit 3195d41
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
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

2 comments on commit 3195d41

@blegat
Copy link
Member

@blegat blegat commented on 3195d41 Sep 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Error while trying to register: "Tag with name v0.5.1 already exists and points to a different commit"

Please sign in to comment.