Skip to content

Commit

Permalink
Fix promotion for JuMP scalar functions (#42)
Browse files Browse the repository at this point in the history
* Add tests

* Fix format
  • Loading branch information
blegat authored Jul 4, 2024
1 parent 3ae8ad3 commit 117bbd5
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/chebyshev.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ _promote_div(::Type{I}) where {I<:Integer} = Rational{I}
_promote_div(::Type{T}) where {T<:Number} = MA.promote_operation(/, T, Int)
# Could be for instance `MathOptInterface.ScalarAffineFunction{Float64}`
# which does not support division with `Int`
_promote_div(::Type{F}) where {F} = F
_promote_div(::Type{F}) where {F} = _float(F)

function _promote_coef(::Type{T}, ::Type{<:AbstractChebyshev}) where {T}
return _promote_div(T)
Expand Down
4 changes: 4 additions & 0 deletions src/monomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ end

_explicit_basis(_, basis::SubBasis) = basis

function explicit_basis(p::MP.AbstractPolynomialLike)
return SubBasis{Monomial}(MP.monomials(p))
end

function explicit_basis(a::SA.AlgebraElement)
return _explicit_basis(SA.coeffs(a), SA.basis(a))
end
Expand Down
2 changes: 1 addition & 1 deletion src/scaled.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ end
_float(::Type{T}) where {T<:Number} = float(T)
# Could be for instance `MathOptInterface.ScalarAffineFunction{Float64}`
# which does not implement `float`
_float(::Type{F}) where {F} = F
_float(::Type{F}) where {F} = MA.promote_operation(*, Float64, F)

_promote_coef(::Type{T}, ::Type{ScaledMonomial}) where {T} = _float(T)

Expand Down
4 changes: 3 additions & 1 deletion test/monomial.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,10 @@ end
# It will be sorted and 1 will be moved at the end
basis = SubBasis{MB.Monomial}([1, x, y])
@test polynomial_type(basis, Int) == polynomial_type(x, Int)
@test polynomial(i -> i^2, basis) == 9x + 4y + 1
poly = 9x + 4y + 1
@test polynomial(i -> i^2, basis) == poly
@test coefficients(9 + x + 4y, basis) == [9, 4, 1]
@test MB.explicit_basis(poly) == basis
end
@testset "Quadratic" begin
basis = SubBasis{MB.Monomial}([x^2, x * y, y^2])
Expand Down
8 changes: 7 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ function _test_basis(basis)
MB.constant_algebra_element_type(B, Int)
end

struct TypeA end
struct TypeB end
Base.zero(::Type{TypeA}) = TypeA()
Base.:*(::Float64, ::TypeA) = TypeB()

function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree)
@polyvar x[1:2]
M = typeof(prod(x))
Expand Down Expand Up @@ -65,7 +70,8 @@ function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree)
p = MB.Polynomial{B}(mono)
@test full_basis[p] == mono
@test full_basis[mono] == p
@test polynomial_type(mono, String) == polynomial_type(typeof(p), String)
@test polynomial_type(mono, B == Monomial ? TypeA : TypeB) ==
polynomial_type(typeof(p), TypeA)
a = MB.algebra_element(p)
@test variables(a) == x
@test typeof(polynomial(a)) == polynomial_type(typeof(a))
Expand Down

0 comments on commit 117bbd5

Please sign in to comment.