Skip to content

Commit 117bbd5

Browse files
authored
Fix promotion for JuMP scalar functions (#42)
* Add tests * Fix format
1 parent 3ae8ad3 commit 117bbd5

File tree

5 files changed

+16
-4
lines changed

5 files changed

+16
-4
lines changed

src/chebyshev.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ _promote_div(::Type{I}) where {I<:Integer} = Rational{I}
44
_promote_div(::Type{T}) where {T<:Number} = MA.promote_operation(/, T, Int)
55
# Could be for instance `MathOptInterface.ScalarAffineFunction{Float64}`
66
# which does not support division with `Int`
7-
_promote_div(::Type{F}) where {F} = F
7+
_promote_div(::Type{F}) where {F} = _float(F)
88

99
function _promote_coef(::Type{T}, ::Type{<:AbstractChebyshev}) where {T}
1010
return _promote_div(T)

src/monomial.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ end
155155

156156
_explicit_basis(_, basis::SubBasis) = basis
157157

158+
function explicit_basis(p::MP.AbstractPolynomialLike)
159+
return SubBasis{Monomial}(MP.monomials(p))
160+
end
161+
158162
function explicit_basis(a::SA.AlgebraElement)
159163
return _explicit_basis(SA.coeffs(a), SA.basis(a))
160164
end

src/scaled.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ end
5151
_float(::Type{T}) where {T<:Number} = float(T)
5252
# Could be for instance `MathOptInterface.ScalarAffineFunction{Float64}`
5353
# which does not implement `float`
54-
_float(::Type{F}) where {F} = F
54+
_float(::Type{F}) where {F} = MA.promote_operation(*, Float64, F)
5555

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

test/monomial.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,10 @@ end
3838
# It will be sorted and 1 will be moved at the end
3939
basis = SubBasis{MB.Monomial}([1, x, y])
4040
@test polynomial_type(basis, Int) == polynomial_type(x, Int)
41-
@test polynomial(i -> i^2, basis) == 9x + 4y + 1
41+
poly = 9x + 4y + 1
42+
@test polynomial(i -> i^2, basis) == poly
4243
@test coefficients(9 + x + 4y, basis) == [9, 4, 1]
44+
@test MB.explicit_basis(poly) == basis
4345
end
4446
@testset "Quadratic" begin
4547
basis = SubBasis{MB.Monomial}([x^2, x * y, y^2])

test/runtests.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ function _test_basis(basis)
2020
MB.constant_algebra_element_type(B, Int)
2121
end
2222

23+
struct TypeA end
24+
struct TypeB end
25+
Base.zero(::Type{TypeA}) = TypeA()
26+
Base.:*(::Float64, ::TypeA) = TypeB()
27+
2328
function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree)
2429
@polyvar x[1:2]
2530
M = typeof(prod(x))
@@ -65,7 +70,8 @@ function api_test(B::Type{<:MB.AbstractMonomialIndexed}, degree)
6570
p = MB.Polynomial{B}(mono)
6671
@test full_basis[p] == mono
6772
@test full_basis[mono] == p
68-
@test polynomial_type(mono, String) == polynomial_type(typeof(p), String)
73+
@test polynomial_type(mono, B == Monomial ? TypeA : TypeB) ==
74+
polynomial_type(typeof(p), TypeA)
6975
a = MB.algebra_element(p)
7076
@test variables(a) == x
7177
@test typeof(polynomial(a)) == polynomial_type(typeof(a))

0 commit comments

Comments
 (0)